chore: 选择fallback时,当节点不可用时触发urltest

This commit is contained in:
gVisor bot 2022-06-03 13:31:56 +08:00
parent b0e723f868
commit 23cfe20eab
2 changed files with 21 additions and 6 deletions

View File

@ -8,11 +8,13 @@ import (
"github.com/Dreamacro/clash/component/dialer"
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/constant/provider"
"time"
)
type Fallback struct {
*GroupBase
disableUDP bool
testUrl string
selected string
}
@ -90,15 +92,27 @@ func (f *Fallback) findAliveProxy(touch bool) C.Proxy {
return al
}
func (f *Fallback) Set(name string) error {
func (f *Fallback) Set(name string) (err error) {
var p C.Proxy
for _, proxy := range f.GetProxies(false) {
if proxy.Name() == name {
f.selected = name
return nil
p = proxy
break
}
}
return errors.New("proxy not exist")
if p == nil {
return errors.New("proxy not exist")
}
f.selected = name
if !p.Alive() {
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*time.Duration(5000))
defer cancel()
_, _ = p.URLTest(ctx, f.testUrl)
}
return nil
}
func NewFallback(option *GroupCommonOption, providers []provider.ProxyProvider) *Fallback {
@ -114,5 +128,6 @@ func NewFallback(option *GroupCommonOption, providers []provider.ProxyProvider)
providers,
}),
disableUDP: option.DisableUDP,
testUrl: option.URL,
}
}

View File

@ -146,14 +146,14 @@ func (gb *GroupBase) onDialFailed() {
gb.failedTimes++
if gb.failedTimes == 1 {
log.Warnln("ProxyGroup: %s first failed", gb.Name())
log.Debugln("ProxyGroup: %s first failed", gb.Name())
gb.failedTime = time.Now()
} else {
if time.Since(gb.failedTime) > gb.failedTimeoutInterval() {
return
}
log.Warnln("ProxyGroup: %s failed count: %d", gb.Name(), gb.failedTimes)
log.Debugln("ProxyGroup: %s failed count: %d", gb.Name(), gb.failedTimes)
if gb.failedTimes >= gb.maxFailedTimes() {
gb.failedTesting.Store(true)
log.Warnln("because %s failed multiple times, active health check", gb.Name())