Feature: add delay history and improve url-test behavior

This commit is contained in:
Dreamacro
2019-03-17 14:52:39 +08:00
parent 63446da5fa
commit 7a9d986ff3
5 changed files with 151 additions and 7 deletions

View File

@ -38,7 +38,7 @@ func (u *URLTest) Now() string {
func (u *URLTest) Dial(metadata *C.Metadata) (net.Conn, error) {
a, err := u.fast.Dial(metadata)
if err != nil {
go u.speedTest()
u.fallback()
}
return a, err
}
@ -74,6 +74,23 @@ Loop:
}
}
func (u *URLTest) fallback() {
fast := u.proxies[0]
min := fast.LastDelay()
for _, proxy := range u.proxies[1:] {
if !proxy.Alive() {
continue
}
delay := proxy.LastDelay()
if delay < min {
fast = proxy
min = delay
}
}
u.fast = fast
}
func (u *URLTest) speedTest() {
if atomic.AddInt32(&u.once, 1) != 1 {
return