Skip to content
This repository was archived by the owner on Jun 16, 2025. It is now read-only.

Commit 130492d

Browse files
committed
Refactor connection handling in Translator and RequestHandler to use inherited scopes for improved coroutine management
1 parent 33e98b4 commit 130492d

File tree

3 files changed

+27
-19
lines changed

3 files changed

+27
-19
lines changed

examples/LoadMonitor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ function loadMonitor(\Async\Coroutine $task, float $threshold = 1.00, int $check
3737
return;
3838
}
3939

40-
sleep($checkInterval);
40+
Async\timeout($checkInterval * 1000);
4141
unset($output);
4242
}
4343
}

examples/RequestHandler.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
final class RequestHandler
99
{
1010
private Scope $scope;
11+
private Scope $connectionScope;
1112

1213
public function __construct()
1314
{
@@ -21,25 +22,28 @@ public function start(): void
2122
{
2223
$server = stream_socket_server('tcp://0.0.0.0:8080');
2324

24-
$this->scope->spawn(function() use ($server) {
25+
spawn with $this->scope use($server) {
2526
try {
26-
while ($client = stream_socket_accept($server)) {
27-
$this->scope->spawn($this->handleConnection(...), $client);
27+
while (($client = stream_socket_accept($server)) !== false) {
28+
spawn with $this->connectionScope $this->handleConnection($client);
2829
}
2930
} finally {
3031
fclose($server);
32+
$this->scope->cancel();
3133
}
32-
});
34+
};
3335
}
3436

3537
private function handleConnection($client): void
3638
{
37-
try {
38-
$request = fread($client, 1024);
39-
$response = "HTTP/1.1 200 OK\r\nContent-Length: 12\r\n\r\nHello World!";
40-
fwrite($client, $response);
41-
} finally {
42-
fclose($client);
39+
async inherit bounded $scope {
40+
try {
41+
$request = fread($client, 1024);
42+
$response = "HTTP/1.1 200 OK\r\nContent-Length: 12\r\n\r\nHello World!";
43+
fwrite($client, $response);
44+
} finally {
45+
fclose($client);
46+
}
4347
}
4448
}
4549
}

examples/Translator/Translator.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
final class Translator
2020
{
2121
private Scope $scope;
22+
private Scope $connectionScope;
2223

2324
public function __construct(private LongPoll $longPoll, private \TranslatorHttpClient $translatorClient)
2425
{
2526
$this->scope = new Scope();
27+
$this->connectionScope = Scope::inherit($this->scope);
2628

2729
// Define an exception handle for all child scopes
2830
// Handling exceptions from child **Scopes** ensures that errors in child coroutines do not propagate
@@ -40,19 +42,21 @@ public function start(): void
4042
private function run(): void
4143
{
4244
while (($socket = $this->longPoll->receive()) !== null) {
43-
spawn with Scope::inherit($this->scope) $this->handleRequest($socket);
45+
spawn with $this->connectionScope $this->handleRequest($socket);
4446
}
4547
}
4648

4749
private function handleRequest(\Socket $socket): void
4850
{
49-
try {
50-
$this->handleLines($socket);
51-
} catch (\Throwable $exception) {
52-
$response = json_encode(['error' => $exception->getMessage(), 'code' => $exception->getCode()]);
53-
socket_write($socket, $response);
54-
} finally {
55-
socket_close($socket);
51+
async inherit bounded $scope {
52+
try {
53+
$this->handleLines($socket);
54+
} catch (\Throwable $exception) {
55+
$response = json_encode(['error' => $exception->getMessage(), 'code' => $exception->getCode()]);
56+
socket_write($socket, $response);
57+
} finally {
58+
socket_close($socket);
59+
}
5660
}
5761
}
5862

0 commit comments

Comments
 (0)