Update snowflake config, allow custom datacenter/worker ids

pull/5859/head
Daniel Supernault 2025-03-13 23:44:02 -06:00
rodzic 134eb6324e
commit 806e210f13
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 23740873EE6F76A1
3 zmienionych plików z 40 dodań i 36 usunięć

Wyświetl plik

@ -2,7 +2,6 @@
namespace App\Services\Account;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;
class AccountStatService
@ -33,7 +32,7 @@ class AccountStatService
{
return Redis::zrangebyscore(
self::REFRESH_CACHE_KEY,
'(' . $lastId,
'('.$lastId,
'+inf',
['limit' => [0, $count]]
);

Wyświetl plik

@ -2,45 +2,50 @@
namespace App\Services;
use Illuminate\Support\Carbon;
use Cache;
use Illuminate\Support\Carbon;
class SnowflakeService {
class SnowflakeService
{
public static function byDate(?Carbon $ts = null)
{
if ($ts instanceof Carbon) {
$ts = now()->parse($ts)->timestamp;
} else {
return self::next();
}
public static function byDate(Carbon $ts = null)
{
if($ts instanceOf Carbon) {
$ts = now()->parse($ts)->timestamp;
} else {
return self::next();
}
$datacenterId = config('snowflake.datacenter_id') ?? random_int(1, 31);
$workerId = config('snowflake.worker_id') ?? random_int(1, 31);
return ((round($ts * 1000) - 1549756800000) << 22)
| (random_int(1,31) << 17)
| (random_int(1,31) << 12)
| 0;
}
return ((round($ts * 1000) - 1549756800000) << 22)
| ($datacenterId << 17)
| ($workerId << 12)
| 0;
}
public static function next()
{
$seq = Cache::get('snowflake:seq');
public static function next()
{
$seq = Cache::get('snowflake:seq');
if(!$seq) {
Cache::put('snowflake:seq', 1);
$seq = 1;
} else {
Cache::increment('snowflake:seq');
}
if (! $seq) {
Cache::put('snowflake:seq', 1);
$seq = 1;
} else {
Cache::increment('snowflake:seq');
}
if($seq >= 4095) {
Cache::put('snowflake:seq', 0);
$seq = 0;
}
if ($seq >= 4095) {
Cache::put('snowflake:seq', 0);
$seq = 0;
}
return ((round(microtime(true) * 1000) - 1549756800000) << 22)
| (random_int(1,31) << 17)
| (random_int(1,31) << 12)
| $seq;
}
$datacenterId = config('snowflake.datacenter_id') ?? random_int(1, 31);
$workerId = config('snowflake.worker_id') ?? random_int(1, 31);
return ((round(microtime(true) * 1000) - 1549756800000) << 22)
| ($datacenterId << 17)
| ($workerId << 12)
| $seq;
}
}

Wyświetl plik

@ -1,6 +1,6 @@
<?php
return [
'epoch' => 1549756800000,
'worker_id' => 1,
'datacenter_id' => 1,
'worker_id' => env('SNOWFLAKE_WORKER_ID', null),
'datacenter_id' => env('SNOWFLAKE_DATACENTER_ID', null),
];