chore: better atomic using

This commit is contained in:
gVisor bot 2023-10-10 16:34:33 +08:00
parent 7554cc3b26
commit fbce316b14
10 changed files with 48 additions and 57 deletions

View file

@ -30,13 +30,13 @@ const (
type extraProxyState struct { type extraProxyState struct {
history *queue.Queue[C.DelayHistory] history *queue.Queue[C.DelayHistory]
alive *atomic.Bool alive atomic.Bool
} }
type Proxy struct { type Proxy struct {
C.ProxyAdapter C.ProxyAdapter
history *queue.Queue[C.DelayHistory] history *queue.Queue[C.DelayHistory]
alive *atomic.Bool alive atomic.Bool
url string url string
extra *xsync.MapOf[string, *extraProxyState] extra *xsync.MapOf[string, *extraProxyState]
} }

View file

@ -28,7 +28,7 @@ type GroupBase struct {
failedTestMux sync.Mutex failedTestMux sync.Mutex
failedTimes int failedTimes int
failedTime time.Time failedTime time.Time
failedTesting *atomic.Bool failedTesting atomic.Bool
proxies [][]C.Proxy proxies [][]C.Proxy
versions []atomic.Uint32 versions []atomic.Uint32
} }

View file

@ -34,12 +34,12 @@ type HealthCheck struct {
url string url string
extra map[string]*extraOption extra map[string]*extraOption
mu sync.Mutex mu sync.Mutex
started *atomic.Bool started atomic.Bool
proxies []C.Proxy proxies []C.Proxy
interval uint interval time.Duration
lazy bool lazy bool
expectedStatus utils.IntRanges[uint16] expectedStatus utils.IntRanges[uint16]
lastTouch *atomic.Int64 lastTouch atomic.TypedValue[time.Time]
done chan struct{} done chan struct{}
singleDo *singledo.Single[struct{}] singleDo *singledo.Single[struct{}]
} }
@ -50,13 +50,14 @@ func (hc *HealthCheck) process() {
return return
} }
ticker := time.NewTicker(time.Duration(hc.interval) * time.Second) ticker := time.NewTicker(hc.interval)
hc.start() hc.start()
for { for {
select { select {
case <-ticker.C: case <-ticker.C:
now := time.Now().Unix() lastTouch := hc.lastTouch.Load()
if !hc.lazy || now-hc.lastTouch.Load() < int64(hc.interval) { since := time.Since(lastTouch)
if !hc.lazy || since < hc.interval {
hc.check() hc.check()
} else { } else {
log.Debugln("Skip once health check because we are lazy") 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 the provider has not set up health checks, then modify it to be the same as the group's interval
if hc.interval == 0 { if hc.interval == 0 {
hc.interval = interval hc.interval = time.Duration(interval) * time.Second
} }
if hc.extra == nil { if hc.extra == nil {
@ -135,7 +136,7 @@ func (hc *HealthCheck) auto() bool {
} }
func (hc *HealthCheck) touch() { func (hc *HealthCheck) touch() {
hc.lastTouch.Store(time.Now().Unix()) hc.lastTouch.Store(time.Now())
} }
func (hc *HealthCheck) start() { func (hc *HealthCheck) start() {
@ -228,11 +229,9 @@ func NewHealthCheck(proxies []C.Proxy, url string, interval uint, lazy bool, exp
proxies: proxies, proxies: proxies,
url: url, url: url,
extra: map[string]*extraOption{}, extra: map[string]*extraOption{},
started: atomic.NewBool(false), interval: time.Duration(interval) * time.Second,
interval: interval,
lazy: lazy, lazy: lazy,
expectedStatus: expectedStatus, expectedStatus: expectedStatus,
lastTouch: atomic.NewInt64(0),
done: make(chan struct{}, 1), done: make(chan struct{}, 1),
singleDo: singledo.NewSingle[struct{}](time.Second), singleDo: singledo.NewSingle[struct{}](time.Second),
} }

View file

@ -11,10 +11,9 @@ type Bool struct {
atomic.Bool atomic.Bool
} }
func NewBool(val bool) *Bool { func NewBool(val bool) (i Bool) {
i := &Bool{}
i.Store(val) i.Store(val)
return i return
} }
func (i *Bool) MarshalJSON() ([]byte, error) { func (i *Bool) MarshalJSON() ([]byte, error) {
@ -39,12 +38,11 @@ type Pointer[T any] struct {
atomic.Pointer[T] atomic.Pointer[T]
} }
func NewPointer[T any](v *T) *Pointer[T] { func NewPointer[T any](v *T) (p Pointer[T]) {
var p Pointer[T]
if v != nil { if v != nil {
p.Store(v) p.Store(v)
} }
return &p return
} }
func (p *Pointer[T]) MarshalJSON() ([]byte, error) { func (p *Pointer[T]) MarshalJSON() ([]byte, error) {
@ -68,10 +66,9 @@ type Int32 struct {
atomic.Int32 atomic.Int32
} }
func NewInt32(val int32) *Int32 { func NewInt32(val int32) (i Int32) {
i := &Int32{}
i.Store(val) i.Store(val)
return i return
} }
func (i *Int32) MarshalJSON() ([]byte, error) { func (i *Int32) MarshalJSON() ([]byte, error) {
@ -96,10 +93,9 @@ type Int64 struct {
atomic.Int64 atomic.Int64
} }
func NewInt64(val int64) *Int64 { func NewInt64(val int64) (i Int64) {
i := &Int64{}
i.Store(val) i.Store(val)
return i return
} }
func (i *Int64) MarshalJSON() ([]byte, error) { func (i *Int64) MarshalJSON() ([]byte, error) {
@ -124,10 +120,9 @@ type Uint32 struct {
atomic.Uint32 atomic.Uint32
} }
func NewUint32(val uint32) *Uint32 { func NewUint32(val uint32) (i Uint32) {
i := &Uint32{}
i.Store(val) i.Store(val)
return i return
} }
func (i *Uint32) MarshalJSON() ([]byte, error) { func (i *Uint32) MarshalJSON() ([]byte, error) {
@ -152,10 +147,9 @@ type Uint64 struct {
atomic.Uint64 atomic.Uint64
} }
func NewUint64(val uint64) *Uint64 { func NewUint64(val uint64) (i Uint64) {
i := &Uint64{}
i.Store(val) i.Store(val)
return i return
} }
func (i *Uint64) MarshalJSON() ([]byte, error) { func (i *Uint64) MarshalJSON() ([]byte, error) {
@ -180,10 +174,9 @@ type Uintptr struct {
atomic.Uintptr atomic.Uintptr
} }
func NewUintptr(val uintptr) *Uintptr { func NewUintptr(val uintptr) (i Uintptr) {
i := &Uintptr{}
i.Store(val) i.Store(val)
return i return
} }
func (i *Uintptr) MarshalJSON() ([]byte, error) { func (i *Uintptr) MarshalJSON() ([]byte, error) {

View file

@ -51,8 +51,7 @@ func (t *TypedValue[T]) UnmarshalJSON(b []byte) error {
return nil return nil
} }
func NewTypedValue[T any](t T) *TypedValue[T] { func NewTypedValue[T any](t T) (v TypedValue[T]) {
v := &TypedValue[T]{}
v.Store(t) v.Store(t)
return v return
} }

View file

@ -23,7 +23,7 @@ type client struct {
r *Resolver r *Resolver
port string port string
host string host string
iface *atomic.TypedValue[string] iface atomic.TypedValue[string]
proxyAdapter C.ProxyAdapter proxyAdapter C.ProxyAdapter
proxyName string proxyName string
addr string addr string
@ -77,8 +77,8 @@ func (c *client) ExchangeContext(ctx context.Context, m *D.Msg) (*D.Msg, error)
network = "tcp" network = "tcp"
} }
options := []dialer.Option{} var options []dialer.Option
if c.iface != nil && c.iface.Load() != "" { if c.iface.Load() != "" {
options = append(options, dialer.WithInterface(c.iface.Load())) options = append(options, dialer.WithInterface(c.iface.Load()))
} }

View file

@ -398,7 +398,7 @@ func (r *Resolver) Invalid() bool {
type NameServer struct { type NameServer struct {
Net string Net string
Addr string Addr string
Interface *atomic.TypedValue[string] Interface atomic.TypedValue[string]
ProxyAdapter C.ProxyAdapter ProxyAdapter C.ProxyAdapter
ProxyName string ProxyName string
Params map[string]string Params map[string]string

View file

@ -43,7 +43,7 @@ type Conn struct {
transport *TransportWrap transport *TransportWrap
writer *io.PipeWriter writer *io.PipeWriter
once sync.Once once sync.Once
close *atomic.Bool close atomic.Bool
err error err error
remain int remain int
br *bufio.Reader br *bufio.Reader

View file

@ -29,12 +29,12 @@ func init() {
type Manager struct { type Manager struct {
connections *xsync.MapOf[string, Tracker] connections *xsync.MapOf[string, Tracker]
uploadTemp *atomic.Int64 uploadTemp atomic.Int64
downloadTemp *atomic.Int64 downloadTemp atomic.Int64
uploadBlip *atomic.Int64 uploadBlip atomic.Int64
downloadBlip *atomic.Int64 downloadBlip atomic.Int64
uploadTotal *atomic.Int64 uploadTotal atomic.Int64
downloadTotal *atomic.Int64 downloadTotal atomic.Int64
process *process.Process process *process.Process
memory uint64 memory uint64
} }

View file

@ -25,8 +25,8 @@ type Tracker interface {
type TrackerInfo struct { type TrackerInfo struct {
UUID uuid.UUID `json:"id"` UUID uuid.UUID `json:"id"`
Metadata *C.Metadata `json:"metadata"` Metadata *C.Metadata `json:"metadata"`
UploadTotal *atomic.Int64 `json:"upload"` UploadTotal atomic.Int64 `json:"upload"`
DownloadTotal *atomic.Int64 `json:"download"` DownloadTotal atomic.Int64 `json:"download"`
Start time.Time `json:"start"` Start time.Time `json:"start"`
Chain C.Chain `json:"chains"` Chain C.Chain `json:"chains"`
Rule string `json:"rule"` Rule string `json:"rule"`