Skip to content

Commit b9dfeca

Browse files
committed
Using separate connection counts for global & local.
1 parent 4e11355 commit b9dfeca

File tree

11 files changed

+64
-17
lines changed

11 files changed

+64
-17
lines changed

src/PubSub/Drivers/LocalClient.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,20 @@ public function channelMemberCounts($appId, array $channelNames): PromiseInterfa
164164
* Get the amount of unique connections.
165165
*
166166
* @param mixed $appId
167+
* @return null|int
168+
*/
169+
public function getLocalConnectionsCount($appId)
170+
{
171+
return null;
172+
}
173+
174+
/**
175+
* Get the amount of connections aggregated on multiple instances.
176+
*
177+
* @param mixed $appId
167178
* @return null|int|\React\Promise\PromiseInterface
168179
*/
169-
public function appConnectionsCount($appId)
180+
public function getGlobalConnectionsCount($appId)
170181
{
171182
return null;
172183
}

src/PubSub/Drivers/RedisClient.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,13 +292,22 @@ public function channelMemberCounts($appId, array $channelNames): PromiseInterfa
292292
* Get the amount of unique connections.
293293
*
294294
* @param mixed $appId
295-
* @return null|int|\React\Promise\PromiseInterface
295+
* @return null|int
296296
*/
297-
public function appConnectionsCount($appId)
297+
public function getLocalConnectionsCount($appId)
298298
{
299-
// Use the in-built Redis manager to avoid async run.
299+
return null;
300+
}
300301

301-
return $this->publishClient->hget($this->getTopicName($appId), 'connections') ?: 0;
302+
/**
303+
* Get the amount of connections aggregated on multiple instances.
304+
*
305+
* @param mixed $appId
306+
* @return null|int|\React\Promise\PromiseInterface
307+
*/
308+
public function getGlobalConnectionsCount($appId)
309+
{
310+
return $this->publishClient->hget($this->getTopicName($appId), 'connections');
302311
}
303312

304313
/**

src/PubSub/ReplicationInterface.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,15 @@ public function channelMemberCounts($appId, array $channelNames): PromiseInterfa
106106
* Get the amount of unique connections.
107107
*
108108
* @param mixed $appId
109+
* @return null|int
110+
*/
111+
public function getLocalConnectionsCount($appId);
112+
113+
/**
114+
* Get the amount of connections aggregated on multiple instances.
115+
*
116+
* @param mixed $appId
109117
* @return null|int|\React\Promise\PromiseInterface
110118
*/
111-
public function appConnectionsCount($appId);
119+
public function getGlobalConnectionsCount($appId);
112120
}

src/Statistics/Logger/MemoryStatisticsLogger.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public function save()
105105

106106
$this->createRecord($statistic, $appId);
107107

108-
$currentConnectionCount = $this->channelManager->getConnectionCount($appId);
108+
$currentConnectionCount = $this->channelManager->getGlobalConnectionsCount($appId);
109109

110110
$statistic->reset($currentConnectionCount);
111111
}

src/Statistics/Logger/RedisStatisticsLogger.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public function save()
124124

125125
$this->createRecord($statistic, $appId);
126126

127-
$currentConnectionCount = $this->channelManager->getConnectionCount($appId);
127+
$currentConnectionCount = $this->channelManager->getGlobalConnectionsCount($appId);
128128

129129
$currentConnectionCount === 0
130130
? $this->resetAppTraces($appId)

src/WebSockets/Channels/ChannelManager.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,15 @@ public function getChannels($appId): array;
3838
* @param mixed $appId
3939
* @return int
4040
*/
41-
public function getConnectionCount($appId): int;
41+
public function getLocalConnectionsCount($appId): int;
42+
43+
/**
44+
* Get the connections count across multiple servers.
45+
*
46+
* @param mixed $appId
47+
* @return int
48+
*/
49+
public function getGlobalConnectionsCount($appId): int;
4250

4351
/**
4452
* Remove connection from all channels.

src/WebSockets/Channels/ChannelManagers/ArrayChannelManager.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function getChannels($appId): array
7373
* @param mixed $appId
7474
* @return int
7575
*/
76-
public function getConnectionCount($appId): int
76+
public function getLocalConnectionsCount($appId): int
7777
{
7878
return collect($this->getChannels($appId))
7979
->flatMap(function (Channel $channel) {
@@ -83,6 +83,17 @@ public function getConnectionCount($appId): int
8383
->count();
8484
}
8585

86+
/**
87+
* Get the connections count across multiple servers.
88+
*
89+
* @param mixed $appId
90+
* @return int
91+
*/
92+
public function getGlobalConnectionsCount($appId): int
93+
{
94+
return $this->getLocalConnectionsCount($appId);
95+
}
96+
8697
/**
8798
* Remove connection from all channels.
8899
*

src/WebSockets/Channels/ChannelManagers/RedisChannelManager.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ public function __construct()
2424
}
2525

2626
/**
27-
* Get the connections count on the app.
27+
* Get the connections count across multiple servers.
2828
*
2929
* @param mixed $appId
3030
* @return int
3131
*/
32-
public function getConnectionCount($appId): int
32+
public function getGlobalConnectionsCount($appId): int
3333
{
34-
return $this->replicator->appConnectionsCount($appId);
34+
return $this->replicator->getGlobalConnectionsCount($appId);
3535
}
3636
}

src/WebSockets/WebSocketHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ protected function verifyOrigin(ConnectionInterface $connection)
165165
protected function limitConcurrentConnections(ConnectionInterface $connection)
166166
{
167167
if (! is_null($capacity = $connection->app->capacity)) {
168-
$connectionsCount = $this->channelManager->getConnectionCount($connection->app->id);
168+
$connectionsCount = $this->channelManager->getGlobalConnectionsCount($connection->app->id);
169169

170170
if ($connectionsCount >= $capacity) {
171171
throw new ConnectionsOverCapacity();

tests/Mocks/FakeMemoryStatisticsLogger.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class FakeMemoryStatisticsLogger extends MemoryStatisticsLogger
1212
public function save()
1313
{
1414
foreach ($this->statistics as $appId => $statistic) {
15-
$currentConnectionCount = $this->channelManager->getConnectionCount($appId);
15+
$currentConnectionCount = $this->channelManager->getLocalConnectionsCount($appId);
1616
$statistic->reset($currentConnectionCount);
1717
}
1818
}

tests/Statistics/Logger/StatisticsLoggerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function it_counts_connections_on_redis_replication()
4949

5050
StatisticsLogger::save();
5151

52-
$this->assertEquals(3, StatisticsLogger::getForAppId(1234)['peak_connection_count']);
52+
$this->assertEquals(2, StatisticsLogger::getForAppId(1234)['peak_connection_count']);
5353
}
5454

5555
/** @test */
@@ -93,7 +93,7 @@ public function it_counts_unique_connections_no_channel_subscriptions_on_redis()
9393

9494
StatisticsLogger::save();
9595

96-
$this->assertEquals(3, StatisticsLogger::getForAppId(1234)['peak_connection_count']);
96+
$this->assertEquals(1, StatisticsLogger::getForAppId(1234)['peak_connection_count']);
9797
}
9898

9999
/** @test */

0 commit comments

Comments
 (0)