Skip to content

Use atomic bool #4886

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 17 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update:use atomic.Int64
  • Loading branch information
me-cs committed May 22, 2025
commit 56c390e750714bb133f237677b4cc044acfc4457
2 changes: 1 addition & 1 deletion core/breaker/googlebreaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type (
k float64
stat *collection.RollingWindow[int64, *bucket]
proba *mathx.Proba
lastPass *syncx.AtomicDuration
lastPass syncx.AtomicDuration
}

windowResult struct {
Expand Down
2 changes: 1 addition & 1 deletion core/discov/subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func newContainer(exclusive bool) *container {
exclusive: exclusive,
values: make(map[string][]string),
mapping: make(map[string]string),
dirty: syncx.ForAtomicBool(true),
dirty: syncx.AtomicBoolFromVal(true),
}
}

Expand Down
2 changes: 1 addition & 1 deletion core/executors/lessexecutor.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
// A LessExecutor is an executor to limit execution once within given time interval.
type LessExecutor struct {
threshold time.Duration
lastTime *syncx.AtomicDuration
lastTime syncx.AtomicDuration
}

// NewLessExecutor returns a LessExecutor with given threshold as time interval.
Expand Down
6 changes: 3 additions & 3 deletions core/load/adaptiveshedder.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ var (
ErrServiceOverloaded = errors.New("service overloaded")

// default to be enabled
enabled = syncx.ForAtomicBool(true)
enabled = syncx.AtomicBoolFromVal(true)
// default to be enabled
logEnabled = syncx.ForAtomicBool(true)
logEnabled = syncx.AtomicBoolFromVal(true)
// make it a variable for unit test
systemOverloadChecker = func(cpuThreshold int64) bool {
return stat.CpuUsage() >= cpuThreshold
Expand Down Expand Up @@ -74,7 +74,7 @@ type (
flying int64
avgFlying float64
avgFlyingLock syncx.SpinLock
overloadTime *syncx.AtomicDuration
overloadTime syncx.AtomicDuration
droppedRecently atomic.Bool
passCounter *collection.RollingWindow[int64, *collection.Bucket[int64]]
rtCounter *collection.RollingWindow[int64, *collection.Bucket[int64]]
Expand Down
2 changes: 1 addition & 1 deletion core/logx/limitedexecutor.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

type limitedExecutor struct {
threshold time.Duration
lastTime *syncx.AtomicDuration
lastTime syncx.AtomicDuration
discarded uint32
}

Expand Down
2 changes: 1 addition & 1 deletion core/logx/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ var (
// ErrLogServiceNameNotSet is an error that indicates that the service name is not set.
ErrLogServiceNameNotSet = errors.New("log service name must be set")
// ExitOnFatal defines whether to exit on fatal errors, defined here to make it easier to test.
ExitOnFatal = syncx.ForAtomicBool(true)
ExitOnFatal = syncx.AtomicBoolFromVal(true)

truncatedField = Field(truncatedKey, true)
)
2 changes: 1 addition & 1 deletion core/stat/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var (
logInterval = time.Minute
writerLock sync.Mutex
reportWriter Writer = nil
logEnabled = syncx.ForAtomicBool(true)
logEnabled = syncx.AtomicBoolFromVal(true)
)

type (
Expand Down
4 changes: 2 additions & 2 deletions core/stores/mon/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ const defaultTimeout = time.Second * 3

var (
slowThreshold = syncx.ForAtomicDuration(defaultSlowThreshold)
logMon = syncx.ForAtomicBool(true)
logSlowMon = syncx.ForAtomicBool(true)
logMon = syncx.AtomicBoolFromVal(true)
logSlowMon = syncx.AtomicBoolFromVal(true)
)

type (
Expand Down
4 changes: 2 additions & 2 deletions core/stores/sqlx/stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ const defaultSlowThreshold = time.Millisecond * 500

var (
slowThreshold = syncx.ForAtomicDuration(defaultSlowThreshold)
logSql = syncx.ForAtomicBool(true)
logSlowSql = syncx.ForAtomicBool(true)
logSql = syncx.AtomicBoolFromVal(true)
logSlowSql = syncx.AtomicBoolFromVal(true)
)

type (
Expand Down
15 changes: 11 additions & 4 deletions core/syncx/atomicbool.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ type AtomicBool atomic.Bool

// NewAtomicBool returns an AtomicBool.
// Deprecated: use atomic.Bool instead.
func NewAtomicBool() AtomicBool {
return AtomicBool(ForAtomicBool(false))
func NewAtomicBool() (d AtomicBool) {
return
}

// ForAtomicBool returns an atomic.Bool with given val.
func ForAtomicBool(val bool) (b atomic.Bool) {
b.Store(val)
// Deprecated: use atomic.Bool instead.
func ForAtomicBool(val bool) (b AtomicBool) {
b.Set(val)
return
}

Expand All @@ -33,3 +34,9 @@ func (b *AtomicBool) Set(v bool) {
func (b *AtomicBool) True() bool {
return (*atomic.Bool)(b).Load()
}

// AtomicBoolFromVal returns an atomic.Bool with given val.
func AtomicBoolFromVal(val bool) (b atomic.Bool) {
b.Store(val)
return
}
2 changes: 1 addition & 1 deletion core/syncx/atomicbool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func TestAtomicBool(t *testing.T) {
val := AtomicBool(ForAtomicBool(true))
val := ForAtomicBool(true)
assert.True(t, val.True())
val.Set(false)
assert.False(t, val.True())
Expand Down
20 changes: 10 additions & 10 deletions core/syncx/atomicduration.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,31 @@ import (
)

// An AtomicDuration is an implementation of atomic duration.
type AtomicDuration int64
type AtomicDuration atomic.Int64

// NewAtomicDuration returns an AtomicDuration.
func NewAtomicDuration() *AtomicDuration {
return new(AtomicDuration)
func NewAtomicDuration() (d AtomicDuration) {
d.Set(0)
return
}

// ForAtomicDuration returns an AtomicDuration with given value.
func ForAtomicDuration(val time.Duration) *AtomicDuration {
d := NewAtomicDuration()
d.Set(val)
return d
func ForAtomicDuration(val time.Duration) (i AtomicDuration) {
i.Set(val)
return
}

// CompareAndSwap compares current value with old, if equals, set the value to val.
func (d *AtomicDuration) CompareAndSwap(old, val time.Duration) bool {
return atomic.CompareAndSwapInt64((*int64)(d), int64(old), int64(val))
return (*atomic.Int64)(d).CompareAndSwap(int64(old), int64(val))
}

// Load loads the current duration.
func (d *AtomicDuration) Load() time.Duration {
return time.Duration(atomic.LoadInt64((*int64)(d)))
return time.Duration((*atomic.Int64)(d).Load())
}

// Set sets the value to val.
func (d *AtomicDuration) Set(val time.Duration) {
atomic.StoreInt64((*int64)(d), int64(val))
(*atomic.Int64)(d).Store(int64(val))
}
2 changes: 1 addition & 1 deletion core/syncx/immutableresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type (
err error
lock sync.RWMutex
refreshInterval time.Duration
lastTime *AtomicDuration
lastTime AtomicDuration
}
)

Expand Down
2 changes: 1 addition & 1 deletion zrpc/internal/balancer/p2c/p2c.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func newBuilder() balancer.Builder {
type p2cPicker struct {
conns []*subConn
r *rand.Rand
stamp *syncx.AtomicDuration
stamp syncx.AtomicDuration
lock sync.Mutex
}

Expand Down
Loading