-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphp_atomic_queue_lib.php
More file actions
233 lines (205 loc) 路 6.27 KB
/
Copy pathphp_atomic_queue_lib.php
File metadata and controls
233 lines (205 loc) 路 6.27 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
declare(strict_types=1);
class AtomicQueueException extends RuntimeException
{
}
final class AtomicQueueTimeoutException extends AtomicQueueException
{
}
final class AtomicQueueClient
{
private const OP_PUSH = 1;
private const OP_POP = 2;
private const STATUS_OK = 1;
private const STATUS_ERROR = 2;
/** @var resource */
private $stream;
public function __construct(private readonly string $socketPath)
{
$this->stream = $this->connect(true);
}
/**
* @return resource
*/
private function connect(bool $allowAutostart)
{
$stream = @stream_socket_client(
'unix://' . $this->socketPath,
$errno,
$errstr,
2.0,
STREAM_CLIENT_CONNECT
);
if (!is_resource($stream)) {
if ($allowAutostart && ($errno === 2 || $errno === 111)) {
atomic_queue_ensure_daemon($this->socketPath);
return $this->connect(false);
}
throw new AtomicQueueException(sprintf(
'connect to daemon failed: %s (%d) at %s',
$errstr ?: 'unknown error',
$errno,
$this->socketPath
));
}
stream_set_write_buffer($stream, 0);
stream_set_read_buffer($stream, 0);
return $stream;
}
public function __destruct()
{
if (is_resource($this->stream)) {
fclose($this->stream);
}
}
public function push(string $channel, string $payload): void
{
$this->writeRequest(self::OP_PUSH, [$channel], $payload, 0);
$response = $this->readResponse();
if (!$response['ok']) {
throw new AtomicQueueException($response['error']);
}
}
public function pop(array $channels, int $timeoutMs = 0): string
{
$this->writeRequest(self::OP_POP, $channels, '', $timeoutMs);
$response = $this->readResponse();
if ($response['ok']) {
return $response['payload'];
}
if ($response['error'] === 'timeout') {
throw new AtomicQueueTimeoutException('timeout');
}
throw new AtomicQueueException($response['error']);
}
private function writeRequest(int $op, array $channels, string $payload, int $timeoutMs): void
{
$frame = chr($op);
$frame .= self::packInt64BE($timeoutMs);
$frame .= pack('n', count($channels));
foreach ($channels as $channel) {
$frame .= pack('n', strlen($channel));
$frame .= $channel;
}
$frame .= pack('N', strlen($payload));
$frame .= $payload;
$this->writeAll($frame);
}
/**
* @return array{ok: bool, channel: string, payload: string, error: string}
*/
private function readResponse(): array
{
$status = ord($this->readExact(1));
$channel = $this->readString16();
$payload = $this->readBytes32();
$error = $this->readString32();
return [
'ok' => $status === self::STATUS_OK,
'channel' => $channel,
'payload' => $payload,
'error' => $error,
];
}
private function readString16(): string
{
$header = unpack('nlen', $this->readExact(2));
return $this->readExact($header['len']);
}
private function readString32(): string
{
return $this->readBytes32();
}
private function readBytes32(): string
{
$header = unpack('Nlen', $this->readExact(4));
return $this->readExact($header['len']);
}
private function readExact(int $length): string
{
if ($length === 0) {
return '';
}
$buffer = '';
while (strlen($buffer) < $length) {
$chunk = fread($this->stream, $length - strlen($buffer));
if ($chunk === false || $chunk === '') {
throw new AtomicQueueException('unexpected EOF from daemon');
}
$buffer .= $chunk;
}
return $buffer;
}
private function writeAll(string $data): void
{
$written = 0;
$length = strlen($data);
while ($written < $length) {
$n = fwrite($this->stream, substr($data, $written));
if ($n === false || $n === 0) {
throw new AtomicQueueException('write to daemon failed');
}
$written += $n;
}
}
private static function packInt64BE(int $value): string
{
$hi = ($value >> 32) & 0xffffffff;
$lo = $value & 0xffffffff;
return pack('NN', $hi, $lo);
}
}
function atomic_queue_default_socket_path(): string
{
$override = getenv('ATOMIC_QUEUE_SOCKET');
if (is_string($override) && $override !== '') {
return $override;
}
return sprintf('/run/user/%d/atomic-queue/atomic-queue.sock', posix_getuid());
}
function atomic_queue_binary_path(): string
{
$override = getenv('ATOMIC_QUEUE_BIN');
if (is_string($override) && $override !== '') {
return $override;
}
return dirname(__DIR__) . '/atomic-queue';
}
function atomic_queue_ensure_daemon(string $socketPath): void
{
$bin = atomic_queue_binary_path();
if (!function_exists('pcntl_fork') || !function_exists('pcntl_exec')) {
throw new AtomicQueueException(sprintf('cannot autostart daemon at %s: pcntl support required', $socketPath));
}
$pid = pcntl_fork();
if ($pid === -1) {
throw new AtomicQueueException(sprintf('start daemon failed at %s', $socketPath));
}
if ($pid === 0) {
if (function_exists('posix_setsid')) {
@posix_setsid();
}
pcntl_exec($bin, ['serve', '--socket', $socketPath]);
exit(1);
}
$deadline = microtime(true) + 2.0;
do {
$stream = @stream_socket_client(
'unix://' . $socketPath,
$errno,
$errstr,
0.1,
STREAM_CLIENT_CONNECT
);
if (is_resource($stream)) {
fclose($stream);
return;
}
usleep(50_000);
} while (microtime(true) < $deadline);
throw new AtomicQueueException(sprintf(
"daemon did not start at %s\ntry:\n atomic-queue serve --socket \"%s\"",
$socketPath,
$socketPath
));
}