2019-12-08 12:17:24 +08:00
|
|
|
package provider
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2021-07-21 17:01:02 +08:00
|
|
|
"github.com/Dreamacro/clash/common/batch"
|
2019-12-08 12:17:24 +08:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2020-11-19 00:53:22 +08:00
|
|
|
|
|
|
|
"go.uber.org/atomic"
|
2019-12-08 12:17:24 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultURLTestTimeout = time.Second * 5
|
|
|
|
)
|
|
|
|
|
|
|
|
type HealthCheckOption struct {
|
|
|
|
URL string
|
|
|
|
Interval uint
|
|
|
|
}
|
|
|
|
|
2019-12-26 18:41:06 +08:00
|
|
|
type HealthCheck struct {
|
2020-11-19 00:53:22 +08:00
|
|
|
url string
|
|
|
|
proxies []C.Proxy
|
|
|
|
interval uint
|
|
|
|
lazy bool
|
|
|
|
lastTouch *atomic.Int64
|
|
|
|
done chan struct{}
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
|
2019-12-26 18:41:06 +08:00
|
|
|
func (hc *HealthCheck) process() {
|
|
|
|
ticker := time.NewTicker(time.Duration(hc.interval) * time.Second)
|
|
|
|
|
2019-12-08 12:17:24 +08:00
|
|
|
go hc.check()
|
2019-12-26 18:41:06 +08:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
2020-11-19 00:53:22 +08:00
|
|
|
now := time.Now().Unix()
|
|
|
|
if !hc.lazy || now-hc.lastTouch.Load() < int64(hc.interval) {
|
|
|
|
hc.check()
|
|
|
|
}
|
2019-12-26 18:41:06 +08:00
|
|
|
case <-hc.done:
|
|
|
|
ticker.Stop()
|
|
|
|
return
|
|
|
|
}
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-26 18:41:06 +08:00
|
|
|
func (hc *HealthCheck) setProxy(proxies []C.Proxy) {
|
|
|
|
hc.proxies = proxies
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hc *HealthCheck) auto() bool {
|
|
|
|
return hc.interval != 0
|
|
|
|
}
|
|
|
|
|
2020-11-19 00:53:22 +08:00
|
|
|
func (hc *HealthCheck) touch() {
|
|
|
|
hc.lastTouch.Store(time.Now().Unix())
|
|
|
|
}
|
|
|
|
|
2019-12-26 18:41:06 +08:00
|
|
|
func (hc *HealthCheck) check() {
|
2021-07-23 00:30:23 +08:00
|
|
|
b, _ := batch.New(context.Background(), batch.WithConcurrencyNum(10))
|
2019-12-08 12:17:24 +08:00
|
|
|
for _, proxy := range hc.proxies {
|
2021-07-21 17:01:02 +08:00
|
|
|
p := proxy
|
2022-03-16 12:10:13 +08:00
|
|
|
b.Go(p.Name(), func() (any, error) {
|
2021-07-21 23:53:31 +08:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), defaultURLTestTimeout)
|
|
|
|
defer cancel()
|
|
|
|
p.URLTest(ctx, hc.url)
|
|
|
|
return nil, nil
|
2021-07-21 17:01:02 +08:00
|
|
|
})
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
2021-07-21 17:01:02 +08:00
|
|
|
b.Wait()
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
|
2019-12-26 18:41:06 +08:00
|
|
|
func (hc *HealthCheck) close() {
|
|
|
|
hc.done <- struct{}{}
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
|
2020-11-19 00:53:22 +08:00
|
|
|
func NewHealthCheck(proxies []C.Proxy, url string, interval uint, lazy bool) *HealthCheck {
|
2019-12-26 18:41:06 +08:00
|
|
|
return &HealthCheck{
|
2020-11-19 00:53:22 +08:00
|
|
|
proxies: proxies,
|
|
|
|
url: url,
|
|
|
|
interval: interval,
|
|
|
|
lazy: lazy,
|
|
|
|
lastTouch: atomic.NewInt64(0),
|
|
|
|
done: make(chan struct{}, 1),
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
}
|