Report whether the limiter currently has capacity without consuming a token, rolling the window over first so an elapsed window reports capacity again. Used by `poll_ready` so an exhausted limiter reports readiness instead of blocking on inner-service backpressure: `call` can then return `RESOURCE_EXHAUSTED` immediately rather than waiting for the inner gRPC service to become ready.
(&self)
| 276 | /// `RESOURCE_EXHAUSTED` immediately rather than waiting for the inner gRPC |
| 277 | /// service to become ready. |
| 278 | fn has_capacity(&self) -> bool { |
| 279 | let now = Instant::now(); |
| 280 | let mut state = self |
| 281 | .state |
| 282 | .lock() |
| 283 | .unwrap_or_else(std::sync::PoisonError::into_inner); |
| 284 | if now.duration_since(state.window_started) >= self.window { |
| 285 | state.window_started = now; |
| 286 | state.remaining = self.requests; |
| 287 | } |
| 288 | state.remaining > 0 |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | #[derive(Clone)] |