Skip to content

Commit edacb41

Browse files
committed
fixup! feat(database): introduce Snowflake IDs generator
Signed-off-by: Benjamin Gaussorgues <[email protected]>
1 parent 0ebb20b commit edacb41

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

lib/private/Snowflake/Generator.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function nextId(): string {
3535
$serverId = $this->getServerId() & 0x1FF; // Keep 9 bits
3636
$isCli = (int)$this->isCli(); // 1 bit
3737
$sequenceId = $this->getSequenceId($seconds, $milliseconds, $serverId); // 12 bits
38-
if ($sequenceId > 0xFFF) {
38+
if ($sequenceId > 0xFFF || $sequenceId === false) {
3939
// Throttle a bit, wait for next millisecond
4040
usleep(1000);
4141
return $this->nextId();
@@ -109,6 +109,12 @@ private function isCli(): bool {
109109
return PHP_SAPI === 'cli';
110110
}
111111

112+
/**
113+
* Generates sequence ID from APCu (general case) or random if APCu disabled or CLI
114+
*
115+
* @return int|false Sequence ID or false if APCu not ready
116+
* @throws \Exception if there is an error with APCu
117+
*/
112118
private function getSequenceId(int $seconds, int $milliseconds, int $serverId): int|false {
113119
$key = 'seq:' . $seconds . ':' . $milliseconds;
114120

0 commit comments

Comments
 (0)