Fix: proxy health check should check not alive proxy on lazy
This commit is contained in:
parent
154cb1d1f0
commit
700ceed194
@ -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)
|
||||
|
@ -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 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user