Skip to content

Commit d3e4143

Browse files
authored
Fix Swoole coroutine server lifecycle (#231)
1 parent 2b4021b commit d3e4143

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

src/Http/Adapter/SwooleCoroutine/Server.php

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,20 @@
1111

1212
class Server extends Adapter
1313
{
14-
protected SwooleServer $server;
1514
protected const REQUEST_CONTAINER_CONTEXT_KEY = '__utopia_http_request_container';
15+
16+
protected SwooleServer $server;
1617
protected Container $container;
18+
1719
/** @var callable|null */
1820
protected $onStartCallback = null;
1921

20-
public function __construct(string $host, ?string $port = null, array $settings = [], ?Container $container = null)
21-
{
22+
public function __construct(
23+
string $host,
24+
?string $port = null,
25+
array $settings = [],
26+
?Container $container = null
27+
) {
2228
$this->server = new SwooleServer($host, $port, false, true);
2329
$this->server->set(\array_merge($settings, [
2430
'http_parse_cookie' => false,
@@ -29,25 +35,23 @@ public function __construct(string $host, ?string $port = null, array $settings
2935
public function onRequest(callable $callback)
3036
{
3137
$this->server->handle('/', function (SwooleRequest $request, SwooleResponse $response) use ($callback) {
32-
go(function () use ($request, $response, $callback) {
33-
$requestContainer = new Container($this->container);
34-
$requestContainer->set('swooleRequest', fn () => $request);
35-
$requestContainer->set('swooleResponse', fn () => $response);
38+
$requestContainer = new Container($this->container);
39+
$requestContainer->set('swooleRequest', fn () => $request);
40+
$requestContainer->set('swooleResponse', fn () => $response);
3641

37-
Coroutine::getContext()[self::REQUEST_CONTAINER_CONTEXT_KEY] = $requestContainer;
42+
Coroutine::getContext()[self::REQUEST_CONTAINER_CONTEXT_KEY] = $requestContainer;
3843

44+
try {
3945
\call_user_func($callback, new Request($request), new Response($response));
40-
});
46+
} finally {
47+
unset(Coroutine::getContext()[self::REQUEST_CONTAINER_CONTEXT_KEY]);
48+
}
4149
});
4250
}
4351

4452
public function getContainer(): Container
4553
{
46-
if (Coroutine::getCid() !== -1) {
47-
return Coroutine::getContext()[self::REQUEST_CONTAINER_CONTEXT_KEY] ?? $this->container;
48-
}
49-
50-
return $this->container;
54+
return Coroutine::getContext()[self::REQUEST_CONTAINER_CONTEXT_KEY] ?? $this->container;
5155
}
5256

5357
public function getServer(): SwooleServer
@@ -62,11 +66,10 @@ public function onStart(callable $callback)
6266

6367
public function start()
6468
{
65-
go(function () {
66-
if ($this->onStartCallback) {
67-
\call_user_func($this->onStartCallback, $this);
68-
}
69-
$this->server->start();
70-
});
69+
if ($this->onStartCallback) {
70+
\call_user_func($this->onStartCallback, $this);
71+
}
72+
73+
$this->server->start();
7174
}
7275
}

0 commit comments

Comments
 (0)