chore: healthcheck only once check at same time

This commit is contained in:
gVisor bot 2022-06-25 08:53:04 +08:00
parent 695e3e2175
commit e107b18954
2 changed files with 20 additions and 13 deletions

View file

@ -111,11 +111,11 @@ func (gb *GroupBase) URLTest(ctx context.Context, url string) (map[string]uint16
wg.Add(1) wg.Add(1)
go func() { go func() {
delay, err := proxy.URLTest(ctx, url) delay, err := proxy.URLTest(ctx, url)
lock.Lock()
if err == nil { if err == nil {
lock.Lock()
mp[proxy.Name()] = delay mp[proxy.Name()] = delay
lock.Unlock()
} }
lock.Unlock()
wg.Done() wg.Done()
}() }()

View file

@ -2,6 +2,7 @@ package provider
import ( import (
"context" "context"
"github.com/Dreamacro/clash/common/singledo"
"time" "time"
"github.com/Dreamacro/clash/common/batch" "github.com/Dreamacro/clash/common/batch"
@ -26,6 +27,7 @@ type HealthCheck struct {
lazy bool lazy bool
lastTouch *atomic.Int64 lastTouch *atomic.Int64
done chan struct{} done chan struct{}
singleDo *singledo.Single[struct{}]
} }
func (hc *HealthCheck) process() { func (hc *HealthCheck) process() {
@ -63,17 +65,21 @@ func (hc *HealthCheck) touch() {
} }
func (hc *HealthCheck) check() { func (hc *HealthCheck) check() {
b, _ := batch.New[bool](context.Background(), batch.WithConcurrencyNum[bool](10)) _, _, _ = hc.singleDo.Do(func() (struct{}, error) {
for _, proxy := range hc.proxies { b, _ := batch.New[bool](context.Background(), batch.WithConcurrencyNum[bool](10))
p := proxy for _, proxy := range hc.proxies {
b.Go(p.Name(), func() (bool, error) { p := proxy
ctx, cancel := context.WithTimeout(context.Background(), defaultURLTestTimeout) b.Go(p.Name(), func() (bool, error) {
defer cancel() ctx, cancel := context.WithTimeout(context.Background(), defaultURLTestTimeout)
_, _ = p.URLTest(ctx, hc.url) defer cancel()
return false, nil _, _ = p.URLTest(ctx, hc.url)
}) return false, nil
} })
b.Wait() }
b.Wait()
return struct{}{}, nil
})
} }
func (hc *HealthCheck) close() { func (hc *HealthCheck) close() {
@ -88,5 +94,6 @@ func NewHealthCheck(proxies []C.Proxy, url string, interval uint, lazy bool) *He
lazy: lazy, lazy: lazy,
lastTouch: atomic.NewInt64(0), lastTouch: atomic.NewInt64(0),
done: make(chan struct{}, 1), done: make(chan struct{}, 1),
singleDo: singledo.NewSingle[struct{}](time.Second),
} }
} }