-
-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathAbstractContext.php
More file actions
142 lines (121 loc) · 4.01 KB
/
AbstractContext.php
File metadata and controls
142 lines (121 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php declare(strict_types=1);
namespace Amp\Parallel\Context\Internal;
use Amp\Cancellation;
use Amp\CancelledException;
use Amp\ForbidCloning;
use Amp\ForbidSerialization;
use Amp\Future;
use Amp\Parallel\Context\Context;
use Amp\Parallel\Context\ContextException;
use Amp\Sync\Channel;
use Amp\Sync\ChannelException;
use function Amp\async;
use function Amp\Parallel\Context\flattenArgument;
/**
* @template-covariant TResult
* @template-covariant TReceive
* @template TSend
* @implements Context<TResult, TReceive, TSend>
*/
abstract class AbstractContext implements Context
{
use ForbidCloning;
use ForbidSerialization;
/** @var Future<ExitResult<TResult>>|null */
private ?Future $result = null;
protected function __construct(
private readonly Channel $ipcChannel,
private readonly Channel $resultChannel,
) {
}
#[\Override]
public function receive(?Cancellation $cancellation = null): mixed
{
try {
$data = $this->ipcChannel->receive($cancellation);
} catch (ChannelException $exception) {
$this->ipcChannel->close();
throw new ContextException(
"The context stopped responding, potentially due to a fatal error or calling exit",
previous: $exception,
);
}
if (!$data instanceof ContextMessage) {
if ($data instanceof ExitResult) {
$data = $data->getResult();
throw new ContextException(\sprintf(
'Context unexpectedly exited when waiting to receive data with result: %s',
flattenArgument($data),
));
}
throw new ContextException(\sprintf(
'Unexpected data type from context: %s',
flattenArgument($data),
));
}
return $data->getMessage();
}
#[\Override]
public function send(mixed $data): void
{
try {
$this->ipcChannel->send($data);
} catch (ChannelException $exception) {
$this->ipcChannel->close();
throw new ContextException(
"The context stopped responding, potentially due to a fatal error or calling exit",
previous: $exception,
);
}
}
#[\Override]
public function close(): void
{
$this->ipcChannel->close();
}
#[\Override]
public function isClosed(): bool
{
return $this->ipcChannel->isClosed();
}
#[\Override]
public function onClose(\Closure $onClose): void
{
$this->ipcChannel->onClose($onClose);
}
protected function receiveExitResult(?Cancellation $cancellation = null): ExitResult
{
while ($this->result) {
try {
$this->result->await($cancellation);
} catch (CancelledException) {
// Ignore cancellation from a prior join request, throw only if this request was cancelled.
$cancellation?->throwIfRequested();
}
}
$this->result = async(function () use ($cancellation): ExitResult {
try {
$data = $this->resultChannel->receive($cancellation);
$this->resultChannel->close();
} catch (CancelledException $exception) {
throw $exception;
} catch (\Throwable $exception) {
$this->resultChannel->close();
throw new ContextException("Failed to receive result from context", previous: $exception);
}
if (!$data instanceof ExitResult) {
throw new ContextException(\sprintf(
"The context sent data instead of exiting: %s",
flattenArgument($data),
));
}
return $data;
});
try {
return $this->result->await();
} catch (CancelledException $exception) {
$this->result = null;
throw $exception;
}
}
}