Remove coroutine request semaphore

This commit is contained in:
Chirag Aggarwal
2026-04-06 10:10:09 +05:30
parent c7f79fb4c1
commit af7883f366
2 changed files with 1 additions and 38 deletions
+1 -10
View File
@@ -74,13 +74,6 @@ if (
}
$payloadSize = 12 * (1024 * 1024); // 12MB - adding slight buffer for headers and other data that might be sent with the payload - update later with valid testing
$requestMemoryBudget = max($payloadSize * 8, 256 * 1024 * 1024);
$memoryReserve = 256 * 1024 * 1024;
$availableRequestMemory = max($memoryLimitBytes - $memoryReserve, 0);
$computedMaxConcurrency = $availableRequestMemory > 0
? max(2, (int) floor($availableRequestMemory / $requestMemoryBudget))
: 2;
$maxConcurrency = max(2, (int) System::getEnv('_APP_HTTP_COROUTINE_MAX_CONCURRENCY', $computedMaxConcurrency));
$swooleAdapter = new Server(
host: "0.0.0.0",
@@ -92,7 +85,6 @@ $swooleAdapter = new Server(
'output_buffer_size' => $payloadSize,
],
container: $container,
maxConcurrency: $maxConcurrency,
);
$container->set('container', fn () => fn () => $swooleAdapter->getContainer());
@@ -197,7 +189,7 @@ function createDatabase(Http $app, string $resourceKey, string $dbName, array $c
// The coroutine adapter does not expose process-worker hooks, so startup work stays in
// a single onStart callback and request routing falls back to coroutine scheduling.
$swooleAdapter->onStart(function () use ($payloadSize, $swooleAdapter, $maxConcurrency) {
$swooleAdapter->onStart(function () use ($payloadSize, $swooleAdapter) {
$app = new Http($swooleAdapter, 'UTC');
/** @var \Utopia\Pools\Group $pools */
@@ -406,7 +398,6 @@ $swooleAdapter->onStart(function () use ($payloadSize, $swooleAdapter, $maxConcu
Span::add('server.adapter', 'swoole-coroutine');
Span::add('server.memory_limit', \ini_get('memory_limit'));
Span::add('server.payload_size', $payloadSize);
Span::add('server.max_concurrency', $maxConcurrency);
Span::current()?->finish();
});
@@ -3,7 +3,6 @@
namespace Appwrite\Utopia\Http\Adapter\SwooleCoroutine;
use Swoole\Coroutine;
use Swoole\Coroutine\Channel;
use Swoole\Coroutine\Http\Server as SwooleServer;
use Swoole\Http\Request as SwooleRequest;
use Swoole\Http\Response as SwooleResponse;
@@ -18,8 +17,6 @@ class Server extends Adapter
protected SwooleServer $server;
protected Container $container;
protected ?Channel $requestSemaphore = null;
protected ?int $maxConcurrency = null;
/** @var callable|null */
protected $onStartCallback = null;
@@ -29,23 +26,17 @@ class Server extends Adapter
?string $port = null,
array $settings = [],
?Container $container = null,
?int $maxConcurrency = null,
) {
$this->server = new SwooleServer($host, $port, false, true);
$this->server->set(\array_merge($settings, [
'http_parse_cookie' => false,
]));
$this->container = $container ?? new Container();
$this->maxConcurrency = ($maxConcurrency !== null && $maxConcurrency > 0) ? $maxConcurrency : null;
}
public function onRequest(callable $callback)
{
$this->server->handle('/', function (SwooleRequest $request, SwooleResponse $response) use ($callback) {
if ($this->requestSemaphore !== null) {
$this->requestSemaphore->pop();
}
$requestContainer = new Container($this->container);
$requestContainer->set('swooleRequest', fn () => $request);
$requestContainer->set('swooleResponse', fn () => $response);
@@ -56,10 +47,6 @@ class Server extends Adapter
\call_user_func($callback, new Request($request), new Response($response));
} finally {
unset(Coroutine::getContext()[self::REQUEST_CONTAINER_CONTEXT_KEY]);
if ($this->requestSemaphore !== null) {
$this->requestSemaphore->push(true);
}
}
});
}
@@ -86,8 +73,6 @@ class Server extends Adapter
public function start()
{
$startServer = function (): void {
$this->initializeRequestSemaphore();
if ($this->onStartCallback) {
\call_user_func($this->onStartCallback, $this);
}
@@ -103,17 +88,4 @@ class Server extends Adapter
\Swoole\Coroutine\run($startServer);
}
private function initializeRequestSemaphore(): void
{
if ($this->requestSemaphore !== null || $this->maxConcurrency === null) {
return;
}
$this->requestSemaphore = new Channel($this->maxConcurrency);
for ($i = 0; $i < $this->maxConcurrency; $i++) {
$this->requestSemaphore->push(true);
}
}
}