| 82 | } |
| 83 | |
| 84 | func (e *BasicEngine) allowByRateLimit(tenantID string, limit int) bool { |
| 85 | if limit <= 0 { |
| 86 | return true |
| 87 | } |
| 88 | |
| 89 | nowSec := time.Now().Unix() |
| 90 | e.mu.Lock() |
| 91 | defer e.mu.Unlock() |
| 92 | |
| 93 | if e.windowSecond != nowSec { |
| 94 | e.windowSecond = nowSec |
| 95 | e.tenantCounts = map[string]int{} |
| 96 | } |
| 97 | |
| 98 | current := e.tenantCounts[tenantID] |
| 99 | if current >= limit { |
| 100 | return false |
| 101 | } |
| 102 | |
| 103 | e.tenantCounts[tenantID] = current + 1 |
| 104 | return true |
| 105 | } |
| 106 | |
| 107 | func validateAgentTenantLimits(req types.AIRequest, tenant config.TenantConfig) error { |
| 108 | if !strings.EqualFold(strings.TrimSpace(req.RequestType), types.RequestTypeAgent) { |