|
32 | 32 | #include <folly/TokenBucket.h> |
33 | 33 |
|
34 | 34 | #include "mongo/logv2/log.h" |
| 35 | +#include "mongo/logv2/log_severity_suppressor.h" |
35 | 36 | #include "mongo/util/duration.h" |
36 | 37 | #include "mongo/util/scopeguard.h" |
37 | 38 |
|
@@ -123,6 +124,10 @@ Status RateLimiter::acquireToken(OperationContext* opCtx) { |
123 | 124 | // don't advance the mock clock before the sleep deadline is calculated. |
124 | 125 | Date_t deadline = opCtx->getServiceContext()->getPreciseClockSource()->now() + napTime; |
125 | 126 | if (auto status = _impl->enqueue(); !status.isOK()) { |
| 127 | + { |
| 128 | + auto lk = _impl->rwMutex.readLock(); |
| 129 | + _impl->tokenBucket.returnTokens(1.0); |
| 130 | + } |
126 | 131 | _impl->stats.rejectedAdmissions.incrementRelaxed(); |
127 | 132 | return status; |
128 | 133 | } |
@@ -157,6 +162,7 @@ Status RateLimiter::acquireToken(OperationContext* opCtx) { |
157 | 162 |
|
158 | 163 | _impl->stats.successfulAdmissions.incrementRelaxed(); |
159 | 164 | _impl->stats.averageTimeQueuedMicros.addSample(waitForTokenSecs * 1'000'000); |
| 165 | + |
160 | 166 | return Status::OK(); |
161 | 167 | } |
162 | 168 |
|
@@ -206,6 +212,11 @@ double RateLimiter::tokensAvailable() const { |
206 | 212 | return _impl->tokenBucket.available(); |
207 | 213 | } |
208 | 214 |
|
| 215 | +double RateLimiter::tokenBalance() const { |
| 216 | + auto lk = _impl->rwMutex.readLock(); |
| 217 | + return _impl->tokenBucket.balance(); |
| 218 | +} |
| 219 | + |
209 | 220 | int64_t RateLimiter::queued() const { |
210 | 221 | return _impl->queued.load(); |
211 | 222 | } |
|
0 commit comments