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($this->channelManager->getRedisKey($appId, null, ['stats']), 'websocket_messages_count', 1); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Handle the incoming API message. |
64
|
|
|
* |
65
|
|
|
* @param string|int $appId |
66
|
|
|
* @return void |
67
|
|
|
*/ |
68
|
|
|
public function apiMessage($appId) |
69
|
|
|
{ |
70
|
|
|
$this->ensureAppIsInSet($appId) |
71
|
|
|
->hincrby($this->channelManager->getRedisKey($appId, null, ['stats']), 'api_messages_count', 1); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Handle the new conection. |
76
|
|
|
* |
77
|
|
|
* @param string|int $appId |
78
|
|
|
* @return void |
79
|
|
|
*/ |
80
|
|
View Code Duplication |
public function connection($appId) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
// Increment the current connections count by 1. |
83
|
|
|
$this->ensureAppIsInSet($appId) |
84
|
|
|
->hincrby( |
85
|
|
|
$this->channelManager->getRedisKey($appId, null, ['stats']), |
|
|
|
|
86
|
|
|
'current_connections_count', 1 |
87
|
|
|
) |
88
|
|
|
->then(function ($currentConnectionsCount) use ($appId) { |
89
|
|
|
// Get the peak connections count from Redis. |
90
|
|
|
$this->channelManager |
|
|
|
|
91
|
|
|
->getPublishClient() |
92
|
|
|
->hget( |
93
|
|
|
$this->channelManager->getRedisKey($appId, null, ['stats']), |
|
|
|
|
94
|
|
|
'peak_connections_count' |
95
|
|
|
) |
96
|
|
|
->then(function ($currentPeakConnectionCount) use ($currentConnectionsCount, $appId) { |
97
|
|
|
// Extract the greatest number between the current peak connection count |
98
|
|
|
// and the current connection number. |
99
|
|
|
$peakConnectionsCount = is_null($currentPeakConnectionCount) |
100
|
|
|
? $currentConnectionsCount |
101
|
|
|
: max($currentPeakConnectionCount, $currentConnectionsCount); |
102
|
|
|
|
103
|
|
|
// Then set it to the database. |
104
|
|
|
$this->channelManager |
|
|
|
|
105
|
|
|
->getPublishClient() |
106
|
|
|
->hset( |
107
|
|
|
$this->channelManager->getRedisKey($appId, null, ['stats']), |
|
|
|
|
108
|
|
|
'peak_connections_count', $peakConnectionsCount |
109
|
|
|
); |
110
|
|
|
}); |
111
|
|
|
}); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Handle disconnections. |
116
|
|
|
* |
117
|
|
|
* @param string|int $appId |
118
|
|
|
* @return void |
119
|
|
|
*/ |
120
|
|
View Code Duplication |
public function disconnection($appId) |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
// Decrement the current connections count by 1. |
123
|
|
|
$this->ensureAppIsInSet($appId) |
124
|
|
|
->hincrby($this->channelManager->getRedisKey($appId, null, ['stats']), 'current_connections_count', -1) |
|
|
|
|
125
|
|
|
->then(function ($currentConnectionsCount) use ($appId) { |
126
|
|
|
// Get the peak connections count from Redis. |
127
|
|
|
$this->channelManager |
|
|
|
|
128
|
|
|
->getPublishClient() |
129
|
|
|
->hget($this->channelManager->getRedisKey($appId, null, ['stats']), 'peak_connections_count') |
|
|
|
|
130
|
|
|
->then(function ($currentPeakConnectionCount) use ($currentConnectionsCount, $appId) { |
131
|
|
|
// Extract the greatest number between the current peak connection count |
132
|
|
|
// and the current connection number. |
133
|
|
|
$peakConnectionsCount = is_null($currentPeakConnectionCount) |
134
|
|
|
? $currentConnectionsCount |
135
|
|
|
: max($currentPeakConnectionCount, $currentConnectionsCount); |
136
|
|
|
|
137
|
|
|
// Then set it to the database. |
138
|
|
|
$this->channelManager |
|
|
|
|
139
|
|
|
->getPublishClient() |
140
|
|
|
->hset( |
141
|
|
|
$this->channelManager->getRedisKey($appId, null, ['stats']), |
|
|
|
|
142
|
|
|
'peak_connections_count', $peakConnectionsCount |
143
|
|
|
); |
144
|
|
|
}); |
145
|
|
|
}); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Save all the stored statistics. |
150
|
|
|
* |
151
|
|
|
* @return void |
152
|
|
|
*/ |
153
|
|
|
public function save() |
154
|
|
|
{ |
155
|
|
|
$this->lock()->get(function () { |
156
|
|
|
$this->channelManager |
|
|
|
|
157
|
|
|
->getPublishClient() |
158
|
|
|
->smembers(static::$redisSetName) |
159
|
|
|
->then(function ($members) { |
160
|
|
|
foreach ($members as $appId) { |
161
|
|
|
$this->channelManager |
|
|
|
|
162
|
|
|
->getPublishClient() |
163
|
|
|
->hgetall($this->channelManager->getRedisKey($appId, null, ['stats'])) |
|
|
|
|
164
|
|
|
->then(function ($list) use ($appId) { |
165
|
|
|
if (! $list) { |
166
|
|
|
return; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
$statistic = $this->arrayToStatisticInstance( |
170
|
|
|
$appId, Helpers::redisListToArray($list) |
171
|
|
|
); |
172
|
|
|
|
173
|
|
|
$this->createRecord($statistic, $appId); |
174
|
|
|
|
175
|
|
|
$this->channelManager |
176
|
|
|
->getGlobalConnectionsCount($appId) |
177
|
|
|
->then(function ($currentConnectionsCount) use ($appId) { |
178
|
|
|
$currentConnectionsCount === 0 || is_null($currentConnectionsCount) |
179
|
|
|
? $this->resetAppTraces($appId) |
180
|
|
|
: $this->resetStatistics($appId, $currentConnectionsCount); |
181
|
|
|
}); |
182
|
|
|
}); |
183
|
|
|
} |
184
|
|
|
}); |
185
|
|
|
}); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Flush the stored statistics. |
190
|
|
|
* |
191
|
|
|
* @return void |
192
|
|
|
*/ |
193
|
|
|
public function flush() |
194
|
|
|
{ |
195
|
|
|
$this->getStatistics()->then(function ($statistics) { |
196
|
|
|
foreach ($statistics as $appId => $statistic) { |
197
|
|
|
$this->resetAppTraces($appId); |
198
|
|
|
} |
199
|
|
|
}); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Get the saved statistics. |
204
|
|
|
* |
205
|
|
|
* @return PromiseInterface[array] |
|
|
|
|
206
|
|
|
*/ |
207
|
|
|
public function getStatistics(): PromiseInterface |
208
|
|
|
{ |
209
|
|
|
return $this->channelManager |
|
|
|
|
210
|
|
|
->getPublishClient() |
211
|
|
|
->smembers(static::$redisSetName) |
212
|
|
|
->then(function ($members) { |
213
|
|
|
$appsWithStatistics = []; |
214
|
|
|
|
215
|
|
|
foreach ($members as $appId) { |
216
|
|
|
$this->channelManager |
|
|
|
|
217
|
|
|
->getPublishClient() |
218
|
|
|
->hgetall($this->channelManager->getRedisKey($appId, null, ['stats'])) |
|
|
|
|
219
|
|
|
->then(function ($list) use ($appId, &$appsWithStatistics) { |
220
|
|
|
$appsWithStatistics[$appId] = $this->arrayToStatisticInstance( |
221
|
|
|
$appId, Helpers::redisListToArray($list) |
222
|
|
|
); |
223
|
|
|
}); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
return $appsWithStatistics; |
227
|
|
|
}); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Get the saved statistics for an app. |
232
|
|
|
* |
233
|
|
|
* @param string|int $appId |
234
|
|
|
* @return PromiseInterface[\BeyondCode\LaravelWebSockets\Statistics\Statistic|null] |
|
|
|
|
235
|
|
|
*/ |
236
|
|
|
public function getAppStatistics($appId): PromiseInterface |
237
|
|
|
{ |
238
|
|
|
return $this->channelManager |
|
|
|
|
239
|
|
|
->getPublishClient() |
240
|
|
|
->hgetall($this->channelManager->getRedisKey($appId, null, ['stats'])) |
|
|
|
|
241
|
|
|
->then(function ($list) use ($appId) { |
242
|
|
|
return $this->arrayToStatisticInstance( |
243
|
|
|
$appId, Helpers::redisListToArray($list) |
244
|
|
|
); |
245
|
|
|
}); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Reset the statistics to a specific connection count. |
250
|
|
|
* |
251
|
|
|
* @param string|int $appId |
252
|
|
|
* @param int $currentConnectionCount |
253
|
|
|
* @return void |
254
|
|
|
*/ |
255
|
|
|
public function resetStatistics($appId, int $currentConnectionCount) |
256
|
|
|
{ |
257
|
|
|
$this->channelManager |
|
|
|
|
258
|
|
|
->getPublishClient() |
259
|
|
|
->hset( |
260
|
|
|
$this->channelManager->getRedisKey($appId, null, ['stats']), |
|
|
|
|
261
|
|
|
'current_connections_count', $currentConnectionCount |
262
|
|
|
); |
263
|
|
|
|
264
|
|
|
$this->channelManager |
|
|
|
|
265
|
|
|
->getPublishClient() |
266
|
|
|
->hset( |
267
|
|
|
$this->channelManager->getRedisKey($appId, null, ['stats']), |
|
|
|
|
268
|
|
|
'peak_connections_count', $currentConnectionCount |
269
|
|
|
); |
270
|
|
|
|
271
|
|
|
$this->channelManager |
|
|
|
|
272
|
|
|
->getPublishClient() |
273
|
|
|
->hset( |
274
|
|
|
$this->channelManager->getRedisKey($appId, null, ['stats']), |
|
|
|
|
275
|
|
|
'websocket_messages_count', 0 |
276
|
|
|
); |
277
|
|
|
|
278
|
|
|
$this->channelManager |
|
|
|
|
279
|
|
|
->getPublishClient() |
280
|
|
|
->hset( |
281
|
|
|
$this->channelManager->getRedisKey($appId, null, ['stats']), |
|
|
|
|
282
|
|
|
'api_messages_count', 0 |
283
|
|
|
); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Remove all app traces from the database if no connections have been set |
288
|
|
|
* in the meanwhile since last save. |
289
|
|
|
* |
290
|
|
|
* @param string|int $appId |
291
|
|
|
* @return void |
292
|
|
|
*/ |
293
|
|
|
public function resetAppTraces($appId) |
294
|
|
|
{ |
295
|
|
|
$this->channelManager |
|
|
|
|
296
|
|
|
->getPublishClient() |
297
|
|
|
->hdel( |
298
|
|
|
$this->channelManager->getRedisKey($appId, null, ['stats']), |
|
|
|
|
299
|
|
|
'current_connections_count' |
300
|
|
|
); |
301
|
|
|
|
302
|
|
|
$this->channelManager |
|
|
|
|
303
|
|
|
->getPublishClient() |
304
|
|
|
->hdel( |
305
|
|
|
$this->channelManager->getRedisKey($appId, null, ['stats']), |
|
|
|
|
306
|
|
|
'peak_connections_count' |
307
|
|
|
); |
308
|
|
|
|
309
|
|
|
$this->channelManager |
|
|
|
|
310
|
|
|
->getPublishClient() |
311
|
|
|
->hdel( |
312
|
|
|
$this->channelManager->getRedisKey($appId, null, ['stats']), |
|
|
|
|
313
|
|
|
'websocket_messages_count' |
314
|
|
|
); |
315
|
|
|
|
316
|
|
|
$this->channelManager |
|
|
|
|
317
|
|
|
->getPublishClient() |
318
|
|
|
->hdel( |
319
|
|
|
$this->channelManager->getRedisKey($appId, null, ['stats']), |
|
|
|
|
320
|
|
|
'api_messages_count' |
321
|
|
|
); |
322
|
|
|
|
323
|
|
|
$this->channelManager |
|
|
|
|
324
|
|
|
->getPublishClient() |
325
|
|
|
->srem(static::$redisSetName, $appId); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Ensure the app id is stored in the Redis database. |
330
|
|
|
* |
331
|
|
|
* @param string|int $appId |
332
|
|
|
* @return \Clue\React\Redis\Client |
333
|
|
|
*/ |
334
|
|
|
protected function ensureAppIsInSet($appId) |
335
|
|
|
{ |
336
|
|
|
$this->channelManager |
|
|
|
|
337
|
|
|
->getPublishClient() |
338
|
|
|
->sadd(static::$redisSetName, $appId); |
339
|
|
|
|
340
|
|
|
return $this->channelManager->getPublishClient(); |
|
|
|
|
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* Get a new RedisLock instance to avoid race conditions. |
345
|
|
|
* |
346
|
|
|
* @return \Illuminate\Cache\CacheLock |
347
|
|
|
*/ |
348
|
|
|
protected function lock() |
349
|
|
|
{ |
350
|
|
|
return new RedisLock($this->redis, static::$redisLockName, 0); |
|
|
|
|
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* Transform a key-value pair to a Statistic instance. |
355
|
|
|
* |
356
|
|
|
* @param string|int $appId |
357
|
|
|
* @param array $stats |
358
|
|
|
* @return \BeyondCode\LaravelWebSockets\Statistics\Statistic |
359
|
|
|
*/ |
360
|
|
|
protected function arrayToStatisticInstance($appId, array $stats) |
361
|
|
|
{ |
362
|
|
|
return Statistic::new($appId) |
363
|
|
|
->setCurrentConnectionsCount($stats['current_connections_count'] ?? 0) |
364
|
|
|
->setPeakConnectionsCount($stats['peak_connections_count'] ?? 0) |
365
|
|
|
->setWebSocketMessagesCount($stats['websocket_messages_count'] ?? 0) |
366
|
|
|
->setApiMessagesCount($stats['api_messages_count'] ?? 0); |
367
|
|
|
} |
368
|
|
|
} |
369
|
|
|
|
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.