mirror of
https://github.com/appwrite/appwrite.git
synced 2026-05-26 13:51:13 +00:00
Remove coroutine request semaphore
This commit is contained in:
+1
-10
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user