Adjust coroutine Redis pool sizing

This commit is contained in:
Chirag Aggarwal
2026-04-06 18:16:10 +05:30
parent 124bc3376f
commit d1b3e2ad81
2 changed files with 21 additions and 1 deletions
+10 -1
View File
@@ -251,6 +251,11 @@ $register->set('pools', function () {
$workerCount = intval(System::getEnv('_APP_CPU_NUM', swoole_cpu_num())) * intval(System::getEnv('_APP_WORKER_PER_CORE', 6));
$processCount = $isCoroutineHttp ? 1 : $workerCount;
$poolSize = max(1, (int)($instanceConnections / $processCount));
// The coroutine HTTP server collapses worker fan-out into one process, but the hot
// Redis-backed request paths still see roughly the same concurrent pressure as before.
// Size these pools to match the previous worker-level concurrency instead of the much
// smaller shared DB budget used by other connection types.
$redisHotPathPoolSize = $isCoroutineHttp ? max($poolSize, $workerCount) : $poolSize;
foreach ($connections as $key => $connection) {
$type = $connection['type'] ?? '';
@@ -337,7 +342,11 @@ $register->set('pools', function () {
$poolAdapter = System::getEnv('_APP_POOL_ADAPTER', default: 'stack') === 'swoole' ? new SwoolePool() : new StackPool();
$pool = new Pool($poolAdapter, $name, $poolSize, function () use ($type, $resource, $dsn) {
$currentPoolSize = \in_array($type, ['cache', 'publisher', 'consumer', 'pubsub'], true)
? $redisHotPathPoolSize
: $poolSize;
$pool = new Pool($poolAdapter, $name, $currentPoolSize, function () use ($type, $resource, $dsn) {
// Get Adapter
switch ($type) {
case 'database':
+11
View File
@@ -639,6 +639,17 @@ $server->onOpen(function (int $connection, SwooleRequest $request) use ($server,
$connectionContainer->set('response', fn () => $response);
$registerRequestResources($connectionContainer);
// Realtime keeps a coroutine-local persistent Redis connection for abuse checks.
// Overriding the request-scoped HTTP resource avoids opening a fresh TCP socket on
// every websocket connection under load.
$connectionContainer->set('redis', function () {
return getRedis();
}, []);
$connectionContainer->set('timelimit', function () {
return function (string $key, int $limit, int $time) {
return new TimeLimitRedis($key, $limit, $time, getRedis());
};
}, []);
$project = null;
$logUser = null;