3
3
namespace BeyondCode \LaravelWebSockets \Statistics \Logger ;
4
4
5
5
use BeyondCode \LaravelWebSockets \Apps \App ;
6
+ use BeyondCode \LaravelWebSockets \PubSub \ReplicationInterface ;
6
7
use BeyondCode \LaravelWebSockets \Statistics \Drivers \StatisticsDriver ;
7
8
use BeyondCode \LaravelWebSockets \WebSockets \Channels \ChannelManager ;
8
9
use Illuminate \Cache \RedisLock ;
9
- use Illuminate \Support \Facades \Cache ;
10
+ use Illuminate \Support \Facades \Redis ;
10
11
11
12
class RedisStatisticsLogger implements StatisticsLogger
12
13
{
@@ -42,7 +43,11 @@ public function __construct(ChannelManager $channelManager, StatisticsDriver $dr
42
43
{
43
44
$ this ->channelManager = $ channelManager ;
44
45
$ 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
+ );
46
51
}
47
52
48
53
/**
@@ -54,7 +59,7 @@ public function __construct(ChannelManager $channelManager, StatisticsDriver $dr
54
59
public function webSocketMessage ($ appId )
55
60
{
56
61
$ this ->ensureAppIsSet ($ appId )
57
- ->hincrby ( $ this ->getHash ($ appId ), 'websocket_message_count ' , 1 );
62
+ ->__call ( ' hincrby ' , [ $ this ->getHash ($ appId ), 'websocket_message_count ' , 1 ] );
58
63
}
59
64
60
65
/**
@@ -66,7 +71,7 @@ public function webSocketMessage($appId)
66
71
public function apiMessage ($ appId )
67
72
{
68
73
$ this ->ensureAppIsSet ($ appId )
69
- ->hincrby ( $ this ->getHash ($ appId ), 'api_message_count ' , 1 );
74
+ ->__call ( ' hincrby ' , [ $ this ->getHash ($ appId ), 'api_message_count ' , 1 ] );
70
75
}
71
76
72
77
/**
@@ -77,16 +82,30 @@ public function apiMessage($appId)
77
82
*/
78
83
public function connection ($ appId )
79
84
{
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 ' ]);
82
94
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.
84
98
85
- $ peakConnectionCount = is_null ($ currentPeakConnectionCount )
86
- ? $ currentConnectionCount
87
- : max ($ currentPeakConnectionCount , $ currentConnectionCount );
99
+ $ peakConnectionCount = is_null ($ currentPeakConnectionCount )
100
+ ? $ currentConnectionCount
101
+ : max ($ currentPeakConnectionCount , $ currentConnectionCount );
88
102
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
+ });
90
109
}
91
110
92
111
/**
@@ -97,16 +116,30 @@ public function connection($appId)
97
116
*/
98
117
public function disconnection ($ appId )
99
118
{
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 ' ]);
102
128
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.
104
132
105
- $ peakConnectionCount = is_null ($ currentPeakConnectionCount )
106
- ? $ currentConnectionCount
107
- : max ($ currentPeakConnectionCount , $ currentConnectionCount );
133
+ $ peakConnectionCount = is_null ($ currentPeakConnectionCount )
134
+ ? $ currentConnectionCount
135
+ : max ($ currentPeakConnectionCount , $ currentConnectionCount );
108
136
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
+ });
110
143
}
111
144
112
145
/**
@@ -117,19 +150,33 @@ public function disconnection($appId)
117
150
public function save ()
118
151
{
119
152
$ 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 )]);
124
162
125
- $ this ->createRecord ($ statistic , $ appId );
163
+ $ member ->then (function ($ statistic ) use ($ appId ) {
164
+ if (! $ statistic ) {
165
+ return ;
166
+ }
126
167
127
- $ currentConnectionCount = $ this ->channelManager -> getGlobalConnectionsCount ( $ appId );
168
+ $ this ->createRecord ( $ statistic , $ appId );
128
169
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
+ });
133
180
});
134
181
}
135
182
@@ -141,9 +188,11 @@ public function save()
141
188
*/
142
189
protected function ensureAppIsSet ($ appId )
143
190
{
144
- $ this ->redis ->sadd ('laravel-websockets:apps ' , $ appId );
191
+ $ this ->replicator
192
+ ->getPublishClient ()
193
+ ->__call ('sadd ' , ['laravel-websockets:apps ' , $ appId ]);
145
194
146
- return $ this ->redis ;
195
+ return $ this ->replicator -> getPublishClient () ;
147
196
}
148
197
149
198
/**
@@ -155,10 +204,21 @@ protected function ensureAppIsSet($appId)
155
204
*/
156
205
public function resetStatistics ($ appId , int $ currentConnectionCount )
157
206
{
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 ]);
162
222
}
163
223
164
224
/**
@@ -170,12 +230,25 @@ public function resetStatistics($appId, int $currentConnectionCount)
170
230
*/
171
231
public function resetAppTraces ($ appId )
172
232
{
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 ' ]);
177
248
178
- $ this ->redis ->srem ('laravel-websockets:apps ' , $ appId );
249
+ $ this ->replicator
250
+ ->getPublishClient ()
251
+ ->__call ('srem ' , ['laravel-websockets:apps ' , $ appId ]);
179
252
}
180
253
181
254
/**
0 commit comments