fix: follow live request results and prioritize usable groups
Publish Docker image (Multi-arch) / Build & push (amd64) (push) Failing after 1m59s
Release (Linux, macOS, Windows) / Linux Release (push) Failing after 4m32s
Publish Docker image (Multi-arch) / Build & push (arm64) (push) Canceled after 0s
Release (Linux, macOS, Windows) / macOS Release (push) Canceled after 0s
Release (Linux, macOS, Windows) / Windows Release (push) Canceled after 0s
Publish Docker image (Multi-arch) / Create multi-arch manifests (push) Canceled after 0s

This commit is contained in:
cneicy committed 2026-09-26 14:49:12 +08:00
1 parent a2d17be0c4
commit d2e03ab7e8
12 files changed
+257 -86

No files matched your search

+41 -21
View File
@@ -2,59 +2,79 @@ package perfmetrics
import (
"context"
"github.com/QuantumNous/new-api/common"
"strconv"
"strings"
"sync"
"time"
"github.com/QuantumNous/new-api/common"
)
var liveSuccess sync.Map
type successTime struct {
sync.Mutex
at int64
// At is milliseconds so a success and failure in the same second stay ordered.
type UserRequestResult struct {
At int64
Success bool
}
func recordUserSuccess(group string, at int64) {
var liveResults sync.Map
type userResultState struct {
sync.Mutex
result UserRequestResult
}
func recordUserResult(group string, result UserRequestResult) {
if group == "" {
return
}
value, _ := liveSuccess.LoadOrStore(group, &successTime{})
v := value.(*successTime)
value, _ := liveResults.LoadOrStore(group, &userResultState{})
v := value.(*userResultState)
v.Lock()
v.at = max(v.at, at)
if result.At >= v.result.At {
v.result = result
}
v.Unlock()
if common.RedisEnabled && common.RDB != nil {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
// A delayed writer must not replace a newer successful request.
_ = common.RDB.Eval(ctx, `local old=tonumber(redis.call('GET',KEYS[1]) or '0'); if tonumber(ARGV[1])>old then redis.call('SET',KEYS[1],ARGV[1],'EX',900) end; return 1`, []string{"status:user-success:" + group}, at).Err()
encoded := strconv.FormatInt(result.At, 10) + ":" + strconv.FormatBool(result.Success)
// Delayed writers cannot replace newer evidence from another instance.
_ = common.RDB.Eval(ctx, `local old=redis.call('GET',KEYS[1]); local at=old and tonumber(string.match(old,'^(%d+):')) or 0; if tonumber(ARGV[1])>=at then redis.call('SET',KEYS[1],ARGV[2],'EX',900) end; return 1`, []string{"status:user-result:" + group}, result.At, encoded).Err()
}
}
func LatestUserSuccess(ctx context.Context, groups []string) map[string]int64 {
result := map[string]int64{}
func LatestUserResults(ctx context.Context, groups []string) map[string]UserRequestResult {
result := map[string]UserRequestResult{}
for _, group := range groups {
if value, ok := liveSuccess.Load(group); ok {
v := value.(*successTime)
if value, ok := liveResults.Load(group); ok {
v := value.(*userResultState)
v.Lock()
result[group] = v.at
result[group] = v.result
v.Unlock()
}
}
if common.RedisEnabled && common.RDB != nil && len(groups) > 0 {
keys := make([]string, len(groups))
for i, g := range groups {
keys[i] = "status:user-success:" + g
keys[i] = "status:user-result:" + g
}
ctx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
values, err := common.RDB.MGet(ctx, keys...).Result()
if err == nil {
for i, value := range values {
if s, ok := value.(string); ok {
at, _ := strconv.ParseInt(s, 10, 64)
result[groups[i]] = max(result[groups[i]], at)
s, ok := value.(string)
if !ok {
continue
}
atText, successText, ok := strings.Cut(s, ":")
if !ok {
continue
}
at, atErr := strconv.ParseInt(atText, 10, 64)
success, successErr := strconv.ParseBool(successText)
if atErr == nil && successErr == nil && at >= result[groups[i]].At {
result[groups[i]] = UserRequestResult{At: at, Success: success}
}
}
}