mirror of
https://github.com/appwrite/appwrite.git
synced 2026-05-26 13:51:13 +00:00
Initialize coroutine semaphore on start
This commit is contained in:
@@ -19,6 +19,7 @@ class Server extends Adapter
|
||||
protected SwooleServer $server;
|
||||
protected Container $container;
|
||||
protected ?Channel $requestSemaphore = null;
|
||||
protected ?int $maxConcurrency = null;
|
||||
|
||||
/** @var callable|null */
|
||||
protected $onStartCallback = null;
|
||||
@@ -35,14 +36,7 @@ class Server extends Adapter
|
||||
'http_parse_cookie' => false,
|
||||
]));
|
||||
$this->container = $container ?? new Container();
|
||||
|
||||
if ($maxConcurrency !== null && $maxConcurrency > 0) {
|
||||
$this->requestSemaphore = new Channel($maxConcurrency);
|
||||
|
||||
for ($i = 0; $i < $maxConcurrency; $i++) {
|
||||
$this->requestSemaphore->push(true);
|
||||
}
|
||||
}
|
||||
$this->maxConcurrency = ($maxConcurrency !== null && $maxConcurrency > 0) ? $maxConcurrency : null;
|
||||
}
|
||||
|
||||
public function onRequest(callable $callback)
|
||||
@@ -92,6 +86,8 @@ class Server extends Adapter
|
||||
public function start()
|
||||
{
|
||||
go(function () {
|
||||
$this->initializeRequestSemaphore();
|
||||
|
||||
if ($this->onStartCallback) {
|
||||
\call_user_func($this->onStartCallback, $this);
|
||||
}
|
||||
@@ -99,4 +95,17 @@ class Server extends Adapter
|
||||
$this->server->start();
|
||||
});
|
||||
}
|
||||
|
||||
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