33namespace BeyondCode \LaravelWebSockets \Statistics \Logger ;
44
55use BeyondCode \LaravelWebSockets \Apps \App ;
6+ use BeyondCode \LaravelWebSockets \PubSub \ReplicationInterface ;
67use BeyondCode \LaravelWebSockets \Statistics \Drivers \StatisticsDriver ;
78use BeyondCode \LaravelWebSockets \WebSockets \Channels \ChannelManager ;
89use Illuminate \Cache \RedisLock ;
9- use Illuminate \Support \Facades \Cache ;
10+ use Illuminate \Support \Facades \Redis ;
1011
1112class RedisStatisticsLogger implements StatisticsLogger
1213{
@@ -42,7 +43,11 @@ public function __construct(ChannelManager $channelManager, StatisticsDriver $dr
4243 {
4344 $ this ->channelManager = $ channelManager ;
4445 $ this ->driver = $ driver ;
45- $ this ->redis = Cache::getRedis ();
46+ $ this ->replicator = app (ReplicationInterface::class);
47+
48+ $ this ->redis = Redis::connection (
49+ config ('websockets.replication.redis.connection ' , 'default ' )
50+ );
4651 }
4752
4853 /**
@@ -54,7 +59,7 @@ public function __construct(ChannelManager $channelManager, StatisticsDriver $dr
5459 public function webSocketMessage ($ appId )
5560 {
5661 $ this ->ensureAppIsSet ($ appId )
57- ->hincrby ( $ this ->getHash ($ appId ), 'websocket_message_count ' , 1 );
62+ ->__call ( ' hincrby ' , [ $ this ->getHash ($ appId ), 'websocket_message_count ' , 1 ] );
5863 }
5964
6065 /**
@@ -66,7 +71,7 @@ public function webSocketMessage($appId)
6671 public function apiMessage ($ appId )
6772 {
6873 $ this ->ensureAppIsSet ($ appId )
69- ->hincrby ( $ this ->getHash ($ appId ), 'api_message_count ' , 1 );
74+ ->__call ( ' hincrby ' , [ $ this ->getHash ($ appId ), 'api_message_count ' , 1 ] );
7075 }
7176
7277 /**
@@ -77,16 +82,30 @@ public function apiMessage($appId)
7782 */
7883 public function connection ($ appId )
7984 {
80- $ currentConnectionCount = $ this ->ensureAppIsSet ($ appId )
81- ->hincrby ($ this ->getHash ($ appId ), 'current_connection_count ' , 1 );
85+ // Increment the current connections count by 1.
86+ $ incremented = $ this ->ensureAppIsSet ($ appId )
87+ ->__call ('hincrby ' , [$ this ->getHash ($ appId ), 'current_connection_count ' , 1 ]);
88+
89+ $ incremented ->then (function ($ currentConnectionCount ) {
90+ // Get the peak connections count from Redis.
91+ $ peakConnectionCount = $ this ->replicator
92+ ->getPublishClient ()
93+ ->__call ('hget ' , [$ this ->getHash ($ appId ), 'peak_connection_count ' ]);
8294
83- $ currentPeakConnectionCount = $ this ->redis ->hget ($ this ->getHash ($ appId ), 'peak_connection_count ' );
95+ $ peakConnectionCount ->then (function ($ currentPeakConnectionCount ) use ($ currentConnectionCount ) {
96+ // Extract the greatest number between the current peak connection count
97+ // and the current connection number.
8498
85- $ peakConnectionCount = is_null ($ currentPeakConnectionCount )
86- ? $ currentConnectionCount
87- : max ($ currentPeakConnectionCount , $ currentConnectionCount );
99+ $ peakConnectionCount = is_null ($ currentPeakConnectionCount )
100+ ? $ currentConnectionCount
101+ : max ($ currentPeakConnectionCount , $ currentConnectionCount );
88102
89- $ this ->redis ->hset ($ this ->getHash ($ appId ), 'peak_connection_count ' , $ peakConnectionCount );
103+ // Then set it to the database.
104+ $ this ->replicator
105+ ->getPublishClient ()
106+ ->__call ('hset ' , [$ this ->getHash ($ appId ), 'peak_connection_count ' , $ peakConnectionCount ]);
107+ });
108+ });
90109 }
91110
92111 /**
@@ -97,16 +116,30 @@ public function connection($appId)
97116 */
98117 public function disconnection ($ appId )
99118 {
100- $ currentConnectionCount = $ this ->ensureAppIsSet ($ appId )
101- ->hincrby ($ this ->getHash ($ appId ), 'current_connection_count ' , -1 );
119+ // Decrement the current connections count by 1.
120+ $ decremented = $ this ->ensureAppIsSet ($ appId )
121+ ->__call ('hincrby ' , [$ this ->getHash ($ appId ), 'current_connection_count ' , -1 ]);
122+
123+ $ decremented ->then (function ($ currentConnectionCount ) {
124+ // Get the peak connections count from Redis.
125+ $ peakConnectionCount = $ this ->replicator
126+ ->getPublishClient ()
127+ ->__call ('hget ' , [$ this ->getHash ($ appId ), 'peak_connection_count ' ]);
102128
103- $ currentPeakConnectionCount = $ this ->redis ->hget ($ this ->getHash ($ appId ), 'peak_connection_count ' );
129+ $ peakConnectionCount ->then (function ($ currentPeakConnectionCount ) use ($ currentConnectionCount ) {
130+ // Extract the greatest number between the current peak connection count
131+ // and the current connection number.
104132
105- $ peakConnectionCount = is_null ($ currentPeakConnectionCount )
106- ? $ currentConnectionCount
107- : max ($ currentPeakConnectionCount , $ currentConnectionCount );
133+ $ peakConnectionCount = is_null ($ currentPeakConnectionCount )
134+ ? $ currentConnectionCount
135+ : max ($ currentPeakConnectionCount , $ currentConnectionCount );
108136
109- $ this ->redis ->hset ($ this ->getHash ($ appId ), 'peak_connection_count ' , $ peakConnectionCount );
137+ // Then set it to the database.
138+ $ this ->replicator
139+ ->getPublishClient ()
140+ ->__call ('hset ' , [$ this ->getHash ($ appId ), 'peak_connection_count ' , $ peakConnectionCount ]);
141+ });
142+ });
110143 }
111144
112145 /**
@@ -117,19 +150,33 @@ public function disconnection($appId)
117150 public function save ()
118151 {
119152 $ this ->lock ()->get (function () {
120- foreach ($ this ->redis ->smembers ('laravel-websockets:apps ' ) as $ appId ) {
121- if (! $ statistic = $ this ->redis ->hgetall ($ this ->getHash ($ appId ))) {
122- continue ;
123- }
153+ $ setMembers = $ this ->replicator
154+ ->getPublishClient ()
155+ ->__call ('smembers ' , ['laravel-websockets:apps ' ]);
156+
157+ $ setMembers ->then (function ($ members ) {
158+ foreach ($ members as $ appId ) {
159+ $ member = $ this ->replicator
160+ ->getPublishClient ()
161+ ->__call ('hgetall ' , [$ this ->getHash ($ appId )]);
124162
125- $ this ->createRecord ($ statistic , $ appId );
163+ $ member ->then (function ($ statistic ) use ($ appId ) {
164+ if (! $ statistic ) {
165+ return ;
166+ }
126167
127- $ currentConnectionCount = $ this ->channelManager -> getGlobalConnectionsCount ( $ appId );
168+ $ this ->createRecord ( $ statistic , $ appId );
128169
129- $ currentConnectionCount === 0
130- ? $ this ->resetAppTraces ($ appId )
131- : $ this ->resetStatistics ($ appId , $ currentConnectionCount );
132- }
170+ $ this ->channelManager
171+ ->getGlobalConnectionsCount ($ appId )
172+ ->then (function ($ currentConnectionCount ) use ($ appId ) {
173+ $ currentConnectionCount === 0
174+ ? $ this ->resetAppTraces ($ appId )
175+ : $ this ->resetStatistics ($ appId , $ currentConnectionCount );
176+ });
177+ });
178+ }
179+ });
133180 });
134181 }
135182
@@ -141,9 +188,11 @@ public function save()
141188 */
142189 protected function ensureAppIsSet ($ appId )
143190 {
144- $ this ->redis ->sadd ('laravel-websockets:apps ' , $ appId );
191+ $ this ->replicator
192+ ->getPublishClient ()
193+ ->__call ('sadd ' , ['laravel-websockets:apps ' , $ appId ]);
145194
146- return $ this ->redis ;
195+ return $ this ->replicator -> getPublishClient () ;
147196 }
148197
149198 /**
@@ -155,10 +204,21 @@ protected function ensureAppIsSet($appId)
155204 */
156205 public function resetStatistics ($ appId , int $ currentConnectionCount )
157206 {
158- $ this ->redis ->hset ($ this ->getHash ($ appId ), 'current_connection_count ' , $ currentConnectionCount );
159- $ this ->redis ->hset ($ this ->getHash ($ appId ), 'peak_connection_count ' , $ currentConnectionCount );
160- $ this ->redis ->hset ($ this ->getHash ($ appId ), 'websocket_message_count ' , 0 );
161- $ this ->redis ->hset ($ this ->getHash ($ appId ), 'api_message_count ' , 0 );
207+ $ this ->replicator
208+ ->getPublishClient ()
209+ ->__call ('hset ' , [$ this ->getHash ($ appId ), 'current_connection_count ' , $ currentConnectionCount ]);
210+
211+ $ this ->replicator
212+ ->getPublishClient ()
213+ ->__call ('hset ' , [$ this ->getHash ($ appId ), 'peak_connection_count ' , $ currentConnectionCount ]);
214+
215+ $ this ->replicator
216+ ->getPublishClient ()
217+ ->__call ('hset ' , [$ this ->getHash ($ appId ), 'websocket_message_count ' , 0 ]);
218+
219+ $ this ->replicator
220+ ->getPublishClient ()
221+ ->__call ('hset ' , [$ this ->getHash ($ appId ), 'api_message_count ' , 0 ]);
162222 }
163223
164224 /**
@@ -170,12 +230,25 @@ public function resetStatistics($appId, int $currentConnectionCount)
170230 */
171231 public function resetAppTraces ($ appId )
172232 {
173- $ this ->redis ->hdel ($ this ->getHash ($ appId ), 'current_connection_count ' );
174- $ this ->redis ->hdel ($ this ->getHash ($ appId ), 'peak_connection_count ' );
175- $ this ->redis ->hdel ($ this ->getHash ($ appId ), 'websocket_message_count ' );
176- $ this ->redis ->hdel ($ this ->getHash ($ appId ), 'api_message_count ' );
233+ $ this ->replicator
234+ ->getPublishClient ()
235+ ->__call ('hdel ' , [$ this ->getHash ($ appId ), 'current_connection_count ' ]);
236+
237+ $ this ->replicator
238+ ->getPublishClient ()
239+ ->__call ('hdel ' , [$ this ->getHash ($ appId ), 'peak_connection_count ' ]);
240+
241+ $ this ->replicator
242+ ->getPublishClient ()
243+ ->__call ('hdel ' , [$ this ->getHash ($ appId ), 'websocket_message_count ' ]);
244+
245+ $ this ->replicator
246+ ->getPublishClient ()
247+ ->__call ('hdel ' , [$ this ->getHash ($ appId ), 'api_message_count ' ]);
177248
178- $ this ->redis ->srem ('laravel-websockets:apps ' , $ appId );
249+ $ this ->replicator
250+ ->getPublishClient ()
251+ ->__call ('srem ' , ['laravel-websockets:apps ' , $ appId ]);
179252 }
180253
181254 /**
0 commit comments