This repository has been archived by the owner on Feb 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 639
/
Copy pathRedisChannelManager.php
840 lines (750 loc) · 26.1 KB
/
RedisChannelManager.php
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
<?php
namespace BeyondCode\LaravelWebSockets\ChannelManagers;
use BeyondCode\LaravelWebSockets\Cache\RedisLock;
use BeyondCode\LaravelWebSockets\Channels\Channel;
use BeyondCode\LaravelWebSockets\DashboardLogger;
use BeyondCode\LaravelWebSockets\Helpers;
use BeyondCode\LaravelWebSockets\Server\MockableConnection;
use Carbon\Carbon;
use Clue\React\Redis\Client;
use Clue\React\Redis\Factory;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Str;
use Ratchet\ConnectionInterface;
use React\EventLoop\LoopInterface;
use function React\Promise\all;
use React\Promise\PromiseInterface;
use stdClass;
class RedisChannelManager extends LocalChannelManager
{
/**
* The running loop.
*
* @var LoopInterface
*/
protected $loop;
/**
* The pub client.
*
* @var Client
*/
protected $publishClient;
/**
* The sub client.
*
* @var Client
*/
protected $subscribeClient;
/**
* The Redis manager instance.
*
* @var \Illuminate\Redis\RedisManager
*/
protected $redis;
/**
* Create a new channel manager instance.
*
* @param LoopInterface $loop
* @param string|null $factoryClass
* @return void
*/
public function __construct(LoopInterface $loop, $factoryClass = null)
{
parent::__construct($loop, $factoryClass);
$this->loop = $loop;
$this->redis = Redis::connection(
config('websockets.replication.modes.redis.connection', 'default')
);
$connectionUri = $this->getConnectionUri();
$factoryClass = $factoryClass ?: Factory::class;
$factory = new $factoryClass($this->loop);
$this->publishClient = $factory->createLazyClient($connectionUri);
$this->subscribeClient = $factory->createLazyClient($connectionUri);
$this->subscribeClient->on('message', function ($channel, $payload) {
$this->onMessage($channel, $payload);
});
}
/**
* Get all channels for a specific app
* across multiple servers.
*
* @param string|int $appId
* @return \React\Promise\PromiseInterface[array]
*/
public function getGlobalChannels($appId): PromiseInterface
{
return $this->publishClient->smembers(
$this->getChannelsRedisHash($appId)
);
}
/**
* Remove connection from all channels.
*
* @param \Ratchet\ConnectionInterface $connection
* @return PromiseInterface[bool]
*/
public function unsubscribeFromAllChannels(ConnectionInterface $connection): PromiseInterface
{
return $this->getGlobalChannels($connection->app->id)
->then(function ($channels) use ($connection) {
$promises = [];
foreach ($channels as $channel) {
$promises[] = $this->unsubscribeFromChannel($connection, $channel, new stdClass);
}
return all($promises);
})
->then(function () use ($connection) {
return parent::unsubscribeFromAllChannels($connection);
});
}
/**
* Subscribe the connection to a specific channel.
*
* @param \Ratchet\ConnectionInterface $connection
* @param string $channelName
* @param stdClass $payload
* @return PromiseInterface[bool]
*/
public function subscribeToChannel(ConnectionInterface $connection, string $channelName, stdClass $payload): PromiseInterface
{
return $this->subscribeToTopic($connection->app->id, $channelName)
->then(function () use ($connection) {
return $this->addConnectionToSet($connection, Carbon::now());
})
->then(function () use ($connection, $channelName) {
return $this->addChannelToSet($connection->app->id, $channelName);
})
->then(function () use ($connection, $channelName) {
return $this->incrementSubscriptionsCount($connection->app->id, $channelName, 1);
})
->then(function () use ($connection, $channelName, $payload) {
return parent::subscribeToChannel($connection, $channelName, $payload);
});
}
/**
* Unsubscribe the connection from the channel.
*
* @param \Ratchet\ConnectionInterface $connection
* @param string $channelName
* @param stdClass $payload
* @return PromiseInterface[bool]
*/
public function unsubscribeFromChannel(ConnectionInterface $connection, string $channelName, stdClass $payload): PromiseInterface
{
return $this->getGlobalConnectionsCount($connection->app->id, $channelName)
->then(function ($count) use ($connection, $channelName) {
if ($count === 0) {
// Make sure to not stay subscribed to the PubSub topic
// if there are no connections.
return $this->unsubscribeFromTopic($connection->app->id, $channelName);
}
return Helpers::createFulfilledPromise(null);
})
->then(function () use ($connection, $channelName) {
return $this->decrementSubscriptionsCount($connection->app->id, $channelName)
->then(function ($count) use ($connection, $channelName) {
// If the total connections count gets to 0 after unsubscribe,
// try again to check & unsubscribe from the PubSub topic if needed.
if ($count < 1) {
$promises = [];
$promises[] = $this->unsubscribeFromTopic($connection->app->id, $channelName);
$promises[] = $this->removeChannelFromSet($connection->app->id, $channelName);
return all($promises);
}
});
})
->then(function () use ($connection) {
return $this->removeConnectionFromSet($connection);
})
->then(function () use ($connection, $channelName, $payload) {
return parent::unsubscribeFromChannel($connection, $channelName, $payload);
});
}
/**
* Subscribe the connection to a specific channel, returning
* a promise containing the amount of connections.
*
* @param string|int $appId
* @return PromiseInterface[int]
*/
public function subscribeToApp($appId): PromiseInterface
{
return $this->subscribeToTopic($appId)
->then(function () use ($appId) {
return $this->incrementSubscriptionsCount($appId);
});
}
/**
* Unsubscribe the connection from the channel, returning
* a promise containing the amount of connections after decrement.
*
* @param string|int $appId
* @return PromiseInterface[int]
*/
public function unsubscribeFromApp($appId): PromiseInterface
{
return $this->unsubscribeFromTopic($appId)
->then(function () use ($appId) {
return $this->decrementSubscriptionsCount($appId);
});
}
/**
* Get the connections count
* across multiple servers.
*
* @param string|int $appId
* @param string|null $channelName
* @return PromiseInterface[int]
*/
public function getGlobalConnectionsCount($appId, string $channelName = null): PromiseInterface
{
return $this->publishClient
->hget($this->getStatsRedisHash($appId, $channelName), 'connections')
->then(function ($count) {
return is_null($count) ? 0 : (int) $count;
});
}
/**
* Broadcast the message across multiple servers.
*
* @param string|int $appId
* @param string|null $socketId
* @param string $channel
* @param stdClass $payload
* @param string|null $serverId
* @return PromiseInterface[bool]
*/
public function broadcastAcrossServers($appId, ?string $socketId, string $channel, stdClass $payload, string $serverId = null): PromiseInterface
{
$payload->appId = $appId;
$payload->socketId = $socketId;
$payload->serverId = $serverId ?: $this->getServerId();
return $this->publishClient
->publish($this->getRedisTopicName($appId, $channel), json_encode($payload))
->then(function () use ($appId, $socketId, $channel, $payload, $serverId) {
return parent::broadcastAcrossServers($appId, $socketId, $channel, $payload, $serverId);
});
}
/**
* Handle the user when it joined a presence channel.
*
* @param \Ratchet\ConnectionInterface $connection
* @param stdClass $user
* @param string $channel
* @param stdClass $payload
* @return PromiseInterface
*/
public function userJoinedPresenceChannel(ConnectionInterface $connection, stdClass $user, string $channel, stdClass $payload): PromiseInterface
{
return $this->storeUserData($connection->app->id, $channel, $connection->socketId, json_encode($user))
->then(function () use ($connection, $channel, $user) {
return $this->addUserSocket($connection->app->id, $channel, $user, $connection->socketId);
})
->then(function () use ($connection, $user, $channel, $payload) {
return parent::userJoinedPresenceChannel($connection, $user, $channel, $payload);
});
}
/**
* Handle the user when it left a presence channel.
*
* @param \Ratchet\ConnectionInterface $connection
* @param stdClass $user
* @param string $channel
* @param stdClass $payload
* @return PromiseInterface[bool]
*/
public function userLeftPresenceChannel(ConnectionInterface $connection, stdClass $user, string $channel): PromiseInterface
{
return $this->removeUserData($connection->app->id, $channel, $connection->socketId)
->then(function () use ($connection, $channel, $user) {
return $this->removeUserSocket($connection->app->id, $channel, $user, $connection->socketId);
})
->then(function () use ($connection, $user, $channel) {
return parent::userLeftPresenceChannel($connection, $user, $channel);
});
}
/**
* Get the presence channel members.
*
* @param string|int $appId
* @param string $channel
* @return \React\Promise\PromiseInterface[array]
*/
public function getChannelMembers($appId, string $channel): PromiseInterface
{
return $this->publishClient
->hgetall($this->getUsersRedisHash($appId, $channel))
->then(function ($list) {
return collect(Helpers::redisListToArray($list))->map(function ($user) {
return json_decode($user);
})->unique('user_id')->toArray();
});
}
/**
* Get a member from a presence channel based on connection.
*
* @param \Ratchet\ConnectionInterface $connection
* @param string $channel
* @return \React\Promise\PromiseInterface[null|array]
*/
public function getChannelMember(ConnectionInterface $connection, string $channel): PromiseInterface
{
return $this->publishClient->hget(
$this->getUsersRedisHash($connection->app->id, $channel), $connection->socketId
);
}
/**
* Get the presence channels total members count.
*
* @param string|int $appId
* @param array $channelNames
* @return \React\Promise\PromiseInterface[array]
*/
public function getChannelsMembersCount($appId, array $channelNames): PromiseInterface
{
$this->publishClient->multi();
foreach ($channelNames as $channel) {
$this->publishClient->hlen(
$this->getUsersRedisHash($appId, $channel)
);
}
return $this->publishClient->exec()
->then(function ($data) use ($channelNames) {
return array_combine($channelNames, $data);
});
}
/**
* Get the socket IDs for a presence channel member.
*
* @param string|int $userId
* @param string|int $appId
* @param string $channelName
* @return \React\Promise\PromiseInterface[array]
*/
public function getMemberSockets($userId, $appId, $channelName): PromiseInterface
{
return $this->publishClient->smembers(
$this->getUserSocketsRedisHash($appId, $channelName, $userId)
);
}
/**
* Keep tracking the connections availability when they pong.
*
* @param \Ratchet\ConnectionInterface $connection
* @return PromiseInterface[bool]
*/
public function connectionPonged(ConnectionInterface $connection): PromiseInterface
{
// This will update the score with the current timestamp.
return $this->addConnectionToSet($connection, Carbon::now())
->then(function () use ($connection) {
return parent::connectionPonged($connection);
});
}
/**
* Remove the obsolete connections that didn't ponged in a while.
*
* @return PromiseInterface[bool]
*/
public function removeObsoleteConnections(): PromiseInterface
{
return $this->lock()->get(function () {
return $this->getConnectionsFromSet(0, now()->subMinutes(2)->format('U'))
->then(function ($connections) {
$promises = [];
foreach ($connections as $socketId => $appId) {
$connection = $this->fakeConnectionForApp($appId, $socketId);
$promises[] = $this->unsubscribeFromAllChannels($connection);
}
return all($promises);
});
})->then(function () {
return parent::removeObsoleteConnections();
});
}
/**
* Handle a message received from Redis on a specific channel.
*
* @param string $redisChannel
* @param string $payload
* @return void
*/
public function onMessage(string $redisChannel, string $payload)
{
$payload = json_decode($payload);
if (isset($payload->serverId) && $this->getServerId() === $payload->serverId) {
return;
}
$payload->channel = Str::after($redisChannel, "{$payload->appId}:");
if (! $channel = $this->find($payload->appId, $payload->channel)) {
return;
}
$appId = $payload->appId ?? null;
$socketId = $payload->socketId ?? null;
$serverId = $payload->serverId ?? null;
DashboardLogger::log($appId, DashboardLogger::TYPE_REPLICATOR_MESSAGE_RECEIVED, [
'fromServerId' => $serverId,
'fromSocketId' => $socketId,
'receiverServerId' => $this->getServerId(),
'channel' => $channel,
'payload' => $payload,
]);
unset($payload->socketId);
unset($payload->serverId);
unset($payload->appId);
$channel->broadcastLocallyToEveryoneExcept($payload, $socketId, $appId);
}
/**
* Build the Redis connection URL from Laravel database config.
*
* @return string
*/
protected function getConnectionUri()
{
$name = config('websockets.replication.modes.redis.connection', 'default');
$config = config("database.redis.{$name}");
$host = $config['host'];
$port = $config['port'] ?: 6379;
$query = [];
if ($config['password']) {
$query['password'] = $config['password'];
}
if ($config['database']) {
$query['db'] = $config['database'];
}
$query = http_build_query($query);
return "redis://{$host}:{$port}".($query ? "?{$query}" : '');
}
/**
* Get the Subscribe client instance.
*
* @return Client
*/
public function getSubscribeClient()
{
return $this->subscribeClient;
}
/**
* Get the Publish client instance.
*
* @return Client
*/
public function getPublishClient()
{
return $this->publishClient;
}
/**
* Get the Redis client used by other classes.
*
* @return Client
*/
public function getRedisClient()
{
return $this->getPublishClient();
}
/**
* Increment the subscribed count number.
*
* @param string|int $appId
* @param string|null $channel
* @param int $increment
* @return PromiseInterface[int]
*/
public function incrementSubscriptionsCount($appId, string $channel = null, int $increment = 1): PromiseInterface
{
return $this->publishClient->hincrby(
$this->getStatsRedisHash($appId, $channel), 'connections', $increment
);
}
/**
* Decrement the subscribed count number.
*
* @param string|int $appId
* @param string|null $channel
* @param int $decrement
* @return PromiseInterface[int]
*/
public function decrementSubscriptionsCount($appId, string $channel = null, int $increment = 1): PromiseInterface
{
return $this->incrementSubscriptionsCount($appId, $channel, $increment * -1);
}
/**
* Add the connection to the sorted list.
*
* @param \Ratchet\ConnectionInterface $connection
* @param \DateTime|string|null $moment
* @return PromiseInterface
*/
public function addConnectionToSet(ConnectionInterface $connection, $moment = null): PromiseInterface
{
$moment = $moment ? Carbon::parse($moment) : Carbon::now();
return $this->publishClient->zadd(
$this->getSocketsRedisHash(),
$moment->format('U'), "{$connection->app->id}:{$connection->socketId}"
);
}
/**
* Remove the connection from the sorted list.
*
* @param \Ratchet\ConnectionInterface $connection
* @return PromiseInterface
*/
public function removeConnectionFromSet(ConnectionInterface $connection): PromiseInterface
{
return $this->publishClient->zrem(
$this->getSocketsRedisHash(),
"{$connection->app->id}:{$connection->socketId}"
);
}
/**
* Get the connections from the sorted list, with last
* connection between certain timestamps.
*
* @param int $start
* @param int $stop
* @param bool $strict
* @return PromiseInterface[array]
*/
public function getConnectionsFromSet(int $start = 0, int $stop = 0, bool $strict = true): PromiseInterface
{
if ($strict) {
$start = "({$start}";
$stop = "({$stop}";
}
return $this->publishClient
->zrangebyscore($this->getSocketsRedisHash(), $start, $stop)
->then(function ($list) {
return collect($list)->mapWithKeys(function ($appWithSocket) {
[$appId, $socketId] = explode(':', $appWithSocket);
return [$socketId => $appId];
})->toArray();
});
}
/**
* Add a channel to the set list.
*
* @param string|int $appId
* @param string $channel
* @return PromiseInterface
*/
public function addChannelToSet($appId, string $channel): PromiseInterface
{
return $this->publishClient->sadd(
$this->getChannelsRedisHash($appId), $channel
);
}
/**
* Remove a channel from the set list.
*
* @param string|int $appId
* @param string $channel
* @return PromiseInterface
*/
public function removeChannelFromSet($appId, string $channel): PromiseInterface
{
return $this->publishClient->srem(
$this->getChannelsRedisHash($appId), $channel
);
}
/**
* Set data for a topic. Might be used for the presence channels.
*
* @param string|int $appId
* @param string|null $channel
* @param string $key
* @param string $data
* @return PromiseInterface
*/
public function storeUserData($appId, string $channel = null, string $key, $data): PromiseInterface
{
return $this->publishClient->hset(
$this->getUsersRedisHash($appId, $channel), $key, $data
);
}
/**
* Remove data for a topic. Might be used for the presence channels.
*
* @param string|int $appId
* @param string|null $channel
* @param string $key
* @return PromiseInterface
*/
public function removeUserData($appId, string $channel = null, string $key): PromiseInterface
{
return $this->publishClient->hdel(
$this->getUsersRedisHash($appId, $channel), $key
);
}
/**
* Subscribe to the topic for the app, or app and channel.
*
* @param string|int $appId
* @param string|null $channel
* @return PromiseInterface
*/
public function subscribeToTopic($appId, string $channel = null): PromiseInterface
{
$topic = $this->getRedisTopicName($appId, $channel);
DashboardLogger::log($appId, DashboardLogger::TYPE_REPLICATOR_SUBSCRIBED, [
'serverId' => $this->getServerId(),
'pubsubTopic' => $topic,
]);
return $this->subscribeClient->subscribe($topic);
}
/**
* Unsubscribe from the topic for the app, or app and channel.
*
* @param string|int $appId
* @param string|null $channel
* @return PromiseInterface
*/
public function unsubscribeFromTopic($appId, string $channel = null): PromiseInterface
{
$topic = $this->getRedisTopicName($appId, $channel);
DashboardLogger::log($appId, DashboardLogger::TYPE_REPLICATOR_UNSUBSCRIBED, [
'serverId' => $this->getServerId(),
'pubsubTopic' => $topic,
]);
return $this->subscribeClient->unsubscribe($topic);
}
/**
* Add the Presence Channel's User's Socket ID to a list.
*
* @param string|int $appId
* @param string $channel
* @param stdClass $user
* @param string $socketId
* @return PromiseInterface
*/
protected function addUserSocket($appId, string $channel, stdClass $user, string $socketId): PromiseInterface
{
return $this->publishClient->sadd(
$this->getUserSocketsRedisHash($appId, $channel, $user->user_id), $socketId
);
}
/**
* Remove the Presence Channel's User's Socket ID from the list.
*
* @param string|int $appId
* @param string $channel
* @param stdClass $user
* @param string $socketId
* @return PromiseInterface
*/
protected function removeUserSocket($appId, string $channel, stdClass $user, string $socketId): PromiseInterface
{
return $this->publishClient->srem(
$this->getUserSocketsRedisHash($appId, $channel, $user->user_id), $socketId
);
}
/**
* Get the Redis Keyspace name to handle subscriptions
* and other key-value sets.
*
* @param string|int|null $appId
* @param string|null $channel
* @return string
*/
public function getRedisKey($appId = null, string $channel = null, array $suffixes = []): string
{
$prefix = config('database.redis.options.prefix', null);
$hash = "{$prefix}{$appId}";
if ($channel) {
$suffixes = array_merge([$channel], $suffixes);
}
$suffixes = implode(':', $suffixes);
if ($suffixes) {
$hash .= ":{$suffixes}";
}
return $hash;
}
/**
* Get the statistics Redis hash.
*
* @param string|int $appId
* @param string|null $channel
* @return string
*/
public function getStatsRedisHash($appId, string $channel = null): string
{
return $this->getRedisKey($appId, $channel, ['stats']);
}
/**
* Get the sockets Redis hash used to store all sockets ids.
*
* @return string
*/
public function getSocketsRedisHash(): string
{
return $this->getRedisKey(null, null, ['sockets']);
}
/**
* Get the channels Redis hash for a specific app id, used
* to store existing channels.
*
* @param string|int $appId
* @return string
*/
public function getChannelsRedisHash($appId): string
{
return $this->getRedisKey($appId, null, ['channels']);
}
/**
* Get the Redis hash for storing presence channels users.
*
* @param string|int $appId
* @param string|null $channel
* @return string
*/
public function getUsersRedisHash($appId, string $channel = null): string
{
return $this->getRedisKey($appId, $channel, ['users']);
}
/**
* Get the Redis hash for storing socket ids
* for a specific presence channels user.
*
* @param string|int $appId
* @param string|null $channel
* @param string|int|null $userId
* @return string
*/
public function getUserSocketsRedisHash($appId, string $channel = null, $userId = null): string
{
return $this->getRedisKey($appId, $channel, [$userId, 'userSockets']);
}
/**
* Get the Redis topic name for PubSub
* used to transfer info between servers.
*
* @param string|int $appId
* @param string|null $channel
* @return string
*/
public function getRedisTopicName($appId, string $channel = null): string
{
return $this->getRedisKey($appId, $channel);
}
/**
* Get a new RedisLock instance to avoid race conditions.
*
* @return RedisLock
*/
protected function lock()
{
return new RedisLock($this->publishClient, static::$lockName, 0);
}
/**
* Create a fake connection for app that will mimick a connection
* by app ID and Socket ID to be able to be passed to the methods
* that accepts a connection class.
*
* @param string|int $appId
* @param string $socketId
* @return ConnectionInterface
*/
public function fakeConnectionForApp($appId, string $socketId)
{
return new MockableConnection($appId, $socketId);
}
}