Fix: limit concurrency number of provider health check

This commit is contained in:
Dreamacro
2021-07-21 17:01:02 +08:00
parent 53e17a916b
commit 8d37220566
4 changed files with 203 additions and 11 deletions

View File

@ -136,6 +136,8 @@ func (p *Proxy) URLTest(ctx context.Context, url string) (t uint16, err error) {
return http.ErrUseLastResponse
},
}
defer client.CloseIdleConnections()
resp, err := client.Do(req)
if err != nil {
return

View File

@ -2,9 +2,9 @@ package provider
import (
"context"
"sync"
"time"
"github.com/Dreamacro/clash/common/batch"
C "github.com/Dreamacro/clash/constant"
"go.uber.org/atomic"
@ -60,19 +60,16 @@ func (hc *HealthCheck) touch() {
func (hc *HealthCheck) check() {
ctx, cancel := context.WithTimeout(context.Background(), defaultURLTestTimeout)
wg := &sync.WaitGroup{}
defer cancel()
b, ctx := batch.WithContext(ctx, batch.WithConcurrencyNum(10))
for _, proxy := range hc.proxies {
wg.Add(1)
go func(p C.Proxy) {
p.URLTest(ctx, hc.url)
wg.Done()
}(proxy)
p := proxy
b.Go(p.Name(), func() (interface{}, error) {
return p.URLTest(ctx, hc.url)
})
}
wg.Wait()
cancel()
b.Wait()
}
func (hc *HealthCheck) close() {