1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BeyondCode\LaravelWebSockets\Statistics\Collectors; |
4
|
|
|
|
5
|
|
|
use BeyondCode\LaravelWebSockets\Helpers; |
6
|
|
|
use BeyondCode\LaravelWebSockets\Statistics\Statistic; |
7
|
|
|
use Illuminate\Cache\RedisLock; |
8
|
|
|
use Illuminate\Support\Facades\Redis; |
9
|
|
|
use React\Promise\PromiseInterface; |
10
|
|
|
|
11
|
|
|
class RedisCollector extends MemoryCollector |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* The Redis manager instance. |
15
|
|
|
* |
16
|
|
|
* @var \Illuminate\Redis\RedisManager |
17
|
|
|
*/ |
18
|
|
|
protected $redis; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The set name for the Redis storage. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected static $redisSetName = 'laravel-websockets:apps'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The lock name to use on Redis to avoid multiple |
29
|
|
|
* collector-to-store actions that may result |
30
|
|
|
* in multiple data points set to the store. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected static $redisLockName = 'laravel-websockets:collector:lock'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Initialize the logger. |
38
|
|
|
* |
39
|
|
|
* @return void |
|
|
|
|
40
|
|
|
*/ |
41
|
|
|
public function __construct() |
42
|
|
|
{ |
43
|
|
|
parent::__construct(); |
44
|
|
|
|
45
|
|
|
$this->redis = Redis::connection( |
|
|
|
|
46
|
|
|
config('websockets.replication.modes.redis.connection', 'default') |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Handle the incoming websocket message. |
52
|
|
|
* |
53
|
|
|
* @param string|int $appId |
54
|
|
|
* @return void |
55
|
|
|
*/ |
56
|
|
|
public function webSocketMessage($appId) |
57
|
|
|
{ |
58
|
|
|
$this->ensureAppIsInSet($appId) |
59
|
|
|
->hincrby( |
60
|
|
|
$this->channelManager->getRedisKey($appId, null, ['stats']), |
|
|
|
|
61
|
|
|
'websocket_messages_count', 1 |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Handle the incoming API message. |
67
|
|
|
* |
68
|
|
|
* @param string|int $appId |
69
|
|
|
* @return void |
70
|
|
|
*/ |
71
|
|
|
public function apiMessage($appId) |
72
|
|
|
{ |
73
|
|
|
$this->ensureAppIsInSet($appId) |
74
|
|
|
->hincrby( |
75
|
|
|
$this->channelManager->getRedisKey($appId, null, ['stats']), |
|
|
|
|
76
|
|
|
'api_messages_count', 1 |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Handle the new conection. |
82
|
|
|
* |
83
|
|
|
* @param string|int $appId |
84
|
|
|
* @return void |
85
|
|
|
*/ |
86
|
|
View Code Duplication |
public function connection($appId) |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
// Increment the current connections count by 1. |
89
|
|
|
$this->ensureAppIsInSet($appId) |
90
|
|
|
->hincrby( |
91
|
|
|
$this->channelManager->getRedisKey($appId, null, ['stats']), |
|
|
|
|
92
|
|
|
'current_connections_count', 1 |
93
|
|
|
) |
94
|
|
|
->then(function ($currentConnectionsCount) use ($appId) { |
95
|
|
|
// Get the peak connections count from Redis. |
96
|
|
|
$this->channelManager |
|
|
|
|
97
|
|
|
->getPublishClient() |
98
|
|
|
->hget( |
99
|
|
|
$this->channelManager->getRedisKey($appId, null, ['stats']), |
|
|
|
|
100
|
|
|
'peak_connections_count' |
101
|
|
|
) |
102
|
|
|
->then(function ($currentPeakConnectionCount) use ($currentConnectionsCount, $appId) { |
103
|
|
|
// Extract the greatest number between the current peak connection count |
104
|
|
|
// and the current connection number. |
105
|
|
|
$peakConnectionsCount = is_null($currentPeakConnectionCount) |
106
|
|
|
? $currentConnectionsCount |
107
|
|
|
: max($currentPeakConnectionCount, $currentConnectionsCount); |
108
|
|
|
|
109
|
|
|
// Then set it to the database. |
110
|
|
|
$this->channelManager |
|
|
|
|
111
|
|
|
->getPublishClient() |
112
|
|
|
->hset( |
113
|
|
|
$this->channelManager->getRedisKey($appId, null, ['stats']), |
|
|
|
|
114
|
|
|
'peak_connections_count', $peakConnectionsCount |
115
|
|
|
); |
116
|
|
|
}); |
117
|
|
|
}); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Handle disconnections. |
122
|
|
|
* |
123
|
|
|
* @param string|int $appId |
124
|
|
|
* @return void |
125
|
|
|
*/ |
126
|
|
View Code Duplication |
public function disconnection($appId) |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
// Decrement the current connections count by 1. |
129
|
|
|
$this->ensureAppIsInSet($appId) |
130
|
|
|
->hincrby( |
131
|
|
|
$this->channelManager->getRedisKey($appId, null, ['stats']), |
|
|
|
|
132
|
|
|
'current_connections_count', -1 |
133
|
|
|
) |
134
|
|
|
->then(function ($currentConnectionsCount) use ($appId) { |
135
|
|
|
// Get the peak connections count from Redis. |
136
|
|
|
$this->channelManager |
|
|
|
|
137
|
|
|
->getPublishClient() |
138
|
|
|
->hget( |
139
|
|
|
$this->channelManager->getRedisKey($appId, null, ['stats']), |
|
|
|
|
140
|
|
|
'peak_connections_count' |
141
|
|
|
) |
142
|
|
|
->then(function ($currentPeakConnectionCount) use ($currentConnectionsCount, $appId) { |
143
|
|
|
// Extract the greatest number between the current peak connection count |
144
|
|
|
// and the current connection number. |
145
|
|
|
$peakConnectionsCount = is_null($currentPeakConnectionCount) |
146
|
|
|
? $currentConnectionsCount |
147
|
|
|
: max($currentPeakConnectionCount, $currentConnectionsCount); |
148
|
|
|
|
149
|
|
|
// Then set it to the database. |
150
|
|
|
$this->channelManager |
|
|
|
|
151
|
|
|
->getPublishClient() |
152
|
|
|
->hset( |
153
|
|
|
$this->channelManager->getRedisKey($appId, null, ['stats']), |
|
|
|
|
154
|
|
|
'peak_connections_count', $peakConnectionsCount |
155
|
|
|
); |
156
|
|
|
}); |
157
|
|
|
}); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Save all the stored statistics. |
162
|
|
|
* |
163
|
|
|
* @return void |
164
|
|
|
*/ |
165
|
|
|
public function save() |
166
|
|
|
{ |
167
|
|
|
$this->lock()->get(function () { |
168
|
|
|
$this->channelManager |
|
|
|
|
169
|
|
|
->getPublishClient() |
170
|
|
|
->smembers(static::$redisSetName) |
171
|
|
|
->then(function ($members) { |
172
|
|
|
foreach ($members as $appId) { |
173
|
|
|
$this->channelManager |
|
|
|
|
174
|
|
|
->getPublishClient() |
175
|
|
|
->hgetall($this->channelManager->getRedisKey($appId, null, ['stats'])) |
|
|
|
|
176
|
|
|
->then(function ($list) use ($appId) { |
177
|
|
|
if (! $list) { |
178
|
|
|
return; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
$statistic = $this->arrayToStatisticInstance( |
182
|
|
|
$appId, Helpers::redisListToArray($list) |
183
|
|
|
); |
184
|
|
|
|
185
|
|
|
$this->createRecord($statistic, $appId); |
186
|
|
|
|
187
|
|
|
$this->channelManager |
188
|
|
|
->getGlobalConnectionsCount($appId) |
189
|
|
|
->then(function ($currentConnectionsCount) use ($appId) { |
190
|
|
|
$currentConnectionsCount === 0 || is_null($currentConnectionsCount) |
191
|
|
|
? $this->resetAppTraces($appId) |
192
|
|
|
: $this->resetStatistics($appId, $currentConnectionsCount); |
193
|
|
|
}); |
194
|
|
|
}); |
195
|
|
|
} |
196
|
|
|
}); |
197
|
|
|
}); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Flush the stored statistics. |
202
|
|
|
* |
203
|
|
|
* @return void |
204
|
|
|
*/ |
205
|
|
|
public function flush() |
206
|
|
|
{ |
207
|
|
|
$this->getStatistics()->then(function ($statistics) { |
208
|
|
|
foreach ($statistics as $appId => $statistic) { |
209
|
|
|
$this->resetAppTraces($appId); |
210
|
|
|
} |
211
|
|
|
}); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Get the saved statistics. |
216
|
|
|
* |
217
|
|
|
* @return PromiseInterface[array] |
|
|
|
|
218
|
|
|
*/ |
219
|
|
|
public function getStatistics(): PromiseInterface |
220
|
|
|
{ |
221
|
|
|
return $this->channelManager |
|
|
|
|
222
|
|
|
->getPublishClient() |
223
|
|
|
->smembers(static::$redisSetName) |
224
|
|
|
->then(function ($members) { |
225
|
|
|
$appsWithStatistics = []; |
226
|
|
|
|
227
|
|
|
foreach ($members as $appId) { |
228
|
|
|
$this->channelManager |
|
|
|
|
229
|
|
|
->getPublishClient() |
230
|
|
|
->hgetall($this->channelManager->getRedisKey($appId, null, ['stats'])) |
|
|
|
|
231
|
|
|
->then(function ($list) use ($appId, &$appsWithStatistics) { |
232
|
|
|
$appsWithStatistics[$appId] = $this->arrayToStatisticInstance( |
233
|
|
|
$appId, Helpers::redisListToArray($list) |
234
|
|
|
); |
235
|
|
|
}); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
return $appsWithStatistics; |
239
|
|
|
}); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Get the saved statistics for an app. |
244
|
|
|
* |
245
|
|
|
* @param string|int $appId |
246
|
|
|
* @return PromiseInterface[\BeyondCode\LaravelWebSockets\Statistics\Statistic|null] |
|
|
|
|
247
|
|
|
*/ |
248
|
|
|
public function getAppStatistics($appId): PromiseInterface |
249
|
|
|
{ |
250
|
|
|
return $this->channelManager |
|
|
|
|
251
|
|
|
->getPublishClient() |
252
|
|
|
->hgetall($this->channelManager->getRedisKey($appId, null, ['stats'])) |
|
|
|
|
253
|
|
|
->then(function ($list) use ($appId) { |
254
|
|
|
return $this->arrayToStatisticInstance( |
255
|
|
|
$appId, Helpers::redisListToArray($list) |
256
|
|
|
); |
257
|
|
|
}); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Reset the statistics to a specific connection count. |
262
|
|
|
* |
263
|
|
|
* @param string|int $appId |
264
|
|
|
* @param int $currentConnectionCount |
265
|
|
|
* @return void |
266
|
|
|
*/ |
267
|
|
|
public function resetStatistics($appId, int $currentConnectionCount) |
268
|
|
|
{ |
269
|
|
|
$this->channelManager |
|
|
|
|
270
|
|
|
->getPublishClient() |
271
|
|
|
->hset( |
272
|
|
|
$this->channelManager->getRedisKey($appId, null, ['stats']), |
|
|
|
|
273
|
|
|
'current_connections_count', $currentConnectionCount |
274
|
|
|
); |
275
|
|
|
|
276
|
|
|
$this->channelManager |
|
|
|
|
277
|
|
|
->getPublishClient() |
278
|
|
|
->hset( |
279
|
|
|
$this->channelManager->getRedisKey($appId, null, ['stats']), |
|
|
|
|
280
|
|
|
'peak_connections_count', $currentConnectionCount |
281
|
|
|
); |
282
|
|
|
|
283
|
|
|
$this->channelManager |
|
|
|
|
284
|
|
|
->getPublishClient() |
285
|
|
|
->hset( |
286
|
|
|
$this->channelManager->getRedisKey($appId, null, ['stats']), |
|
|
|
|
287
|
|
|
'websocket_messages_count', 0 |
288
|
|
|
); |
289
|
|
|
|
290
|
|
|
$this->channelManager |
|
|
|
|
291
|
|
|
->getPublishClient() |
292
|
|
|
->hset( |
293
|
|
|
$this->channelManager->getRedisKey($appId, null, ['stats']), |
|
|
|
|
294
|
|
|
'api_messages_count', 0 |
295
|
|
|
); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Remove all app traces from the database if no connections have been set |
300
|
|
|
* in the meanwhile since last save. |
301
|
|
|
* |
302
|
|
|
* @param string|int $appId |
303
|
|
|
* @return void |
304
|
|
|
*/ |
305
|
|
|
public function resetAppTraces($appId) |
306
|
|
|
{ |
307
|
|
|
$this->channelManager |
|
|
|
|
308
|
|
|
->getPublishClient() |
309
|
|
|
->hdel( |
310
|
|
|
$this->channelManager->getRedisKey($appId, null, ['stats']), |
|
|
|
|
311
|
|
|
'current_connections_count' |
312
|
|
|
); |
313
|
|
|
|
314
|
|
|
$this->channelManager |
|
|
|
|
315
|
|
|
->getPublishClient() |
316
|
|
|
->hdel( |
317
|
|
|
$this->channelManager->getRedisKey($appId, null, ['stats']), |
|
|
|
|
318
|
|
|
'peak_connections_count' |
319
|
|
|
); |
320
|
|
|
|
321
|
|
|
$this->channelManager |
|
|
|
|
322
|
|
|
->getPublishClient() |
323
|
|
|
->hdel( |
324
|
|
|
$this->channelManager->getRedisKey($appId, null, ['stats']), |
|
|
|
|
325
|
|
|
'websocket_messages_count' |
326
|
|
|
); |
327
|
|
|
|
328
|
|
|
$this->channelManager |
|
|
|
|
329
|
|
|
->getPublishClient() |
330
|
|
|
->hdel( |
331
|
|
|
$this->channelManager->getRedisKey($appId, null, ['stats']), |
|
|
|
|
332
|
|
|
'api_messages_count' |
333
|
|
|
); |
334
|
|
|
|
335
|
|
|
$this->channelManager |
|
|
|
|
336
|
|
|
->getPublishClient() |
337
|
|
|
->srem(static::$redisSetName, $appId); |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* Ensure the app id is stored in the Redis database. |
342
|
|
|
* |
343
|
|
|
* @param string|int $appId |
344
|
|
|
* @return \Clue\React\Redis\Client |
345
|
|
|
*/ |
346
|
|
|
protected function ensureAppIsInSet($appId) |
347
|
|
|
{ |
348
|
|
|
$this->channelManager |
|
|
|
|
349
|
|
|
->getPublishClient() |
350
|
|
|
->sadd(static::$redisSetName, $appId); |
351
|
|
|
|
352
|
|
|
return $this->channelManager->getPublishClient(); |
|
|
|
|
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* Get a new RedisLock instance to avoid race conditions. |
357
|
|
|
* |
358
|
|
|
* @return \Illuminate\Cache\CacheLock |
359
|
|
|
*/ |
360
|
|
|
protected function lock() |
361
|
|
|
{ |
362
|
|
|
return new RedisLock($this->redis, static::$redisLockName, 0); |
|
|
|
|
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* Transform a key-value pair to a Statistic instance. |
367
|
|
|
* |
368
|
|
|
* @param string|int $appId |
369
|
|
|
* @param array $stats |
370
|
|
|
* @return \BeyondCode\LaravelWebSockets\Statistics\Statistic |
371
|
|
|
*/ |
372
|
|
|
protected function arrayToStatisticInstance($appId, array $stats) |
373
|
|
|
{ |
374
|
|
|
return Statistic::new($appId) |
375
|
|
|
->setCurrentConnectionsCount($stats['current_connections_count'] ?? 0) |
376
|
|
|
->setPeakConnectionsCount($stats['peak_connections_count'] ?? 0) |
377
|
|
|
->setWebSocketMessagesCount($stats['websocket_messages_count'] ?? 0) |
378
|
|
|
->setApiMessagesCount($stats['api_messages_count'] ?? 0); |
379
|
|
|
} |
380
|
|
|
} |
381
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.