generated from glhd/laravel-package-template
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic working id resolver implementation
- Loading branch information
Showing
7 changed files
with
162 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,5 +6,5 @@ | |
|
||
interface ResolvesIds | ||
{ | ||
public function get(): WorkerIds; | ||
public function get(...$lengths): WorkerIds; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
namespace Glhd\Bits\IdResolvers; | ||
|
||
use Illuminate\Cache\CacheManager; | ||
use Illuminate\Support\DateFactory; | ||
use RuntimeException; | ||
|
||
class CacheResolver extends IdResolver | ||
{ | ||
protected ?int $value = null; | ||
|
||
public function __construct( | ||
protected CacheManager $cache, | ||
protected DateFactory $dates, | ||
protected int $max = 0b1111111111, | ||
) { | ||
} | ||
|
||
protected function value(): int | ||
{ | ||
return $this->value ??= $this->acquire(); | ||
} | ||
|
||
protected function acquire(): int | ||
{ | ||
// First we acquire a lock to ensure no other process is reserving an ID. Then we | ||
// get the current list of reserved IDs, and either find one that hasn't been | ||
// reserved, or one where the reservation is expired. | ||
|
||
return $this->cache->lock('glhd-bits-ids:lock') | ||
->block(5, function() { | ||
$reserved = $this->cache->get('glhd-bits-ids:reserved', fn() => []); | ||
|
||
if ($id = $this->firstAvailable($reserved) ?? $this->findExpired($reserved)) { | ||
$reserved[$id] = $this->dates->now()->addHour()->unix(); | ||
$this->cache->forever('glhd-bits-ids:reserved', $reserved); | ||
return $id; | ||
} | ||
|
||
throw new RuntimeException('Unable to acquire a unique worker ID.'); | ||
}); | ||
} | ||
|
||
protected function firstAvailable(array $reserved): ?int | ||
{ | ||
for ($id = 0; $id <= $this->max; $id++) { | ||
if (! isset($reserved[$id])) { | ||
return $id; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
protected function findExpired(array $reserved): ?int | ||
{ | ||
$now = $this->dates->now()->unix(); | ||
|
||
[$_, $id] = collect($reserved) | ||
->filter(fn($expiration) => $expiration <= $now) | ||
->reduce(function($carry, $expiration, $id) { | ||
return $expiration < $carry[0] ? [$expiration, $id] : $carry; | ||
}, [PHP_INT_MAX, null]); | ||
|
||
return $id; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace Glhd\Bits\IdResolvers; | ||
|
||
use Glhd\Bits\Config\WorkerIds; | ||
use Glhd\Bits\Contracts\ResolvesIds; | ||
|
||
abstract class IdResolver implements ResolvesIds | ||
{ | ||
abstract protected function value(): int; | ||
|
||
public function get(...$lengths): WorkerIds | ||
{ | ||
$value = $this->value(); | ||
$ids = []; | ||
|
||
foreach (array_reverse($lengths) as $length) { | ||
$bitmask = (1 << $length) - 1; | ||
array_unshift($ids, $value & $bitmask); | ||
$value = $value >> $length; | ||
} | ||
|
||
return new WorkerIds(...$ids); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters