diff --git a/lib/private/Snowflake/SnowflakeGenerator.php b/lib/private/Snowflake/SnowflakeGenerator.php index fcadc2a2a8ca1..e5f29a8b1d6a8 100644 --- a/lib/private/Snowflake/SnowflakeGenerator.php +++ b/lib/private/Snowflake/SnowflakeGenerator.php @@ -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) { @@ -104,12 +104,18 @@ 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 + $serverid = hexdec(hash('xxh32', gethostname() ?: random_bytes(8))); + } + + return $serverid & 0x1FF; } private function isCli(): bool {