mirror of
https://github.com/nadoo/glider.git
synced 2026-10-12 06:45:49 +08:00
This commit is contained in:
parent
08bc767eda
commit
487da737b9
@ -7,7 +7,7 @@ import (
|
||||
|
||||
// UPStream is a dns upstream.
|
||||
type UPStream struct {
|
||||
index uint32
|
||||
index atomic.Uint32
|
||||
servers []string
|
||||
}
|
||||
|
||||
@ -24,12 +24,12 @@ func NewUPStream(servers []string) *UPStream {
|
||||
|
||||
// Server returns a dns server.
|
||||
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.
|
||||
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.
|
||||
|
||||
@ -17,7 +17,7 @@ type session struct {
|
||||
mu sync.Mutex
|
||||
streams map[uint32]*stream
|
||||
synack map[uint32]chan synackResult
|
||||
nextID uint32
|
||||
nextID atomic.Uint32
|
||||
|
||||
incoming chan *stream
|
||||
done chan struct{}
|
||||
@ -32,14 +32,15 @@ type synackResult struct {
|
||||
}
|
||||
|
||||
func newSession(conn net.Conn) *session {
|
||||
return &session{
|
||||
s := &session{
|
||||
conn: conn,
|
||||
streams: map[uint32]*stream{},
|
||||
synack: map[uint32]chan synackResult{},
|
||||
nextID: 1,
|
||||
incoming: make(chan *stream, 32),
|
||||
done: make(chan struct{}),
|
||||
}
|
||||
s.nextID.Store(1)
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *session) start() {
|
||||
@ -59,7 +60,7 @@ func (s *session) acceptStream() (*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)
|
||||
s.mu.Lock()
|
||||
s.streams[id] = st
|
||||
|
||||
@ -20,11 +20,11 @@ type Forwarder struct {
|
||||
proxy.Dialer
|
||||
url string
|
||||
addr string
|
||||
priority uint32
|
||||
maxFailures uint32 // maxfailures to set to Disabled
|
||||
disabled uint32
|
||||
failures uint32
|
||||
latency int64
|
||||
priority atomic.Uint32
|
||||
maxFailures atomic.Uint32 // maxfailures to set to Disabled
|
||||
disabled atomic.Uint32
|
||||
failures atomic.Uint32
|
||||
latency atomic.Int64
|
||||
intface string // local interface or ip address
|
||||
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.
|
||||
func (f *Forwarder) Failures() uint32 {
|
||||
return atomic.LoadUint32(&f.failures)
|
||||
return f.failures.Load()
|
||||
}
|
||||
|
||||
// IncFailures increase the failuer count by 1.
|
||||
func (f *Forwarder) IncFailures() {
|
||||
failures := atomic.AddUint32(&f.failures, 1)
|
||||
failures := f.failures.Add(1)
|
||||
if f.MaxFailures() == 0 {
|
||||
return
|
||||
}
|
||||
@ -149,17 +149,17 @@ func (f *Forwarder) AddHandler(h StatusHandler) {
|
||||
|
||||
// Enable the forwarder.
|
||||
func (f *Forwarder) Enable() {
|
||||
if atomic.CompareAndSwapUint32(&f.disabled, 1, 0) {
|
||||
if f.disabled.CompareAndSwap(1, 0) {
|
||||
for _, h := range f.handlers {
|
||||
h(f)
|
||||
}
|
||||
}
|
||||
atomic.StoreUint32(&f.failures, 0)
|
||||
f.failures.Store(0)
|
||||
}
|
||||
|
||||
// Disable the forwarder.
|
||||
func (f *Forwarder) Disable() {
|
||||
if atomic.CompareAndSwapUint32(&f.disabled, 0, 1) {
|
||||
if f.disabled.CompareAndSwap(0, 1) {
|
||||
for _, h := range f.handlers {
|
||||
h(f)
|
||||
}
|
||||
@ -168,7 +168,7 @@ func (f *Forwarder) Disable() {
|
||||
|
||||
// Enabled returns the status of forwarder.
|
||||
func (f *Forwarder) Enabled() bool {
|
||||
return !isTrue(atomic.LoadUint32(&f.disabled))
|
||||
return !isTrue(f.disabled.Load())
|
||||
}
|
||||
|
||||
func isTrue(n uint32) bool {
|
||||
@ -177,30 +177,30 @@ func isTrue(n uint32) bool {
|
||||
|
||||
// Priority returns the priority of forwarder.
|
||||
func (f *Forwarder) Priority() uint32 {
|
||||
return atomic.LoadUint32(&f.priority)
|
||||
return f.priority.Load()
|
||||
}
|
||||
|
||||
// SetPriority sets the priority of forwarder.
|
||||
func (f *Forwarder) SetPriority(l uint32) {
|
||||
atomic.StoreUint32(&f.priority, l)
|
||||
f.priority.Store(l)
|
||||
}
|
||||
|
||||
// MaxFailures returns the maxFailures of forwarder.
|
||||
func (f *Forwarder) MaxFailures() uint32 {
|
||||
return atomic.LoadUint32(&f.maxFailures)
|
||||
return f.maxFailures.Load()
|
||||
}
|
||||
|
||||
// SetMaxFailures sets the maxFailures of forwarder.
|
||||
func (f *Forwarder) SetMaxFailures(l uint32) {
|
||||
atomic.StoreUint32(&f.maxFailures, l)
|
||||
f.maxFailures.Store(l)
|
||||
}
|
||||
|
||||
// Latency returns the latency of forwarder.
|
||||
func (f *Forwarder) Latency() int64 {
|
||||
return atomic.LoadInt64(&f.latency)
|
||||
return f.latency.Load()
|
||||
}
|
||||
|
||||
// SetLatency sets the latency of forwarder.
|
||||
func (f *Forwarder) SetLatency(l int64) {
|
||||
atomic.StoreInt64(&f.latency, l)
|
||||
f.latency.Store(l)
|
||||
}
|
||||
|
||||
@ -23,8 +23,8 @@ type FwdrGroup struct {
|
||||
fwdrs []*Forwarder
|
||||
avail []*Forwarder // available forwarders
|
||||
mu sync.RWMutex
|
||||
index uint32
|
||||
priority uint32
|
||||
index atomic.Uint32
|
||||
priority atomic.Uint32
|
||||
next func(addr string) *Forwarder
|
||||
}
|
||||
|
||||
@ -123,17 +123,17 @@ func (p *FwdrGroup) NextDialer(dstAddr string) proxy.Dialer {
|
||||
defer p.mu.RUnlock()
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
// 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.
|
||||
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.
|
||||
func (p *FwdrGroup) init() {
|
||||
@ -287,7 +287,7 @@ func (p *FwdrGroup) setLatency(fwdr *Forwarder, elapsed time.Duration) {
|
||||
|
||||
// Round Robin.
|
||||
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.
|
||||
|
||||
Loading…
Reference in New Issue
Block a user