From 1025101954133aca16753f020d2b3d0e0ff70d84 Mon Sep 17 00:00:00 2001 From: xishang0128 Date: Wed, 24 Jan 2024 12:45:35 +0800 Subject: [PATCH] chore: add `timeout` option --- adapter/outboundgroup/parser.go | 3 ++- adapter/provider/healthcheck.go | 13 +++++++------ adapter/provider/parser.go | 3 ++- config/config.go | 2 +- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/adapter/outboundgroup/parser.go b/adapter/outboundgroup/parser.go index 6f44b8ea..959041dd 100644 --- a/adapter/outboundgroup/parser.go +++ b/adapter/outboundgroup/parser.go @@ -28,6 +28,7 @@ type GroupCommonOption struct { Use []string `group:"use,omitempty"` URL string `group:"url,omitempty"` Interval int `group:"interval,omitempty"` + TestTimeout int `group:"timeout,omitempty"` Lazy bool `group:"lazy,omitempty"` DisableUDP bool `group:"disable-udp,omitempty"` Filter string `group:"filter,omitempty"` @@ -116,7 +117,7 @@ func ParseProxyGroup(config map[string]any, proxyMap map[string]C.Proxy, provide } } - hc := provider.NewHealthCheck(ps, testUrl, uint(groupOption.Interval), groupOption.Lazy, expectedStatus) + hc := provider.NewHealthCheck(ps, testUrl, uint(groupOption.TestTimeout), uint(groupOption.Interval), groupOption.Lazy, expectedStatus) pd, err := provider.NewCompatibleProvider(groupName, ps, hc) if err != nil { diff --git a/adapter/provider/healthcheck.go b/adapter/provider/healthcheck.go index 12e1df42..fbc1d14f 100644 --- a/adapter/provider/healthcheck.go +++ b/adapter/provider/healthcheck.go @@ -16,10 +16,6 @@ import ( "github.com/dlclark/regexp2" ) -const ( - defaultURLTestTimeout = time.Second * 5 -) - type HealthCheckOption struct { URL string Interval uint @@ -42,6 +38,7 @@ type HealthCheck struct { lastTouch atomic.TypedValue[time.Time] done chan struct{} singleDo *singledo.Single[struct{}] + timeout time.Duration } func (hc *HealthCheck) process() { @@ -198,7 +195,7 @@ func (hc *HealthCheck) execute(b *batch.Batch[bool], url, uid string, option *ex p := proxy b.Go(p.Name(), func() (bool, error) { - ctx, cancel := context.WithTimeout(context.Background(), defaultURLTestTimeout) + ctx, cancel := context.WithTimeout(context.Background(), hc.timeout) defer cancel() log.Debugln("Health Checking, proxy: %s, url: %s, id: {%s}", p.Name(), url, uid) _, _ = p.URLTest(ctx, url, expectedStatus) @@ -212,15 +209,19 @@ func (hc *HealthCheck) close() { hc.done <- struct{}{} } -func NewHealthCheck(proxies []C.Proxy, url string, interval uint, lazy bool, expectedStatus utils.IntRanges[uint16]) *HealthCheck { +func NewHealthCheck(proxies []C.Proxy, url string, timeout uint, interval uint, lazy bool, expectedStatus utils.IntRanges[uint16]) *HealthCheck { if url == "" { // expectedStatus = nil url = C.DefaultTestURL } + if timeout == 0 { + timeout = 5000 + } return &HealthCheck{ proxies: proxies, url: url, + timeout: time.Duration(timeout) * time.Millisecond, extra: map[string]*extraOption{}, interval: time.Duration(interval) * time.Second, lazy: lazy, diff --git a/adapter/provider/parser.go b/adapter/provider/parser.go index 9956dc8c..88b36b7c 100644 --- a/adapter/provider/parser.go +++ b/adapter/provider/parser.go @@ -22,6 +22,7 @@ type healthCheckSchema struct { Enable bool `provider:"enable"` URL string `provider:"url"` Interval int `provider:"interval"` + TestTimeout int `provider:"timeout,omitempty"` Lazy bool `provider:"lazy,omitempty"` ExpectedStatus string `provider:"expected-status,omitempty"` } @@ -75,7 +76,7 @@ func ParseProxyProvider(name string, mapping map[string]any) (types.ProxyProvide } hcInterval = uint(schema.HealthCheck.Interval) } - hc := NewHealthCheck([]C.Proxy{}, schema.HealthCheck.URL, hcInterval, schema.HealthCheck.Lazy, expectedStatus) + hc := NewHealthCheck([]C.Proxy{}, schema.HealthCheck.URL, uint(schema.HealthCheck.TestTimeout), hcInterval, schema.HealthCheck.Lazy, expectedStatus) var vehicle types.Vehicle switch schema.Type { diff --git a/config/config.go b/config/config.go index dce61f63..b0401596 100644 --- a/config/config.go +++ b/config/config.go @@ -775,7 +775,7 @@ func parseProxies(cfg *RawConfig) (proxies map[string]C.Proxy, providersMap map[ } ps = append(ps, proxies[v]) } - hc := provider.NewHealthCheck(ps, "", 0, true, nil) + hc := provider.NewHealthCheck(ps, "", 5000, 0, true, nil) pd, _ := provider.NewCompatibleProvider(provider.ReservedName, ps, hc) providersMap[provider.ReservedName] = pd