Skip to content

Commit 2ef0585

Browse files
erin2722MongoDB Bot
authored andcommitted
SERVER-105670 Calculate deadline before incrementing queue stats in RateLimiter (#36796)
GitOrigin-RevId: 749995500086a37d5ff1ba858928fbbf39b015f1
1 parent 4161f96 commit 2ef0585

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/mongo/db/admission/rate_limiter.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ Status RateLimiter::acquireToken(OperationContext* opCtx) {
119119
}
120120

121121
if (auto napTime = doubleToMillis(waitForTokenSecs); napTime > Milliseconds{0}) {
122+
// Calculate the deadline before incrementing the queued metric to ensure that unit tests
123+
// don't advance the mock clock before the sleep deadline is calculated.
124+
Date_t deadline = opCtx->getServiceContext()->getPreciseClockSource()->now() + napTime;
122125
if (auto status = _impl->enqueue(); !status.isOK()) {
123126
_impl->stats.rejectedAdmissions.incrementRelaxed();
124127
return status;
@@ -134,7 +137,7 @@ Status RateLimiter::acquireToken(OperationContext* opCtx) {
134137
"Going to sleep waiting for token acquisition",
135138
"rateLimiterName"_attr = _impl->name,
136139
"napTimeMillis"_attr = napTime.toString());
137-
opCtx->sleepFor(napTime);
140+
opCtx->sleepUntil(deadline);
138141
} catch (const DBException& e) {
139142
_impl->stats.interruptedInQueue.incrementRelaxed();
140143
LOGV2_DEBUG(10440800,

0 commit comments

Comments
 (0)