Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions lib/private/Snowflake/SnowflakeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function nextId(): string {
// Relative time
[$seconds, $milliseconds] = $this->getCurrentTime();

$serverId = $this->getServerId() & 0x1FF; // Keep 9 bits
$serverId = $this->getServerId(); // Already 9 bits
$isCli = (int)$this->isCli(); // 1 bit
$sequenceId = $this->sequenceGenerator->nextId($seconds, $milliseconds, $serverId); // 12 bits
if ($sequenceId > 0xFFF || $sequenceId === false) {
Expand Down Expand Up @@ -104,12 +104,20 @@ private function getCurrentTime(): array {

/**
* Return configured serverid or generate one if not set
*
* @return int<0,511>
*/
private function getServerId(): int {
$serverid = $this->config->getSystemValueInt('serverid', -1);
return $serverid > 0
? $serverid
: crc32(gethostname() ?: random_bytes(8));
if ($serverid < 1) {
// Fallback: generates a server ID based on hostname
// or random bytes if hostname isn't available
/** @var int<0,max> */
$serverid = hexdec(hash('xxh32', gethostname() ?: random_bytes(8)));
}

/** @var int<0,511> */
return $serverid & 0x1FF;
}

private function isCli(): bool {
Expand Down
Loading