From fbce316b14813b79a979f56a58ed50244f5bf3b0 Mon Sep 17 00:00:00 2001 From: gVisor bot Date: Tue, 10 Oct 2023 16:34:33 +0800 Subject: [PATCH] chore: better atomic using --- adapter/adapter.go | 4 ++-- adapter/outboundgroup/groupbase.go | 2 +- adapter/provider/healthcheck.go | 21 +++++++++--------- common/atomic/type.go | 35 ++++++++++++------------------ common/atomic/value.go | 5 ++--- dns/client.go | 6 ++--- dns/resolver.go | 2 +- transport/gun/gun.go | 2 +- tunnel/statistic/manager.go | 12 +++++----- tunnel/statistic/tracker.go | 16 +++++++------- 10 files changed, 48 insertions(+), 57 deletions(-) diff --git a/adapter/adapter.go b/adapter/adapter.go index e9ce59bb..62941e6d 100644 --- a/adapter/adapter.go +++ b/adapter/adapter.go @@ -30,13 +30,13 @@ const ( type extraProxyState struct { history *queue.Queue[C.DelayHistory] - alive *atomic.Bool + alive atomic.Bool } type Proxy struct { C.ProxyAdapter history *queue.Queue[C.DelayHistory] - alive *atomic.Bool + alive atomic.Bool url string extra *xsync.MapOf[string, *extraProxyState] } diff --git a/adapter/outboundgroup/groupbase.go b/adapter/outboundgroup/groupbase.go index 66776bf5..22dd407b 100644 --- a/adapter/outboundgroup/groupbase.go +++ b/adapter/outboundgroup/groupbase.go @@ -28,7 +28,7 @@ type GroupBase struct { failedTestMux sync.Mutex failedTimes int failedTime time.Time - failedTesting *atomic.Bool + failedTesting atomic.Bool proxies [][]C.Proxy versions []atomic.Uint32 } diff --git a/adapter/provider/healthcheck.go b/adapter/provider/healthcheck.go index 35327b1c..feb972ab 100644 --- a/adapter/provider/healthcheck.go +++ b/adapter/provider/healthcheck.go @@ -34,12 +34,12 @@ type HealthCheck struct { url string extra map[string]*extraOption mu sync.Mutex - started *atomic.Bool + started atomic.Bool proxies []C.Proxy - interval uint + interval time.Duration lazy bool expectedStatus utils.IntRanges[uint16] - lastTouch *atomic.Int64 + lastTouch atomic.TypedValue[time.Time] done chan struct{} singleDo *singledo.Single[struct{}] } @@ -50,13 +50,14 @@ func (hc *HealthCheck) process() { return } - ticker := time.NewTicker(time.Duration(hc.interval) * time.Second) + ticker := time.NewTicker(hc.interval) hc.start() for { select { case <-ticker.C: - now := time.Now().Unix() - if !hc.lazy || now-hc.lastTouch.Load() < int64(hc.interval) { + lastTouch := hc.lastTouch.Load() + since := time.Since(lastTouch) + if !hc.lazy || since < hc.interval { hc.check() } else { log.Debugln("Skip once health check because we are lazy") @@ -85,7 +86,7 @@ func (hc *HealthCheck) registerHealthCheckTask(url string, expectedStatus utils. // if the provider has not set up health checks, then modify it to be the same as the group's interval if hc.interval == 0 { - hc.interval = interval + hc.interval = time.Duration(interval) * time.Second } if hc.extra == nil { @@ -135,7 +136,7 @@ func (hc *HealthCheck) auto() bool { } func (hc *HealthCheck) touch() { - hc.lastTouch.Store(time.Now().Unix()) + hc.lastTouch.Store(time.Now()) } func (hc *HealthCheck) start() { @@ -228,11 +229,9 @@ func NewHealthCheck(proxies []C.Proxy, url string, interval uint, lazy bool, exp proxies: proxies, url: url, extra: map[string]*extraOption{}, - started: atomic.NewBool(false), - interval: interval, + interval: time.Duration(interval) * time.Second, lazy: lazy, expectedStatus: expectedStatus, - lastTouch: atomic.NewInt64(0), done: make(chan struct{}, 1), singleDo: singledo.NewSingle[struct{}](time.Second), } diff --git a/common/atomic/type.go b/common/atomic/type.go index f1549235..71695c63 100644 --- a/common/atomic/type.go +++ b/common/atomic/type.go @@ -11,10 +11,9 @@ type Bool struct { atomic.Bool } -func NewBool(val bool) *Bool { - i := &Bool{} +func NewBool(val bool) (i Bool) { i.Store(val) - return i + return } func (i *Bool) MarshalJSON() ([]byte, error) { @@ -39,12 +38,11 @@ type Pointer[T any] struct { atomic.Pointer[T] } -func NewPointer[T any](v *T) *Pointer[T] { - var p Pointer[T] +func NewPointer[T any](v *T) (p Pointer[T]) { if v != nil { p.Store(v) } - return &p + return } func (p *Pointer[T]) MarshalJSON() ([]byte, error) { @@ -68,10 +66,9 @@ type Int32 struct { atomic.Int32 } -func NewInt32(val int32) *Int32 { - i := &Int32{} +func NewInt32(val int32) (i Int32) { i.Store(val) - return i + return } func (i *Int32) MarshalJSON() ([]byte, error) { @@ -96,10 +93,9 @@ type Int64 struct { atomic.Int64 } -func NewInt64(val int64) *Int64 { - i := &Int64{} +func NewInt64(val int64) (i Int64) { i.Store(val) - return i + return } func (i *Int64) MarshalJSON() ([]byte, error) { @@ -124,10 +120,9 @@ type Uint32 struct { atomic.Uint32 } -func NewUint32(val uint32) *Uint32 { - i := &Uint32{} +func NewUint32(val uint32) (i Uint32) { i.Store(val) - return i + return } func (i *Uint32) MarshalJSON() ([]byte, error) { @@ -152,10 +147,9 @@ type Uint64 struct { atomic.Uint64 } -func NewUint64(val uint64) *Uint64 { - i := &Uint64{} +func NewUint64(val uint64) (i Uint64) { i.Store(val) - return i + return } func (i *Uint64) MarshalJSON() ([]byte, error) { @@ -180,10 +174,9 @@ type Uintptr struct { atomic.Uintptr } -func NewUintptr(val uintptr) *Uintptr { - i := &Uintptr{} +func NewUintptr(val uintptr) (i Uintptr) { i.Store(val) - return i + return } func (i *Uintptr) MarshalJSON() ([]byte, error) { diff --git a/common/atomic/value.go b/common/atomic/value.go index ca0eb631..95733533 100644 --- a/common/atomic/value.go +++ b/common/atomic/value.go @@ -51,8 +51,7 @@ func (t *TypedValue[T]) UnmarshalJSON(b []byte) error { return nil } -func NewTypedValue[T any](t T) *TypedValue[T] { - v := &TypedValue[T]{} +func NewTypedValue[T any](t T) (v TypedValue[T]) { v.Store(t) - return v + return } diff --git a/dns/client.go b/dns/client.go index 56f55668..89f083aa 100644 --- a/dns/client.go +++ b/dns/client.go @@ -23,7 +23,7 @@ type client struct { r *Resolver port string host string - iface *atomic.TypedValue[string] + iface atomic.TypedValue[string] proxyAdapter C.ProxyAdapter proxyName string addr string @@ -77,8 +77,8 @@ func (c *client) ExchangeContext(ctx context.Context, m *D.Msg) (*D.Msg, error) network = "tcp" } - options := []dialer.Option{} - if c.iface != nil && c.iface.Load() != "" { + var options []dialer.Option + if c.iface.Load() != "" { options = append(options, dialer.WithInterface(c.iface.Load())) } diff --git a/dns/resolver.go b/dns/resolver.go index 3a530918..2c429358 100644 --- a/dns/resolver.go +++ b/dns/resolver.go @@ -398,7 +398,7 @@ func (r *Resolver) Invalid() bool { type NameServer struct { Net string Addr string - Interface *atomic.TypedValue[string] + Interface atomic.TypedValue[string] ProxyAdapter C.ProxyAdapter ProxyName string Params map[string]string diff --git a/transport/gun/gun.go b/transport/gun/gun.go index e98f7fb5..cfe8aa3d 100644 --- a/transport/gun/gun.go +++ b/transport/gun/gun.go @@ -43,7 +43,7 @@ type Conn struct { transport *TransportWrap writer *io.PipeWriter once sync.Once - close *atomic.Bool + close atomic.Bool err error remain int br *bufio.Reader diff --git a/tunnel/statistic/manager.go b/tunnel/statistic/manager.go index 19ce58d9..4797829f 100644 --- a/tunnel/statistic/manager.go +++ b/tunnel/statistic/manager.go @@ -29,12 +29,12 @@ func init() { type Manager struct { connections *xsync.MapOf[string, Tracker] - uploadTemp *atomic.Int64 - downloadTemp *atomic.Int64 - uploadBlip *atomic.Int64 - downloadBlip *atomic.Int64 - uploadTotal *atomic.Int64 - downloadTotal *atomic.Int64 + uploadTemp atomic.Int64 + downloadTemp atomic.Int64 + uploadBlip atomic.Int64 + downloadBlip atomic.Int64 + uploadTotal atomic.Int64 + downloadTotal atomic.Int64 process *process.Process memory uint64 } diff --git a/tunnel/statistic/tracker.go b/tunnel/statistic/tracker.go index f0f868de..bc2dbd5c 100644 --- a/tunnel/statistic/tracker.go +++ b/tunnel/statistic/tracker.go @@ -23,14 +23,14 @@ type Tracker interface { } type TrackerInfo struct { - UUID uuid.UUID `json:"id"` - Metadata *C.Metadata `json:"metadata"` - UploadTotal *atomic.Int64 `json:"upload"` - DownloadTotal *atomic.Int64 `json:"download"` - Start time.Time `json:"start"` - Chain C.Chain `json:"chains"` - Rule string `json:"rule"` - RulePayload string `json:"rulePayload"` + UUID uuid.UUID `json:"id"` + Metadata *C.Metadata `json:"metadata"` + UploadTotal atomic.Int64 `json:"upload"` + DownloadTotal atomic.Int64 `json:"download"` + Start time.Time `json:"start"` + Chain C.Chain `json:"chains"` + Rule string `json:"rule"` + RulePayload string `json:"rulePayload"` } type tcpTracker struct {