From f748e0405c849dee72cc6f726b90026a6dbcc57d Mon Sep 17 00:00:00 2001 From: gVisor bot Date: Wed, 24 Oct 2018 17:06:08 +0800 Subject: [PATCH] Improve: url-test will automatically speed test when the connection fails --- adapters/outbound/urltest.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/adapters/outbound/urltest.go b/adapters/outbound/urltest.go index df2be060..74402e48 100644 --- a/adapters/outbound/urltest.go +++ b/adapters/outbound/urltest.go @@ -3,6 +3,7 @@ package adapters import ( "errors" "sync" + "sync/atomic" "time" C "github.com/Dreamacro/clash/constant" @@ -15,6 +16,7 @@ type URLTest struct { fast C.Proxy interval time.Duration done chan struct{} + once int32 } type URLTestOption struct { @@ -37,7 +39,11 @@ func (u *URLTest) Now() string { } func (u *URLTest) Generator(metadata *C.Metadata) (adapter C.ProxyAdapter, err error) { - return u.fast.Generator(metadata) + a, err := u.fast.Generator(metadata) + if err != nil { + go u.speedTest() + } + return a, err } func (u *URLTest) Close() { @@ -59,6 +65,11 @@ Loop: } func (u *URLTest) speedTest() { + if atomic.AddInt32(&u.once, 1) != 1 { + return + } + defer atomic.StoreInt32(&u.once, 0) + wg := sync.WaitGroup{} wg.Add(len(u.proxies)) c := make(chan interface{}) @@ -108,6 +119,7 @@ func NewURLTest(option URLTestOption, proxies []C.Proxy) (*URLTest, error) { fast: proxies[0], interval: interval, done: make(chan struct{}), + once: 0, } go urlTest.loop() return urlTest, nil