From f415504d3574afb909fa9f8b24578e6cc8ff31e5 Mon Sep 17 00:00:00 2001 From: Finessse Date: Mon, 12 Jan 2026 07:37:03 +0700 Subject: [PATCH] feat(api): add rate limits monitoring endpoints - GET /v0/management/rate-limits - detailed per-credential status - GET /v0/management/rate-limits/summary - availability overview - Shows available, cooling_down, disabled counts and next recovery time --- .../api/handlers/management/rate_limits.go | 168 ++++++++++++++++++ internal/api/server.go | 4 + 2 files changed, 172 insertions(+) create mode 100644 internal/api/handlers/management/rate_limits.go diff --git a/internal/api/handlers/management/rate_limits.go b/internal/api/handlers/management/rate_limits.go new file mode 100644 index 00000000..bdb5d32f --- /dev/null +++ b/internal/api/handlers/management/rate_limits.go @@ -0,0 +1,168 @@ +package management + +import ( + "net/http" + "time" + + "github.com/gin-gonic/gin" +) + +// RateLimitEntry represents the rate limit status for a single credential. +type RateLimitEntry struct { + AuthID string `json:"auth_id"` + Provider string `json:"provider"` + Label string `json:"label,omitempty"` + Email string `json:"email,omitempty"` + QuotaExceeded bool `json:"quota_exceeded"` + QuotaReason string `json:"quota_reason,omitempty"` + RecoverAt time.Time `json:"recover_at,omitempty"` + RecoverIn string `json:"recover_in,omitempty"` + BackoffLevel int `json:"backoff_level,omitempty"` + ModelLimits []ModelRateLimitEntry `json:"model_limits,omitempty"` +} + +// ModelRateLimitEntry represents the rate limit status for a specific model. +type ModelRateLimitEntry struct { + Model string `json:"model"` + QuotaExceeded bool `json:"quota_exceeded"` + QuotaReason string `json:"quota_reason,omitempty"` + RecoverAt time.Time `json:"recover_at,omitempty"` + RecoverIn string `json:"recover_in,omitempty"` + BackoffLevel int `json:"backoff_level,omitempty"` +} + +// RateLimitsResponse contains overall rate limit status. +type RateLimitsResponse struct { + TotalCredentials int `json:"total_credentials"` + CoolingDown int `json:"cooling_down"` + Available int `json:"available"` + Credentials []RateLimitEntry `json:"credentials"` +} + +// GetRateLimits returns the rate limit status for all credentials. +// GET /v0/management/rate-limits +func (h *Handler) GetRateLimits(c *gin.Context) { + if h.authManager == nil { + c.JSON(http.StatusOK, RateLimitsResponse{}) + return + } + + auths := h.authManager.List() + now := time.Now() + + resp := RateLimitsResponse{ + TotalCredentials: len(auths), + Credentials: make([]RateLimitEntry, 0, len(auths)), + } + + for _, auth := range auths { + entry := RateLimitEntry{ + AuthID: auth.ID, + Provider: auth.Provider, + Label: auth.Label, + QuotaExceeded: auth.Quota.Exceeded, + QuotaReason: auth.Quota.Reason, + BackoffLevel: auth.Quota.BackoffLevel, + } + + // Extract email from metadata + if auth.Metadata != nil { + if email, ok := auth.Metadata["email"].(string); ok { + entry.Email = email + } + } + + // Calculate recover time + if !auth.Quota.NextRecoverAt.IsZero() { + entry.RecoverAt = auth.Quota.NextRecoverAt + if auth.Quota.NextRecoverAt.After(now) { + entry.RecoverIn = auth.Quota.NextRecoverAt.Sub(now).Round(time.Second).String() + } + } + + // Per-model rate limits + if len(auth.ModelStates) > 0 { + entry.ModelLimits = make([]ModelRateLimitEntry, 0, len(auth.ModelStates)) + for model, state := range auth.ModelStates { + if state.Quota.Exceeded || !state.Quota.NextRecoverAt.IsZero() { + modelEntry := ModelRateLimitEntry{ + Model: model, + QuotaExceeded: state.Quota.Exceeded, + QuotaReason: state.Quota.Reason, + BackoffLevel: state.Quota.BackoffLevel, + } + if !state.Quota.NextRecoverAt.IsZero() { + modelEntry.RecoverAt = state.Quota.NextRecoverAt + if state.Quota.NextRecoverAt.After(now) { + modelEntry.RecoverIn = state.Quota.NextRecoverAt.Sub(now).Round(time.Second).String() + } + } + entry.ModelLimits = append(entry.ModelLimits, modelEntry) + } + } + } + + // Count cooling down vs available + isCooling := auth.Quota.Exceeded || (auth.Quota.NextRecoverAt.After(now)) + if isCooling { + resp.CoolingDown++ + } else if !auth.Disabled && !auth.Unavailable { + resp.Available++ + } + + resp.Credentials = append(resp.Credentials, entry) + } + + c.JSON(http.StatusOK, resp) +} + +// GetRateLimitsSummary returns a brief summary of rate limit status. +// GET /v0/management/rate-limits/summary +func (h *Handler) GetRateLimitsSummary(c *gin.Context) { + if h.authManager == nil { + c.JSON(http.StatusOK, gin.H{ + "total": 0, + "available": 0, + "cooling_down": 0, + "disabled": 0, + }) + return + } + + auths := h.authManager.List() + now := time.Now() + + var available, coolingDown, disabled int + var nextRecovery time.Time + + for _, auth := range auths { + if auth.Disabled { + disabled++ + continue + } + + isCooling := auth.Quota.Exceeded || (auth.Quota.NextRecoverAt.After(now)) + if isCooling { + coolingDown++ + if nextRecovery.IsZero() || auth.Quota.NextRecoverAt.Before(nextRecovery) { + nextRecovery = auth.Quota.NextRecoverAt + } + } else if !auth.Unavailable { + available++ + } + } + + resp := gin.H{ + "total": len(auths), + "available": available, + "cooling_down": coolingDown, + "disabled": disabled, + } + + if !nextRecovery.IsZero() && nextRecovery.After(now) { + resp["next_recovery_at"] = nextRecovery + resp["next_recovery_in"] = nextRecovery.Sub(now).Round(time.Second).String() + } + + c.JSON(http.StatusOK, resp) +} diff --git a/internal/api/server.go b/internal/api/server.go index 82250a42..bbb47fdf 100644 --- a/internal/api/server.go +++ b/internal/api/server.go @@ -573,6 +573,10 @@ func (s *Server) registerManagementRoutes() { mgmt.GET("/usage/export", s.mgmt.ExportUsageStatistics) mgmt.POST("/usage/import", s.mgmt.ImportUsageStatistics) + // Rate limit monitoring + mgmt.GET("/rate-limits", s.mgmt.GetRateLimits) + mgmt.GET("/rate-limits/summary", s.mgmt.GetRateLimitsSummary) + // Request monitoring and history routes mgmt.GET("/requests", s.mgmt.GetRequests) mgmt.GET("/request-history", s.mgmt.GetRequestHistory)