Skip to content

Commit 7b5e5ff

Browse files
committed
Refactor AbstractQueue::get()
1 parent 839e2c2 commit 7b5e5ff

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

src/AbstractQueue.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -152,23 +152,16 @@ final public function ensureCountIndex(array $fields, bool $includeRunning)
152152
*/
153153
final public function get(array $query, array $options = []) : array
154154
{
155-
$options += self::DEFAULT_GET_OPTIONS;
156-
157155
$completeQuery = $this->buildPayloadQuery(
158156
['earliestGet' => ['$lte' => new UTCDateTime((int)(microtime(true) * 1000))]],
159157
$query
160158
);
161159

162-
$resetTimestamp = $this->calculateResetTimestamp($options['runningResetDuration']);
163-
164-
$update = ['$set' => ['earliestGet' => new UTCDateTime($resetTimestamp)]];
165-
166-
//ints overflow to floats, should be fine
167-
$end = microtime(true) + ($options['waitDurationInMillis'] / 1000.0);
160+
$options += self::DEFAULT_GET_OPTIONS;
161+
$update = ['$set' => ['earliestGet' => $this->calculateEarliestGet($options['runningResetDuration'])]];
162+
$end = $this->calculateEndTime($options['waitDurationInMillis']);
168163
$sleepTime = $this->calculateSleepTime($options['pollDurationInMillis']);
169-
170164
$messages = new ArrayObject();
171-
172165
while (count($messages) < $options['maxNumberOfMessages']) {
173166
if ($this->tryFindOneAndUpdate($completeQuery, $update, $messages)) {
174167
continue;
@@ -306,11 +299,11 @@ private function calculateSleepTime(int $pollDurationInMillis) : int
306299
return is_int($sleepTime) ? $sleepTime : PHP_INT_MAX;
307300
}
308301

309-
private function calculateResetTimestamp(int $runningResetDuration) : int
302+
private function calculateEarliestGet(int $runningResetDuration) : UTCDateTime
310303
{
311304
$resetTimestamp = time() + $runningResetDuration;
312305
//ints overflow to floats, max at PHP_INT_MAX
313-
return min(max(0, $resetTimestamp * 1000), self::MONGO_INT32_MAX);
306+
return new UTCDateTime(min(max(0, $resetTimestamp * 1000), self::MONGO_INT32_MAX));
314307
}
315308

316309
private function tryFindOneAndUpdate(array $query, array $update, ArrayObject $messages) : bool
@@ -412,4 +405,10 @@ private function throwIfTrue(
412405
throw $reflectionClass->newInstanceArgs([$message]);
413406
}
414407
}
408+
409+
private function calculateEndTime(int $waitDurationInMillis) : int
410+
{
411+
//ints overflow to floats, should be fine
412+
return microtime(true) + ($waitDurationInMillis / 1000.0);
413+
}
415414
}

0 commit comments

Comments
 (0)