From 4f69372656491de60fdbe5a85f5fdaf1e571c8a6 Mon Sep 17 00:00:00 2001 From: nadoo <287492+nadoo@users.noreply.github.com> Date: Fri, 24 Aug 2018 00:57:55 +0800 Subject: [PATCH] ha: change dialer when there's a forwarder enabled with higher priority --- strategy/ha.go | 6 +++--- strategy/lha.go | 2 +- strategy/rr.go | 24 ++++++++++++------------ 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/strategy/ha.go b/strategy/ha.go index ef55fc9..f66f32b 100644 --- a/strategy/ha.go +++ b/strategy/ha.go @@ -6,7 +6,7 @@ import ( "github.com/nadoo/glider/proxy" ) -// high availability forwarder +// high availability dialer type haDialer struct{ *rrDialer } // newHADialer . @@ -15,8 +15,8 @@ func newHADialer(dialers []*proxy.Forwarder, webhost string, duration int) proxy } func (ha *haDialer) nextDialer(dstAddr string) *proxy.Forwarder { - d := ha.fwdrs[ha.index] - if !d.Enabled() { + d := ha.fwdrs[ha.Index()] + if !d.Enabled() || d.Priority() < ha.Priority() { d = ha.nextDialer(dstAddr) } return d diff --git a/strategy/lha.go b/strategy/lha.go index a45e62d..c761865 100644 --- a/strategy/lha.go +++ b/strategy/lha.go @@ -6,7 +6,7 @@ import ( "github.com/nadoo/glider/proxy" ) -// latency based high availability forwarder +// latency based high availability dialer type lhaDialer struct{ *rrDialer } // newLHADialer . diff --git a/strategy/rr.go b/strategy/rr.go index a2745bd..34713d2 100644 --- a/strategy/rr.go +++ b/strategy/rr.go @@ -95,6 +95,18 @@ func (rr *rrDialer) nextDialer(dstAddr string) *proxy.Forwarder { return rr.fwdrs[idx] } +// Index returns the active forwarder's Index of rrDialer +func (rr *rrDialer) Index() int32 { return atomic.LoadInt32(&rr.index) } + +// SetIndex sets the active forwarder's Index of rrDialer +func (rr *rrDialer) SetIndex(p int32) { atomic.StoreInt32(&rr.index, p) } + +// Priority returns the active priority of rrDialer +func (rr *rrDialer) Priority() uint32 { return atomic.LoadUint32(&rr.priority) } + +// SetPriority sets the active priority of rrDialer +func (rr *rrDialer) SetPriority(p uint32) { atomic.StoreUint32(&rr.priority, p) } + // Check implements the Checker interface func (rr *rrDialer) Check() { for i := 0; i < len(rr.fwdrs); i++ { @@ -147,15 +159,3 @@ func (rr *rrDialer) check(i int) { rc.Close() } } - -// Index returns the active forwarder's Index of rrDialer -func (rr *rrDialer) Index() int32 { return atomic.LoadInt32(&rr.index) } - -// SetIndex sets the active forwarder's Index of rrDialer -func (rr *rrDialer) SetIndex(p int32) { atomic.StoreInt32(&rr.index, p) } - -// Priority returns the active priority of rrDialer -func (rr *rrDialer) Priority() uint32 { return atomic.LoadUint32(&rr.priority) } - -// SetPriority sets the active priority of rrDialer -func (rr *rrDialer) SetPriority(p uint32) { atomic.StoreUint32(&rr.priority, p) }