Improve: url-test will automatically speed test when the connection fails

This commit is contained in:
gVisor bot 2018-10-24 17:06:08 +08:00
parent 2fb13706c8
commit f748e0405c

View file

@ -3,6 +3,7 @@ package adapters
import ( import (
"errors" "errors"
"sync" "sync"
"sync/atomic"
"time" "time"
C "github.com/Dreamacro/clash/constant" C "github.com/Dreamacro/clash/constant"
@ -15,6 +16,7 @@ type URLTest struct {
fast C.Proxy fast C.Proxy
interval time.Duration interval time.Duration
done chan struct{} done chan struct{}
once int32
} }
type URLTestOption struct { type URLTestOption struct {
@ -37,7 +39,11 @@ func (u *URLTest) Now() string {
} }
func (u *URLTest) Generator(metadata *C.Metadata) (adapter C.ProxyAdapter, err error) { 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() { func (u *URLTest) Close() {
@ -59,6 +65,11 @@ Loop:
} }
func (u *URLTest) speedTest() { func (u *URLTest) speedTest() {
if atomic.AddInt32(&u.once, 1) != 1 {
return
}
defer atomic.StoreInt32(&u.once, 0)
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
wg.Add(len(u.proxies)) wg.Add(len(u.proxies))
c := make(chan interface{}) c := make(chan interface{})
@ -108,6 +119,7 @@ func NewURLTest(option URLTestOption, proxies []C.Proxy) (*URLTest, error) {
fast: proxies[0], fast: proxies[0],
interval: interval, interval: interval,
done: make(chan struct{}), done: make(chan struct{}),
once: 0,
} }
go urlTest.loop() go urlTest.loop()
return urlTest, nil return urlTest, nil