chore: restore function name to AliveForTestUrl

This commit is contained in:
Larvan2 2023-12-22 21:18:17 +08:00
parent 08a1f10af4
commit ac381736a5
6 changed files with 20 additions and 20 deletions

View File

@ -33,13 +33,13 @@ type internalProxyState struct {
type Proxy struct {
C.ProxyAdapter
history *queue.Queue[C.DelayHistory]
alive atomic.Bool
history *queue.Queue[C.DelayHistory]
extra *xsync.MapOf[string, *internalProxyState]
}
// Alive implements C.Proxy
func (p *Proxy) Alive(url string) bool {
// AliveForTestUrl implements C.Proxy
func (p *Proxy) AliveForTestUrl(url string) bool {
if state, ok := p.extra.Load(url); ok {
return state.alive.Load()
}
@ -127,7 +127,7 @@ func (p *Proxy) ExtraDelayHistories() map[string]C.ProxyState {
// LastDelayForTestUrl return last history record of the specified URL. if proxy is not alive, return the max value of uint16.
// implements C.Proxy
func (p *Proxy) LastDelayForTestUrl(url string) (delay uint16) {
var max uint16 = 0xffff
var maxDelay uint16 = 0xffff
alive := false
var history C.DelayHistory
@ -138,7 +138,7 @@ func (p *Proxy) LastDelayForTestUrl(url string) (delay uint16) {
}
if !alive || history.Delay == 0 {
return max
return maxDelay
}
return history.Delay
}

View File

@ -102,12 +102,12 @@ func (f *Fallback) findAliveProxy(touch bool) C.Proxy {
proxies := f.GetProxies(touch)
for _, proxy := range proxies {
if len(f.selected) == 0 {
if proxy.Alive(f.testUrl) {
if proxy.AliveForTestUrl(f.testUrl) {
return proxy
}
} else {
if proxy.Name() == f.selected {
if proxy.Alive(f.testUrl) {
if proxy.AliveForTestUrl(f.testUrl) {
return proxy
} else {
f.selected = ""
@ -133,7 +133,7 @@ func (f *Fallback) Set(name string) error {
}
f.selected = name
if !p.Alive(f.testUrl) {
if !p.AliveForTestUrl(f.testUrl) {
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*time.Duration(5000))
defer cancel()
expectedStatus, _ := utils.NewIntRanges[uint16](f.expectedStatus)

View File

@ -150,7 +150,7 @@ func strategyRoundRobin(url string) strategyFn {
for ; i < length; i++ {
id := (idx + i) % length
proxy := proxies[id]
if proxy.Alive(url) {
if proxy.AliveForTestUrl(url) {
i++
return proxy
}
@ -168,14 +168,14 @@ func strategyConsistentHashing(url string) strategyFn {
for i := 0; i < maxRetry; i, key = i+1, key+1 {
idx := jumpHash(key, buckets)
proxy := proxies[idx]
if proxy.Alive(url) {
if proxy.AliveForTestUrl(url) {
return proxy
}
}
// when availability is poor, traverse the entire list to get the available nodes
for _, proxy := range proxies {
if proxy.Alive(url) {
if proxy.AliveForTestUrl(url) {
return proxy
}
}
@ -201,7 +201,7 @@ func strategyStickySessions(url string) strategyFn {
nowIdx := idx
for i := 1; i < maxRetry; i++ {
proxy := proxies[nowIdx]
if proxy.Alive(url) {
if proxy.AliveForTestUrl(url) {
if nowIdx != idx {
lruCache.Delete(key)
lruCache.Set(key, nowIdx)

View File

@ -101,7 +101,7 @@ func (u *URLTest) fast(touch bool) C.Proxy {
proxies := u.GetProxies(touch)
if u.selected != "" {
for _, proxy := range proxies {
if !proxy.Alive(u.testUrl) {
if !proxy.AliveForTestUrl(u.testUrl) {
continue
}
if proxy.Name() == u.selected {
@ -113,7 +113,7 @@ func (u *URLTest) fast(touch bool) C.Proxy {
elm, _, shared := u.fastSingle.Do(func() (C.Proxy, error) {
fast := proxies[0]
min := fast.LastDelayForTestUrl(u.testUrl)
minDelay := fast.LastDelayForTestUrl(u.testUrl)
fastNotExist := true
for _, proxy := range proxies[1:] {
@ -121,19 +121,19 @@ func (u *URLTest) fast(touch bool) C.Proxy {
fastNotExist = false
}
if !proxy.Alive(u.testUrl) {
if !proxy.AliveForTestUrl(u.testUrl) {
continue
}
delay := proxy.LastDelayForTestUrl(u.testUrl)
if delay < min {
if delay < minDelay {
fast = proxy
min = delay
minDelay = delay
}
}
// tolerance
if u.fastNode == nil || fastNotExist || !u.fastNode.Alive(u.testUrl) || u.fastNode.LastDelayForTestUrl(u.testUrl) > fast.LastDelayForTestUrl(u.testUrl)+u.tolerance {
if u.fastNode == nil || fastNotExist || !u.fastNode.AliveForTestUrl(u.testUrl) || u.fastNode.LastDelayForTestUrl(u.testUrl) > fast.LastDelayForTestUrl(u.testUrl)+u.tolerance {
u.fastNode = fast
}
return u.fastNode, nil

View File

@ -202,7 +202,7 @@ func (hc *HealthCheck) execute(b *batch.Batch[bool], url, uid string, option *ex
defer cancel()
log.Debugln("Health Checking, proxy: %s, url: %s, id: {%s}", p.Name(), url, uid)
_, _ = p.URLTest(ctx, url, expectedStatus)
log.Debugln("Health Checked, proxy: %s, url: %s, alive: %t, delay: %d ms uid: {%s}", p.Name(), url, p.Alive(url), p.LastDelayForTestUrl(url), uid)
log.Debugln("Health Checked, proxy: %s, url: %s, alive: %t, delay: %d ms uid: {%s}", p.Name(), url, p.AliveForTestUrl(url), p.LastDelayForTestUrl(url), uid)
return false, nil
})
}

View File

@ -156,7 +156,7 @@ type DelayHistoryStoreType int
type Proxy interface {
ProxyAdapter
Alive(url string) bool
AliveForTestUrl(url string) bool
DelayHistory() []DelayHistory
ExtraDelayHistories() map[string]ProxyState
LastDelayForTestUrl(url string) uint16