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