Fix: proxy health check should check not alive proxy on lazy
This commit is contained in:
@ -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)
|
||||
|
Reference in New Issue
Block a user