chore: 选择fallback时,当节点不可用时触发urltest
This commit is contained in:
parent
b0e723f868
commit
23cfe20eab
2 changed files with 21 additions and 6 deletions
|
@ -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,17 +92,29 @@ 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
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
return &Fallback{
|
||||
GroupBase: NewGroupBase(GroupBaseOption{
|
||||
|
@ -114,5 +128,6 @@ func NewFallback(option *GroupCommonOption, providers []provider.ProxyProvider)
|
|||
providers,
|
||||
}),
|
||||
disableUDP: option.DisableUDP,
|
||||
testUrl: option.URL,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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())
|
||||
|
|
Loading…
Reference in a new issue