Files

49 lines
1.3 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package service
import "time"
// ResolveProxyFallbackTarget 计算一个过期代理 start 应把账号改投到哪里。
// 返回 (targetID, change):
// - change=false:不改动账号(mode=none,或链路成环/无解的兜底)
// - change=true, targetID=nil:改投为直连
// - change=true, targetID!=nil:改投到该备用代理 id
//
// byID 是「全部代理」的快照(id -> Proxy),now 为判定基准时间。
func ResolveProxyFallbackTarget(start Proxy, byID map[int64]Proxy, now time.Time) (*int64, bool) {
switch start.FallbackMode {
case FallbackModeDirect:
return nil, true
case FallbackModeProxy:
visited := map[int64]struct{}{start.ID: {}}
curID := start.BackupProxyID
for {
if curID == nil {
return nil, false
}
if _, seen := visited[*curID]; seen {
return nil, false
}
p, ok := byID[*curID]
if !ok {
return nil, false
}
// Disabled nodes may define a fallback, but cannot be selected as targets.
if p.IsActive() && !p.IsExpired(now) {
id := p.ID
return &id, true
}
visited[*curID] = struct{}{}
switch p.FallbackMode {
case FallbackModeDirect:
return nil, true
case FallbackModeProxy:
curID = p.BackupProxyID
default:
return nil, false
}
}
default:
return nil, false
}
}