|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ReactInspector\EventLoop; |
|
6
|
|
|
|
|
7
|
|
|
use React\EventLoop\LoopInterface; |
|
8
|
|
|
use React\EventLoop\TimerInterface; |
|
9
|
|
|
use WyriHaximus\Metrics\Label; |
|
10
|
|
|
use WyriHaximus\Metrics\Registry; |
|
11
|
|
|
use WyriHaximus\Metrics\Registry\Counters; |
|
12
|
|
|
use WyriHaximus\Metrics\Registry\Gauges; |
|
13
|
|
|
|
|
14
|
|
|
use function spl_object_id; |
|
15
|
|
|
|
|
16
|
|
|
/** @psalm-suppress UnusedVariable */ |
|
17
|
|
|
final class LoopDecorator implements LoopInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var array<int, array<int, callable>> */ |
|
20
|
|
|
private array $signalListeners = []; |
|
21
|
|
|
|
|
22
|
|
|
private Gauges $streams; |
|
23
|
|
|
private Counters $streamTicks; |
|
24
|
|
|
private Gauges $timers; |
|
25
|
|
|
private Counters $timerTicks; |
|
26
|
|
|
private Gauges $futureTicks; |
|
27
|
|
|
private Counters $futureTickTicks; |
|
28
|
|
|
private Gauges $signals; |
|
29
|
|
|
private Counters $signalTicks; |
|
30
|
|
|
|
|
31
|
|
|
public function __construct(private LoopInterface $loop, private Registry $registry) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->streams = $this->registry->gauge('react_event_loop_streams', 'Active streams', new Label\Name('kind')); |
|
34
|
|
|
$this->streamTicks = $this->registry->counter('react_event_loop_stream_ticks', 'Stream calls occurred', new Label\Name('kind')); |
|
35
|
|
|
$this->timers = $this->registry->gauge('react_event_loop_timers', 'Active timers', new Label\Name('kind')); |
|
36
|
|
|
$this->timerTicks = $this->registry->counter('react_event_loop_timer_ticks', 'Timer calls occurred', new Label\Name('kind')); |
|
37
|
|
|
$this->futureTicks = $this->registry->gauge('react_event_loop_future_ticks', 'Queued future ticks'); |
|
38
|
|
|
$this->futureTickTicks = $this->registry->counter('react_event_loop_future_tick_ticks', 'Ticks calls occurred'); |
|
39
|
|
|
$this->signals = $this->registry->gauge('react_event_loop_signals', 'Active signal listeners', new Label\Name('signal')); |
|
40
|
|
|
$this->signalTicks = $this->registry->counter('react_event_loop_signal_ticks', 'The number of calls occurred when a signal is caught', new Label\Name('signal')); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* {@inheritDoc} |
|
45
|
|
|
*/ |
|
46
|
|
|
public function addReadStream($stream, $listener): void |
|
47
|
|
|
{ |
|
48
|
|
|
$this->streams->gauge(new Label('kind', 'read'))->incr(); |
|
49
|
|
|
/** @psalm-suppress MissingClosureParamType */ |
|
50
|
|
|
$this->loop->addReadStream($stream, function ($stream) use ($listener): void { |
|
51
|
|
|
$this->streamTicks->counter(new Label('kind', 'read'))->incr(); |
|
52
|
|
|
$listener($stream, $this); |
|
53
|
|
|
}); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* {@inheritDoc} |
|
58
|
|
|
*/ |
|
59
|
|
|
public function addWriteStream($stream, $listener): void |
|
60
|
|
|
{ |
|
61
|
|
|
$this->streams->gauge(new Label('kind', 'write'))->incr(); |
|
62
|
|
|
/** @psalm-suppress MissingClosureParamType */ |
|
63
|
|
|
$this->loop->addWriteStream($stream, function ($stream) use ($listener): void { |
|
64
|
|
|
$this->streamTicks->counter(new Label('kind', 'write'))->incr(); |
|
65
|
|
|
$listener($stream, $this); |
|
66
|
|
|
}); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* {@inheritDoc} |
|
71
|
|
|
*/ |
|
72
|
|
|
public function removeReadStream($stream): void |
|
73
|
|
|
{ |
|
74
|
|
|
$this->streams->gauge(new Label('kind', 'read'))->dcr(); |
|
75
|
|
|
$this->loop->removeReadStream($stream); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* {@inheritDoc} |
|
80
|
|
|
*/ |
|
81
|
|
|
public function removeWriteStream($stream): void |
|
82
|
|
|
{ |
|
83
|
|
|
$this->streams->gauge(new Label('kind', 'write'))->dcr(); |
|
84
|
|
|
$this->loop->removeWriteStream($stream); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* {@inheritDoc} |
|
89
|
|
|
*/ |
|
90
|
|
|
// phpcs:disable |
|
91
|
|
|
public function addTimer($interval, $callback) |
|
92
|
|
|
{ |
|
93
|
|
|
$loopTimer = null; |
|
94
|
|
|
$wrapper = function () use (&$loopTimer, $callback): void { |
|
95
|
|
|
$this->timers->gauge(new Label('kind', 'one-off'))->dcr(); |
|
96
|
|
|
$this->timerTicks->counter(new Label('kind', 'one-off'))->incr(); |
|
97
|
|
|
$callback($loopTimer); |
|
98
|
|
|
}; |
|
99
|
|
|
$this->timers->gauge(new Label('kind', 'one-off'))->incr(); |
|
100
|
|
|
|
|
101
|
|
|
return $this->loop->addTimer($interval, $wrapper); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* {@inheritDoc} |
|
106
|
|
|
*/ |
|
107
|
|
|
// phpcs:disable |
|
108
|
|
|
public function addPeriodicTimer($interval, $callback) |
|
109
|
|
|
{ |
|
110
|
|
|
$this->timers->gauge(new Label('kind', 'periodic'))->incr(); |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @psalm-suppress MixedAssignment |
|
114
|
|
|
* @psalm-suppress MixedReturnStatement |
|
115
|
|
|
* @psalm-suppress UndefinedVariable |
|
116
|
|
|
*/ |
|
117
|
|
|
return $loopTimer = $this->loop->addPeriodicTimer( |
|
|
|
|
|
|
118
|
|
|
$interval, |
|
119
|
|
|
function () use (&$loopTimer, $callback): void { |
|
120
|
|
|
$this->timerTicks->counter(new Label('kind', 'periodic'))->incr(); |
|
121
|
|
|
$callback($loopTimer); |
|
122
|
|
|
} |
|
123
|
|
|
); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* {@inheritDoc} |
|
128
|
|
|
*/ |
|
129
|
|
|
public function cancelTimer(TimerInterface $timer): void |
|
130
|
|
|
{ |
|
131
|
|
|
$this->timers->gauge(new Label('kind', $timer->isPeriodic() ? 'periodic' : 'one-off'))->dcr(); |
|
132
|
|
|
|
|
133
|
|
|
$this->loop->cancelTimer($timer); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* {@inheritDoc} |
|
138
|
|
|
*/ |
|
139
|
|
|
public function futureTick($listener): void |
|
140
|
|
|
{ |
|
141
|
|
|
$this->futureTicks->gauge()->incr(); |
|
142
|
|
|
|
|
143
|
|
|
$this->loop->futureTick(function () use ($listener): void { |
|
144
|
|
|
$this->futureTicks->gauge()->dcr(); |
|
145
|
|
|
$this->futureTickTicks->counter()->incr(); |
|
146
|
|
|
$listener($this); |
|
147
|
|
|
}); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
public function run(): void |
|
151
|
|
|
{ |
|
152
|
|
|
$this->loop->run(); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* {@inheritDoc} |
|
157
|
|
|
*/ |
|
158
|
|
|
public function stop(): void |
|
159
|
|
|
{ |
|
160
|
|
|
$this->loop->stop(); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* {@inheritDoc} |
|
165
|
|
|
*/ |
|
166
|
|
|
public function addSignal($signal, $listener): void |
|
167
|
|
|
{ |
|
168
|
|
|
/** |
|
169
|
|
|
* @psalm-suppress InvalidArgument |
|
170
|
|
|
* @phpstan-ignore-next-line |
|
171
|
|
|
*/ |
|
172
|
|
|
$listenerId = spl_object_id($listener); |
|
173
|
|
|
/** @psalm-suppress MissingClosureParamType */ |
|
174
|
|
|
$wrapper = function ($signal) use ($listener): void { |
|
175
|
|
|
$this->signalTicks->counter(new Label('signal', (string)$signal))->incr(); |
|
176
|
|
|
$listener($signal); |
|
177
|
|
|
}; |
|
178
|
|
|
$this->signalListeners[$signal][$listenerId] = $wrapper; |
|
179
|
|
|
$this->signals->gauge(new Label('signal', (string)$signal))->incr(); |
|
180
|
|
|
$this->loop->addSignal($signal, $wrapper); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* {@inheritDoc} |
|
185
|
|
|
*/ |
|
186
|
|
|
public function removeSignal($signal, $listener): void |
|
187
|
|
|
{ |
|
188
|
|
|
/** |
|
189
|
|
|
* @psalm-suppress InvalidArgument |
|
190
|
|
|
* @phpstan-ignore-next-line |
|
191
|
|
|
*/ |
|
192
|
|
|
$listenerId = spl_object_id($listener); |
|
193
|
|
|
$wrapper = $this->signalListeners[$signal][$listenerId]; |
|
194
|
|
|
unset($this->signalListeners[$signal][$listenerId]); |
|
195
|
|
|
$this->signals->gauge(new Label('signal', (string)$signal))->dcr(); |
|
196
|
|
|
$this->loop->removeSignal($signal, $wrapper); |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|