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

View File

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

View File

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

View File

@ -101,7 +101,7 @@ func (u *URLTest) fast(touch bool) C.Proxy {
proxies := u.GetProxies(touch) proxies := u.GetProxies(touch)
if u.selected != "" { if u.selected != "" {
for _, proxy := range proxies { for _, proxy := range proxies {
if !proxy.Alive(u.testUrl) { if !proxy.AliveForTestUrl(u.testUrl) {
continue continue
} }
if proxy.Name() == u.selected { 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) { elm, _, shared := u.fastSingle.Do(func() (C.Proxy, error) {
fast := proxies[0] fast := proxies[0]
min := fast.LastDelayForTestUrl(u.testUrl) minDelay := fast.LastDelayForTestUrl(u.testUrl)
fastNotExist := true fastNotExist := true
for _, proxy := range proxies[1:] { for _, proxy := range proxies[1:] {
@ -121,19 +121,19 @@ func (u *URLTest) fast(touch bool) C.Proxy {
fastNotExist = false fastNotExist = false
} }
if !proxy.Alive(u.testUrl) { if !proxy.AliveForTestUrl(u.testUrl) {
continue continue
} }
delay := proxy.LastDelayForTestUrl(u.testUrl) delay := proxy.LastDelayForTestUrl(u.testUrl)
if delay < min { if delay < minDelay {
fast = proxy fast = proxy
min = delay minDelay = delay
} }
} }
// tolerance // 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 u.fastNode = fast
} }
return u.fastNode, nil 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() defer cancel()
log.Debugln("Health Checking, proxy: %s, url: %s, id: {%s}", p.Name(), url, uid) log.Debugln("Health Checking, proxy: %s, url: %s, id: {%s}", p.Name(), url, uid)
_, _ = p.URLTest(ctx, url, expectedStatus) _, _ = 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 return false, nil
}) })
} }

View File

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