Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Commit 23e8b3d

Browse files
committed
Fixed issues with connections being closed within 10 seconds
1 parent 16ff2aa commit 23e8b3d

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

src/ChannelManagers/RedisChannelManager.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public function subscribeToChannel(ConnectionInterface $connection, string $chan
160160
{
161161
$this->subscribeToTopic($connection->app->id, $channelName);
162162

163-
$this->addConnectionToSet($connection);
163+
$this->addConnectionToSet($connection, Carbon::now());
164164

165165
$this->addChannelToSet(
166166
$connection->app->id, $channelName
@@ -416,7 +416,7 @@ public function getMemberSockets($userId, $appId, $channelName): PromiseInterfac
416416
public function connectionPonged(ConnectionInterface $connection): bool
417417
{
418418
// This will update the score with the current timestamp.
419-
$this->addConnectionToSet($connection);
419+
$this->addConnectionToSet($connection, Carbon::now());
420420

421421
return parent::connectionPonged($connection);
422422
}
@@ -431,9 +431,7 @@ public function removeObsoleteConnections(): bool
431431
$this->lock()->get(function () {
432432
$this->getConnectionsFromSet(0, now()->subMinutes(2)->format('U'))
433433
->then(function ($connections) {
434-
foreach ($connections as $connection => $score) {
435-
[$appId, $socketId] = explode(':', $connection);
436-
434+
foreach ($connections as $appId => $socketId) {
437435
$this->unsubscribeFromAllChannels(
438436
$this->fakeConnectionForApp($appId, $socketId)
439437
);
@@ -571,9 +569,11 @@ public function decrementSubscriptionsCount($appId, string $channel = null, int
571569
*/
572570
public function addConnectionToSet(ConnectionInterface $connection, $moment = null)
573571
{
572+
$moment = $moment ? Carbon::parse($moment) : Carbon::now();
573+
574574
$this->publishClient->zadd(
575575
$this->getRedisKey(null, null, ['sockets']),
576-
Carbon::parse($moment)->format('U'), "{$connection->app->id}:{$connection->socketId}"
576+
$moment->format('U'), "{$connection->app->id}:{$connection->socketId}"
577577
);
578578
}
579579

@@ -597,16 +597,26 @@ public function removeConnectionFromSet(ConnectionInterface $connection)
597597
*
598598
* @param int $start
599599
* @param int $stop
600+
* @param bool $strict
600601
* @return PromiseInterface
601602
*/
602-
public function getConnectionsFromSet(int $start = 0, int $stop = 0)
603+
public function getConnectionsFromSet(int $start = 0, int $stop = 0, bool $strict = true)
603604
{
604-
return $this->publishClient->zrange(
605+
if ($strict) {
606+
$start = "({$start}";
607+
$stop = "({$stop}";
608+
}
609+
610+
return $this->publishClient->zrangebyscore(
605611
$this->getRedisKey(null, null, ['sockets']),
606-
$start, $stop, 'withscores'
612+
$start, $stop
607613
)
608614
->then(function ($list) {
609-
return Helpers::redisListToArray($list);
615+
return collect($list)->mapWithKeys(function ($appWithSocket) {
616+
[$appId, $socketId] = explode(':', $appWithSocket);
617+
618+
return [$appId => $socketId];
619+
})->toArray();
610620
});
611621
}
612622

tests/Mocks/PromiseResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ public function then(callable $onFulfilled = null, callable $onRejected = null,
5252
$result = call_user_func($onFulfilled, $result);
5353

5454
return $result instanceof PromiseInterface
55-
? $result
56-
: new FulfilledPromise($result);
55+
? new self($result, $this->loop)
56+
: new self(new FulfilledPromise($result), $this->loop);
5757
}
5858

5959
/**

0 commit comments

Comments
 (0)