Check if a request from the tenant is within quota.
(&self, tenant_id: TenantId)
| 136 | |
| 137 | /// Check if a request from the tenant is within quota. |
| 138 | pub fn check(&self, tenant_id: TenantId) -> QuotaCheck { |
| 139 | let quota = self.quota(tenant_id); |
| 140 | let usage = self.usage.get(&tenant_id); |
| 141 | |
| 142 | let usage = match usage { |
| 143 | Some(u) => u, |
| 144 | None => return QuotaCheck::Allowed, // No usage yet. |
| 145 | }; |
| 146 | |
| 147 | if usage.memory_bytes > quota.max_memory_bytes { |
| 148 | return QuotaCheck::MemoryExceeded { |
| 149 | used: usage.memory_bytes, |
| 150 | limit: quota.max_memory_bytes, |
| 151 | }; |
| 152 | } |
| 153 | if usage.storage_bytes > quota.max_storage_bytes { |
| 154 | return QuotaCheck::StorageExceeded { |
| 155 | used: usage.storage_bytes, |
| 156 | limit: quota.max_storage_bytes, |
| 157 | }; |
| 158 | } |
| 159 | if usage.active_requests >= quota.max_concurrent_requests { |
| 160 | return QuotaCheck::ConcurrencyExceeded { |
| 161 | active: usage.active_requests, |
| 162 | limit: quota.max_concurrent_requests, |
| 163 | }; |
| 164 | } |
| 165 | if usage.requests_this_second >= quota.max_qps { |
| 166 | return QuotaCheck::RateLimited { |
| 167 | qps: usage.requests_this_second, |
| 168 | limit: quota.max_qps, |
| 169 | }; |
| 170 | } |
| 171 | |
| 172 | QuotaCheck::Allowed |
| 173 | } |
| 174 | |
| 175 | /// Record a new request from a tenant. |
| 176 | pub fn request_start(&mut self, tenant_id: TenantId) { |