checkRateLimit returns true if the request is allowed (under limit).
(ctx context.Context, userID string)
| 38 | |
| 39 | // checkRateLimit returns true if the request is allowed (under limit). |
| 40 | func (h *AIHandler) checkRateLimit(ctx context.Context, userID string) bool { |
| 41 | rdb := h.rdb.RDB(ctx) |
| 42 | if rdb == nil || userID == "" { |
| 43 | return true |
| 44 | } |
| 45 | key := "ratelimit:ai:" + userID |
| 46 | count, err := rdb.Incr(ctx, key).Result() |
| 47 | if err != nil { |
| 48 | return true // fail open |
| 49 | } |
| 50 | if count == 1 { |
| 51 | _ = rdb.Expire(ctx, key, rateLimitWindow).Err() |
| 52 | } |
| 53 | return count <= rateLimitMax |
| 54 | } |
| 55 | |
| 56 | // GetProviders returns available AI providers and models. |
| 57 | func (h *AIHandler) GetProviders(w http.ResponseWriter, r *http.Request) { |