Skip to content

Commit

Permalink
Reducing wait time complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
quentin.schmick committed Aug 30, 2022
1 parent 792dd3a commit 47da129
Showing 1 changed file with 30 additions and 88 deletions.
118 changes: 30 additions & 88 deletions src/Jobs/DebouncedJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace AlwaysOpen\Sidekick\Jobs;

use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Cache\Lock;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Foundation\Bus\PendingClosureDispatch;
Expand Down Expand Up @@ -64,6 +64,9 @@ public function handle()

$this->checkAndWaitUntilReady();

/**
* @var Lock $lock
*/
$lock = tap(Cache::lock($this->getCacheKey(), 6))->block(6);

try {
Expand All @@ -73,30 +76,36 @@ public function handle()
Cache::forget($this->getMinimumWaitTimeKey());
Cache::forget($this->getDebounceKey());

$lock->release();
$lock->forceRelease();
}
}

protected function checkAndWaitUntilReady() : void
public function getWaitTime() : int
{
$maximum = Cache::get($this->getMaximumWaitTimeKey());
$minimum = Cache::get($this->getMinimumWaitTimeKey());
$minimum = Cache::get($this->getMinimumWaitTimeKey()) ?? now();
$maximum = Cache::get($this->getMaximumWaitTimeKey()) ?? $minimum;

if (! $maximum && $minimum) {
while (! $this->minimumWaitComplete()) {
$this->setDebounce();
usleep(self::$MICROSECONDS_SLEEP);
}
} elseif (! $minimum && $maximum) {
while (! $this->maximumWaitComplete()) {
$this->setDebounce();
usleep(self::$MICROSECONDS_SLEEP);
}
} else {
while ($this->getMinimumMillisecondsLeft() > 0) {
$this->setDebounce();
usleep(self::$MICROSECONDS_SLEEP);
}
$now = now();

$minimumWait = $now->diffInMilliseconds($minimum);
$maximumWait = $now->diffInMilliseconds($maximum);

if ($minimumWait < 0) {
$minimumWait = 0;
}

if ($maximumWait < 0) {
$maximumWait = 0;
}

return min($minimumWait, $maximumWait);
}

protected function checkAndWaitUntilReady() : void
{
while (! $this->getWaitTime() > 0) {
$this->setDebounce();
usleep(self::$MICROSECONDS_SLEEP);
}
}

Expand All @@ -107,7 +116,7 @@ protected function debounceExists() : bool

protected function setDebounce() : void
{
Cache::put($this->getDebounceKey(), true, now()->addMilliseconds($this->getMaximumMillisecondsLeft() + self::$MICROSECONDS_SLEEP));
Cache::put($this->getDebounceKey(), true, now()->addMilliseconds($this->getWaitTime() *2));
}

protected function getDebounceKey() : string
Expand All @@ -122,16 +131,6 @@ protected function persistMinimumWaitTime() : void
Cache::put($this->getMinimumWaitTimeKey(), $minimum, $minimum->addMinute());
}

protected function minimumWaitComplete() : bool
{
/**
* @var Carbon|null $minimum
*/
$minimum = Cache::get($this->getMinimumWaitTimeKey());

return ! $minimum || $minimum->isPast();
}

protected function getMinimumWaitTimeKey() : string
{
return $this->getCacheKey() . ':minimum';
Expand All @@ -146,17 +145,6 @@ protected function persistMaximumWaitTime() : void

Cache::put($this->getMaximumWaitTimeKey(), $maximum, $maximum?->addMinute());
}

protected function maximumWaitComplete() : bool
{
/**
* @var Carbon|null $maximum
*/
$maximum = Cache::get($this->getMaximumWaitTimeKey());

return ! $maximum || $maximum->isPast();
}

protected function getMaximumWaitTimeKey() : string
{
return $this->getCacheKey() . ':maximum';
Expand All @@ -175,50 +163,4 @@ public function getCacheKey() : string
{
return $this->_cacheKey;
}

protected function getMinimumMillisecondsLeft() : int
{
/**
* @var Carbon|null $minimum
* @var Carbon|null $maximum
*/
$minimum = Cache::get($this->getMinimumWaitTimeKey());
$maximum = Cache::get($this->getMaximumWaitTimeKey());
$now = now();

if ($minimum && $maximum) {
return min($now->diffInMilliseconds($maximum, absolute: false), $now->diffInMilliseconds($minimum, absolute: false));
}

if ($minimum) {
return $now->diffInMilliseconds($minimum, absolute: false);
}

return 0;
}

protected function getMaximumMillisecondsLeft() : int
{
/**
* @var Carbon|null $minimum
* @var Carbon|null $maximum
*/
$minimum = Cache::get($this->getMinimumWaitTimeKey());
$maximum = Cache::get($this->getMaximumWaitTimeKey());
$now = now();

if ($minimum && $maximum) {
return max($now->diffInMilliseconds($maximum, absolute: false), $now->diffInMilliseconds($minimum, absolute: false));
}

if ($maximum) {
return $now->diffInMilliseconds($maximum, absolute: false);
}

if ($minimum) {
return $now->diffInMilliseconds($minimum, absolute: false);
}

return 0;
}
}

0 comments on commit 47da129

Please sign in to comment.