File tree Expand file tree Collapse file tree 1 file changed +4
-1
lines changed Expand file tree Collapse file tree 1 file changed +4
-1
lines changed Original file line number Diff line number Diff 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 ,
You can’t perform that action at this time.
0 commit comments