chore: use atomictypes
Some checks failed
Build / Build (push) Has been cancelled

This commit is contained in:
nadoo 2026-10-09 21:36:45 +08:00
parent 08bc767eda
commit 487da737b9
4 changed files with 31 additions and 30 deletions

View File

@ -7,7 +7,7 @@ import (
// UPStream is a dns upstream. // UPStream is a dns upstream.
type UPStream struct { type UPStream struct {
index uint32 index atomic.Uint32
servers []string servers []string
} }
@ -24,12 +24,12 @@ func NewUPStream(servers []string) *UPStream {
// Server returns a dns server. // Server returns a dns server.
func (u *UPStream) Server() string { func (u *UPStream) Server() string {
return u.servers[atomic.LoadUint32(&u.index)%uint32(len(u.servers))] return u.servers[u.index.Load()%uint32(len(u.servers))]
} }
// Switch switches to the next dns server. // Switch switches to the next dns server.
func (u *UPStream) Switch() string { func (u *UPStream) Switch() string {
return u.servers[atomic.AddUint32(&u.index, 1)%uint32(len(u.servers))] return u.servers[u.index.Add(1)%uint32(len(u.servers))]
} }
// SwitchIf switches to the next dns server if needed. // SwitchIf switches to the next dns server if needed.

View File

@ -17,7 +17,7 @@ type session struct {
mu sync.Mutex mu sync.Mutex
streams map[uint32]*stream streams map[uint32]*stream
synack map[uint32]chan synackResult synack map[uint32]chan synackResult
nextID uint32 nextID atomic.Uint32
incoming chan *stream incoming chan *stream
done chan struct{} done chan struct{}
@ -32,14 +32,15 @@ type synackResult struct {
} }
func newSession(conn net.Conn) *session { func newSession(conn net.Conn) *session {
return &session{ s := &session{
conn: conn, conn: conn,
streams: map[uint32]*stream{}, streams: map[uint32]*stream{},
synack: map[uint32]chan synackResult{}, synack: map[uint32]chan synackResult{},
nextID: 1,
incoming: make(chan *stream, 32), incoming: make(chan *stream, 32),
done: make(chan struct{}), done: make(chan struct{}),
} }
s.nextID.Store(1)
return s
} }
func (s *session) start() { func (s *session) start() {
@ -59,7 +60,7 @@ func (s *session) acceptStream() (*stream, error) {
} }
func (s *session) openStream() (*stream, error) { func (s *session) openStream() (*stream, error) {
id := atomic.AddUint32(&s.nextID, 1) - 1 id := s.nextID.Add(1) - 1
st := newStream(id, s) st := newStream(id, s)
s.mu.Lock() s.mu.Lock()
s.streams[id] = st s.streams[id] = st

View File

@ -20,11 +20,11 @@ type Forwarder struct {
proxy.Dialer proxy.Dialer
url string url string
addr string addr string
priority uint32 priority atomic.Uint32
maxFailures uint32 // maxfailures to set to Disabled maxFailures atomic.Uint32 // maxfailures to set to Disabled
disabled uint32 disabled atomic.Uint32
failures uint32 failures atomic.Uint32
latency int64 latency atomic.Int64
intface string // local interface or ip address intface string // local interface or ip address
handlers []StatusHandler handlers []StatusHandler
} }
@ -124,12 +124,12 @@ func (f *Forwarder) Dial(network, addr string) (c net.Conn, err error) {
// Failures returns the failuer count of forwarder. // Failures returns the failuer count of forwarder.
func (f *Forwarder) Failures() uint32 { func (f *Forwarder) Failures() uint32 {
return atomic.LoadUint32(&f.failures) return f.failures.Load()
} }
// IncFailures increase the failuer count by 1. // IncFailures increase the failuer count by 1.
func (f *Forwarder) IncFailures() { func (f *Forwarder) IncFailures() {
failures := atomic.AddUint32(&f.failures, 1) failures := f.failures.Add(1)
if f.MaxFailures() == 0 { if f.MaxFailures() == 0 {
return return
} }
@ -149,17 +149,17 @@ func (f *Forwarder) AddHandler(h StatusHandler) {
// Enable the forwarder. // Enable the forwarder.
func (f *Forwarder) Enable() { func (f *Forwarder) Enable() {
if atomic.CompareAndSwapUint32(&f.disabled, 1, 0) { if f.disabled.CompareAndSwap(1, 0) {
for _, h := range f.handlers { for _, h := range f.handlers {
h(f) h(f)
} }
} }
atomic.StoreUint32(&f.failures, 0) f.failures.Store(0)
} }
// Disable the forwarder. // Disable the forwarder.
func (f *Forwarder) Disable() { func (f *Forwarder) Disable() {
if atomic.CompareAndSwapUint32(&f.disabled, 0, 1) { if f.disabled.CompareAndSwap(0, 1) {
for _, h := range f.handlers { for _, h := range f.handlers {
h(f) h(f)
} }
@ -168,7 +168,7 @@ func (f *Forwarder) Disable() {
// Enabled returns the status of forwarder. // Enabled returns the status of forwarder.
func (f *Forwarder) Enabled() bool { func (f *Forwarder) Enabled() bool {
return !isTrue(atomic.LoadUint32(&f.disabled)) return !isTrue(f.disabled.Load())
} }
func isTrue(n uint32) bool { func isTrue(n uint32) bool {
@ -177,30 +177,30 @@ func isTrue(n uint32) bool {
// Priority returns the priority of forwarder. // Priority returns the priority of forwarder.
func (f *Forwarder) Priority() uint32 { func (f *Forwarder) Priority() uint32 {
return atomic.LoadUint32(&f.priority) return f.priority.Load()
} }
// SetPriority sets the priority of forwarder. // SetPriority sets the priority of forwarder.
func (f *Forwarder) SetPriority(l uint32) { func (f *Forwarder) SetPriority(l uint32) {
atomic.StoreUint32(&f.priority, l) f.priority.Store(l)
} }
// MaxFailures returns the maxFailures of forwarder. // MaxFailures returns the maxFailures of forwarder.
func (f *Forwarder) MaxFailures() uint32 { func (f *Forwarder) MaxFailures() uint32 {
return atomic.LoadUint32(&f.maxFailures) return f.maxFailures.Load()
} }
// SetMaxFailures sets the maxFailures of forwarder. // SetMaxFailures sets the maxFailures of forwarder.
func (f *Forwarder) SetMaxFailures(l uint32) { func (f *Forwarder) SetMaxFailures(l uint32) {
atomic.StoreUint32(&f.maxFailures, l) f.maxFailures.Store(l)
} }
// Latency returns the latency of forwarder. // Latency returns the latency of forwarder.
func (f *Forwarder) Latency() int64 { func (f *Forwarder) Latency() int64 {
return atomic.LoadInt64(&f.latency) return f.latency.Load()
} }
// SetLatency sets the latency of forwarder. // SetLatency sets the latency of forwarder.
func (f *Forwarder) SetLatency(l int64) { func (f *Forwarder) SetLatency(l int64) {
atomic.StoreInt64(&f.latency, l) f.latency.Store(l)
} }

View File

@ -23,8 +23,8 @@ type FwdrGroup struct {
fwdrs []*Forwarder fwdrs []*Forwarder
avail []*Forwarder // available forwarders avail []*Forwarder // available forwarders
mu sync.RWMutex mu sync.RWMutex
index uint32 index atomic.Uint32
priority uint32 priority atomic.Uint32
next func(addr string) *Forwarder next func(addr string) *Forwarder
} }
@ -123,17 +123,17 @@ func (p *FwdrGroup) NextDialer(dstAddr string) proxy.Dialer {
defer p.mu.RUnlock() defer p.mu.RUnlock()
if len(p.avail) == 0 { if len(p.avail) == 0 {
return p.fwdrs[atomic.AddUint32(&p.index, 1)%uint32(len(p.fwdrs))] return p.fwdrs[p.index.Add(1)%uint32(len(p.fwdrs))]
} }
return p.next(dstAddr) return p.next(dstAddr)
} }
// Priority returns the active priority of dialer. // Priority returns the active priority of dialer.
func (p *FwdrGroup) Priority() uint32 { return atomic.LoadUint32(&p.priority) } func (p *FwdrGroup) Priority() uint32 { return p.priority.Load() }
// SetPriority sets the active priority of daler. // SetPriority sets the active priority of daler.
func (p *FwdrGroup) SetPriority(pri uint32) { atomic.StoreUint32(&p.priority, pri) } func (p *FwdrGroup) SetPriority(pri uint32) { p.priority.Store(pri) }
// init traverse d.fwdrs and init the available forwarder slice. // init traverse d.fwdrs and init the available forwarder slice.
func (p *FwdrGroup) init() { func (p *FwdrGroup) init() {
@ -287,7 +287,7 @@ func (p *FwdrGroup) setLatency(fwdr *Forwarder, elapsed time.Duration) {
// Round Robin. // Round Robin.
func (p *FwdrGroup) scheduleRR(dstAddr string) *Forwarder { func (p *FwdrGroup) scheduleRR(dstAddr string) *Forwarder {
return p.avail[atomic.AddUint32(&p.index, 1)%uint32(len(p.avail))] return p.avail[p.index.Add(1)%uint32(len(p.avail))]
} }
// High Availability. // High Availability.