1
0

Fix: proxy health check should check not alive proxy on lazy

This commit is contained in:
Dreamacro 2023-06-18 18:27:47 +08:00
parent 154cb1d1f0
commit 700ceed194
2 changed files with 19 additions and 7 deletions

View File

@ -7,6 +7,7 @@ import (
"github.com/Dreamacro/clash/common/batch" "github.com/Dreamacro/clash/common/batch"
C "github.com/Dreamacro/clash/constant" C "github.com/Dreamacro/clash/constant"
"github.com/samber/lo"
"go.uber.org/atomic" "go.uber.org/atomic"
) )
@ -31,13 +32,20 @@ type HealthCheck struct {
func (hc *HealthCheck) process() { func (hc *HealthCheck) process() {
ticker := time.NewTicker(time.Duration(hc.interval) * time.Second) ticker := time.NewTicker(time.Duration(hc.interval) * time.Second)
go hc.check() go hc.checkAll()
for { for {
select { select {
case <-ticker.C: case <-ticker.C:
now := time.Now().Unix() now := time.Now().Unix()
if !hc.lazy || now-hc.lastTouch.Load() < int64(hc.interval) { if !hc.lazy || now-hc.lastTouch.Load() < int64(hc.interval) {
hc.check() hc.checkAll()
} else { // lazy but still need to check not alive proxies
notAliveProxies := lo.Filter(hc.proxies, func(proxy C.Proxy, _ int) bool {
return !proxy.Alive()
})
if len(notAliveProxies) != 0 {
hc.check(notAliveProxies)
}
} }
case <-hc.done: case <-hc.done:
ticker.Stop() ticker.Stop()
@ -58,9 +66,13 @@ func (hc *HealthCheck) touch() {
hc.lastTouch.Store(time.Now().Unix()) hc.lastTouch.Store(time.Now().Unix())
} }
func (hc *HealthCheck) check() { func (hc *HealthCheck) checkAll() {
hc.check(hc.proxies)
}
func (hc *HealthCheck) check(proxies []C.Proxy) {
b, _ := batch.New(context.Background(), batch.WithConcurrencyNum(10)) b, _ := batch.New(context.Background(), batch.WithConcurrencyNum(10))
for _, proxy := range hc.proxies { for _, proxy := range proxies {
p := proxy p := proxy
b.Go(p.Name(), func() (any, error) { b.Go(p.Name(), func() (any, error) {
ctx, cancel := context.WithTimeout(context.Background(), defaultURLTestTimeout) ctx, cancel := context.WithTimeout(context.Background(), defaultURLTestTimeout)

View File

@ -54,7 +54,7 @@ func (pp *proxySetProvider) Name() string {
} }
func (pp *proxySetProvider) HealthCheck() { func (pp *proxySetProvider) HealthCheck() {
pp.healthCheck.check() pp.healthCheck.checkAll()
} }
func (pp *proxySetProvider) Update() error { func (pp *proxySetProvider) Update() error {
@ -91,7 +91,7 @@ func (pp *proxySetProvider) setProxies(proxies []C.Proxy) {
pp.proxies = proxies pp.proxies = proxies
pp.healthCheck.setProxy(proxies) pp.healthCheck.setProxy(proxies)
if pp.healthCheck.auto() { if pp.healthCheck.auto() {
go pp.healthCheck.check() go pp.healthCheck.checkAll()
} }
} }
@ -186,7 +186,7 @@ func (cp *compatibleProvider) Name() string {
} }
func (cp *compatibleProvider) HealthCheck() { func (cp *compatibleProvider) HealthCheck() {
cp.healthCheck.check() cp.healthCheck.checkAll()
} }
func (cp *compatibleProvider) Update() error { func (cp *compatibleProvider) Update() error {