From 5bf7a9575c6522a4be996f61b606e014903e1bc1 Mon Sep 17 00:00:00 2001 From: Luis Pater Date: Tue, 23 Sep 2025 10:21:45 +0800 Subject: [PATCH] fix(auth): address index logic bug and remove redundant conditions - Updated loop iteration in `AuthSelector` to correct index management for selecting candidates. - Fixed cursor index reset condition for large values to prevent overflow. - Removed unnecessary conditional reassignment of `allowRemote` in management handler for clarity and correctness. --- internal/api/handlers/management/handler.go | 3 --- sdk/cliproxy/auth/selector.go | 10 ++++++++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/internal/api/handlers/management/handler.go b/internal/api/handlers/management/handler.go index 10781e94..bb999fee 100644 --- a/internal/api/handlers/management/handler.go +++ b/internal/api/handlers/management/handler.go @@ -73,9 +73,6 @@ func (h *Handler) Middleware() gin.HandlerFunc { h.attemptsMu.Unlock() allowRemote := h.cfg.RemoteManagement.AllowRemote - if !allowRemote { - allowRemote = true - } if !allowRemote { c.AbortWithStatusJSON(http.StatusForbidden, gin.H{"error": "remote management disabled"}) return diff --git a/sdk/cliproxy/auth/selector.go b/sdk/cliproxy/auth/selector.go index 62136b7f..291c590c 100644 --- a/sdk/cliproxy/auth/selector.go +++ b/sdk/cliproxy/auth/selector.go @@ -26,7 +26,7 @@ func (s *RoundRobinSelector) Pick(ctx context.Context, provider, model string, o } available := make([]*Auth, 0, len(auths)) now := time.Now() - for i := range auths { + for i := 0; i < len(auths); i++ { candidate := auths[i] if candidate.Unavailable && candidate.NextRetryAfter.After(now) { continue @@ -42,7 +42,13 @@ func (s *RoundRobinSelector) Pick(ctx context.Context, provider, model string, o key := provider + ":" + model s.mu.Lock() index := s.cursors[key] - s.cursors[key] = (index + 1) % len(available) + + if index >= 2_147_483_640 { + index = 0 + } + + s.cursors[key] = index + 1 s.mu.Unlock() + // log.Debugf("available: %d, index: %d, key: %d", len(available), index, index%len(available)) return available[index%len(available)], nil }