diff --git a/adapter/provider/healthcheck.go b/adapter/provider/healthcheck.go index 7a89ffa..8f66a95 100644 --- a/adapter/provider/healthcheck.go +++ b/adapter/provider/healthcheck.go @@ -7,6 +7,7 @@ import ( "github.com/Dreamacro/clash/common/batch" C "github.com/Dreamacro/clash/constant" + "github.com/samber/lo" "go.uber.org/atomic" ) @@ -31,13 +32,20 @@ type HealthCheck struct { func (hc *HealthCheck) process() { ticker := time.NewTicker(time.Duration(hc.interval) * time.Second) - go hc.check() + go hc.checkAll() for { select { case <-ticker.C: now := time.Now().Unix() 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: ticker.Stop() @@ -58,9 +66,13 @@ func (hc *HealthCheck) touch() { 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)) - for _, proxy := range hc.proxies { + for _, proxy := range proxies { p := proxy b.Go(p.Name(), func() (any, error) { ctx, cancel := context.WithTimeout(context.Background(), defaultURLTestTimeout) diff --git a/adapter/provider/provider.go b/adapter/provider/provider.go index 72dcab1..03fd697 100644 --- a/adapter/provider/provider.go +++ b/adapter/provider/provider.go @@ -54,7 +54,7 @@ func (pp *proxySetProvider) Name() string { } func (pp *proxySetProvider) HealthCheck() { - pp.healthCheck.check() + pp.healthCheck.checkAll() } func (pp *proxySetProvider) Update() error { @@ -91,7 +91,7 @@ func (pp *proxySetProvider) setProxies(proxies []C.Proxy) { pp.proxies = proxies pp.healthCheck.setProxy(proxies) 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() { - cp.healthCheck.check() + cp.healthCheck.checkAll() } func (cp *compatibleProvider) Update() error {