mirror of
https://github.com/nadoo/glider.git
synced 2025-02-24 01:45:39 +08:00
use sync.Map in status
This commit is contained in:
parent
8f9f10d4ee
commit
ae7c62efe5
21
strategy.go
21
strategy.go
@ -6,6 +6,7 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewStrategyDialer returns a new Strategy Dialer
|
// NewStrategyDialer returns a new Strategy Dialer
|
||||||
@ -37,7 +38,7 @@ type rrDialer struct {
|
|||||||
dialers []Dialer
|
dialers []Dialer
|
||||||
idx int
|
idx int
|
||||||
|
|
||||||
status map[int]bool
|
status sync.Map
|
||||||
|
|
||||||
// for checking
|
// for checking
|
||||||
website string
|
website string
|
||||||
@ -48,12 +49,11 @@ type rrDialer struct {
|
|||||||
func newRRDialer(dialers []Dialer, website string, duration int) *rrDialer {
|
func newRRDialer(dialers []Dialer, website string, duration int) *rrDialer {
|
||||||
rr := &rrDialer{dialers: dialers}
|
rr := &rrDialer{dialers: dialers}
|
||||||
|
|
||||||
rr.status = make(map[int]bool)
|
|
||||||
rr.website = website
|
rr.website = website
|
||||||
rr.duration = duration
|
rr.duration = duration
|
||||||
|
|
||||||
for k := range dialers {
|
for k := range dialers {
|
||||||
rr.status[k] = true
|
rr.status.Store(k,true)
|
||||||
go rr.checkDialer(k)
|
go rr.checkDialer(k)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,7 +74,8 @@ func (rr *rrDialer) NextDialer(dstAddr string) Dialer {
|
|||||||
found := false
|
found := false
|
||||||
for i := 0; i < n; i++ {
|
for i := 0; i < n; i++ {
|
||||||
rr.idx = (rr.idx + 1) % n
|
rr.idx = (rr.idx + 1) % n
|
||||||
if rr.status[rr.idx] {
|
result, ok := rr.status.Load(rr.idx)
|
||||||
|
if (ok && result.(bool)) {
|
||||||
found = true
|
found = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -109,7 +110,7 @@ func (rr *rrDialer) checkDialer(idx int) {
|
|||||||
startTime := time.Now()
|
startTime := time.Now()
|
||||||
c, err := d.Dial("tcp", rr.website)
|
c, err := d.Dial("tcp", rr.website)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
rr.status[idx] = false
|
rr.status.Store(idx, false)
|
||||||
logf("proxy-check %s -> %s, set to DISABLED. error in dial: %s", d.Addr(), rr.website, err)
|
logf("proxy-check %s -> %s, set to DISABLED. error in dial: %s", d.Addr(), rr.website, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -119,15 +120,15 @@ func (rr *rrDialer) checkDialer(idx int) {
|
|||||||
|
|
||||||
_, err = io.ReadFull(c, buf)
|
_, err = io.ReadFull(c, buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
rr.status[idx] = false
|
rr.status.Store(idx, false)
|
||||||
logf("proxy-check %s -> %s, set to DISABLED. error in read: %s", d.Addr(), rr.website, err)
|
logf("proxy-check %s -> %s, set to DISABLED. error in read: %s", d.Addr(), rr.website, err)
|
||||||
} else if bytes.Equal([]byte("HTTP"), buf) {
|
} else if bytes.Equal([]byte("HTTP"), buf) {
|
||||||
rr.status[idx] = true
|
rr.status.Store(idx, true)
|
||||||
retry = 2
|
retry = 2
|
||||||
dialTime := time.Since(startTime)
|
dialTime := time.Since(startTime)
|
||||||
logf("proxy-check %s -> %s, set to ENABLED. connect time: %s", d.Addr(), rr.website, dialTime.String())
|
logf("proxy-check %s -> %s, set to ENABLED. connect time: %s", d.Addr(), rr.website, dialTime.String())
|
||||||
} else {
|
} else {
|
||||||
rr.status[idx] = false
|
rr.status.Store(idx, false)
|
||||||
logf("proxy-check %s -> %s, set to DISABLED. server response: %s", d.Addr(), rr.website, buf)
|
logf("proxy-check %s -> %s, set to DISABLED. server response: %s", d.Addr(), rr.website, buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -148,7 +149,9 @@ func newHADialer(dialers []Dialer, webhost string, duration int) Dialer {
|
|||||||
func (ha *haDialer) Dial(network, addr string) (net.Conn, error) {
|
func (ha *haDialer) Dial(network, addr string) (net.Conn, error) {
|
||||||
d := ha.dialers[ha.idx]
|
d := ha.dialers[ha.idx]
|
||||||
|
|
||||||
if !ha.status[ha.idx] {
|
result, ok := ha.status.Load(ha.idx)
|
||||||
|
|
||||||
|
if (ok && !result.(bool)) {
|
||||||
d = ha.NextDialer(addr)
|
d = ha.NextDialer(addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user