From e836b4ac1014f87ba1c669b4825e2a3e1792ff45 Mon Sep 17 00:00:00 2001 From: hkfires <10558748+hkfires@users.noreply.github.com> Date: Fri, 26 Sep 2025 09:49:53 +0800 Subject: [PATCH] fix(auth): Make round-robin auth selection deterministic --- sdk/cliproxy/auth/manager.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/sdk/cliproxy/auth/manager.go b/sdk/cliproxy/auth/manager.go index edda9273..75c21991 100644 --- a/sdk/cliproxy/auth/manager.go +++ b/sdk/cliproxy/auth/manager.go @@ -15,6 +15,7 @@ import ( "github.com/router-for-me/CLIProxyAPI/v6/internal/util" cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/executor" log "github.com/sirupsen/logrus" + "sort" ) // ProviderExecutor defines the contract required by Manager to execute provider calls. @@ -796,6 +797,12 @@ func (m *Manager) pickNext(ctx context.Context, provider, model string, opts cli } candidates = append(candidates, auth.Clone()) } + // Ensure stable iteration order across requests to make round-robin deterministic. + // Go map iteration order is randomized; without sorting, the selector's index-based + // rotation may appear non-round-robin. + if len(candidates) > 1 { + sort.Slice(candidates, func(i, j int) bool { return candidates[i].ID < candidates[j].ID }) + } m.mu.RUnlock() if len(candidates) == 0 { return nil, nil, &Error{Code: "auth_not_found", Message: "no auth available"}