Sub2API v1.0 - AI API 网关(二开初始版本,基于上游 Wei-Shaw/sub2api)
Release / update-version (push) Has been cancelled
Release / build-frontend (push) Has been cancelled
Release / release (push) Has been cancelled
Release / sync-version-file (push) Has been cancelled
CI / shell (push) Canceled after 0s
CI / test (push) Canceled after 0s
CI / frontend (push) Canceled after 0s
CI / golangci-lint (push) Canceled after 0s
Security Scan / backend-security (push) Canceled after 0s
Security Scan / frontend-security (push) Canceled after 0s
Release / update-version (push) Has been cancelled
Release / build-frontend (push) Has been cancelled
Release / release (push) Has been cancelled
Release / sync-version-file (push) Has been cancelled
CI / shell (push) Canceled after 0s
CI / test (push) Canceled after 0s
CI / frontend (push) Canceled after 0s
CI / golangci-lint (push) Canceled after 0s
Security Scan / backend-security (push) Canceled after 0s
Security Scan / frontend-security (push) Canceled after 0s
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
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
|
||||
}
|
||||
if !(&p).IsExpired(now) && p.Status != StatusExpired {
|
||||
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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user