From 806e210f136c882071c8f371fe9c3631fdf51298 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Thu, 13 Mar 2025 23:44:02 -0600 Subject: [PATCH 1/2] Update snowflake config, allow custom datacenter/worker ids --- app/Services/Account/AccountStatService.php | 3 +- app/Services/SnowflakeService.php | 69 +++++++++++---------- config/snowflake.php | 4 +- 3 files changed, 40 insertions(+), 36 deletions(-) diff --git a/app/Services/Account/AccountStatService.php b/app/Services/Account/AccountStatService.php index dd99ef7fc..4cb794659 100644 --- a/app/Services/Account/AccountStatService.php +++ b/app/Services/Account/AccountStatService.php @@ -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]] ); diff --git a/app/Services/SnowflakeService.php b/app/Services/SnowflakeService.php index f28dc6efb..fdf9a4f88 100644 --- a/app/Services/SnowflakeService.php +++ b/app/Services/SnowflakeService.php @@ -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; + } } diff --git a/config/snowflake.php b/config/snowflake.php index 7f7134158..d90c6e726 100644 --- a/config/snowflake.php +++ b/config/snowflake.php @@ -1,6 +1,6 @@ 1549756800000, - 'worker_id' => 1, - 'datacenter_id' => 1, + 'worker_id' => env('SNOWFLAKE_WORKER_ID', null), + 'datacenter_id' => env('SNOWFLAKE_DATACENTER_ID', null), ]; From f64f31427cf9c6384cb0a195b3b06722998ccb7c Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Thu, 13 Mar 2025 23:44:36 -0600 Subject: [PATCH 2/2] Update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a8da8c536..3dfbd0dd3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -51,6 +51,8 @@ - Update ApiV1Controller, fix max_id pagination on home and public timeline feeds ([38e17a06e](https://github.com/pixelfed/pixelfed/commit/38e17a06e)) - Update Post component, rewrite local post urls ([d2f2a1b1c](https://github.com/pixelfed/pixelfed/commit/d2f2a1b1c)) - Update Profile component, rewrite local profile urls ([dfbccaa19](https://github.com/pixelfed/pixelfed/commit/dfbccaa19)) +- Update AccountPostCountStatUpdate, fix memory leak ([134eb6324](https://github.com/pixelfed/pixelfed/commit/134eb6324)) +- Update snowflake config, allow custom datacenter/worker ids ([806e210f1](https://github.com/pixelfed/pixelfed/commit/806e210f1)) - ([](https://github.com/pixelfed/pixelfed/commit/)) ## [v0.12.4 (2024-11-08)](https://github.com/pixelfed/pixelfed/compare/v0.12.4...dev)