Skip to content

Commit 4238387

Browse files
authored
refactor: Deprecate eventsPerSecond on Realtime (supabase#838)
* deprecate eventsPerSecond * Add deprecation flag to rateLimited enum value
1 parent e241e76 commit 4238387

File tree

6 files changed

+12
-45
lines changed

6 files changed

+12
-45
lines changed

packages/realtime_client/lib/src/push.dart

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ class Push {
1717
Map<String, dynamic>? _receivedResp;
1818
final List<Hook> _recHooks = [];
1919
String? _refEvent;
20-
bool rateLimited = false;
2120

2221
/// The channel
2322
final RealtimeChannel _channel;
@@ -59,7 +58,7 @@ class Push {
5958
}
6059
startTimeout();
6160
sent = true;
62-
final status = _channel.socket.push(
61+
_channel.socket.push(
6362
Message(
6463
topic: _channel.topic,
6564
event: _event,
@@ -68,9 +67,6 @@ class Push {
6867
joinRef: _channel.joinRef,
6968
),
7069
);
71-
if (status == 'rate limited') {
72-
rateLimited = true;
73-
}
7470
}
7571

7672
void updatePayload(Map<String, dynamic> payload) {

packages/realtime_client/lib/src/realtime_channel.dart

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -518,10 +518,6 @@ class RealtimeChannel {
518518
opts['timeout'] ?? _timeout,
519519
);
520520

521-
if (push.rateLimited) {
522-
completer.complete(ChannelResponse.rateLimited);
523-
}
524-
525521
if (payload['type'] == 'broadcast' &&
526522
(params['config']?['broadcast']?['ack'] == null ||
527523
params['config']?['broadcast']?['ack'] == false)) {

packages/realtime_client/lib/src/realtime_client.dart

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ class RealtimeClient {
8181
};
8282
int longpollerTimeout = 20000;
8383
SocketStates? connState;
84-
int eventsPerSecondLimitMs = 100;
85-
bool inThrottle = false;
8684

8785
/// Initializes the Socket
8886
///
@@ -122,11 +120,6 @@ class RealtimeClient {
122120
if (headers != null) ...headers,
123121
},
124122
transport = transport ?? createWebSocketClient {
125-
final eventsPerSecond = params['eventsPerSecond'];
126-
if (eventsPerSecond != null) {
127-
eventsPerSecondLimitMs = (1000 / int.parse(eventsPerSecond)).floor();
128-
}
129-
130123
final customJWT = this.headers['Authorization']?.split(' ').last;
131124
accessToken = customJWT ?? params['apikey'];
132125

@@ -288,7 +281,6 @@ class RealtimeClient {
288281
///
289282
/// If the socket is not connected, the message gets enqueued within a local buffer, and sent out when a connection is next established.
290283
String? push(Message message) {
291-
final event = message.event;
292284
void callback() {
293285
encode(message.toJson(), (result) => conn?.sink.add(result));
294286
}
@@ -297,18 +289,7 @@ class RealtimeClient {
297289
message.payload);
298290

299291
if (isConnected) {
300-
if ([
301-
ChannelEvents.broadcast,
302-
ChannelEvents.presence,
303-
ChannelEvents.postgresChanges
304-
].contains(event)) {
305-
final isThrottled = _throttle(callback)();
306-
if (isThrottled) {
307-
return 'rate limited';
308-
}
309-
} else {
310-
callback();
311-
}
292+
callback();
312293
} else {
313294
sendBuffer.add(callback);
314295
}
@@ -487,17 +468,4 @@ class RealtimeClient {
487468
));
488469
setAuth(accessToken);
489470
}
490-
491-
bool Function() _throttle(Function callback, [int? eventsPerSecondLimit]) {
492-
return () {
493-
if (inThrottle) return true;
494-
callback();
495-
inThrottle = true;
496-
Timer(
497-
Duration(
498-
milliseconds: eventsPerSecondLimit ?? eventsPerSecondLimitMs),
499-
() => inThrottle = false);
500-
return false;
501-
};
502-
}
503471
}

packages/realtime_client/lib/src/types.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,14 @@ class ChannelFilter {
101101
}
102102
}
103103

104-
enum ChannelResponse { ok, timedOut, rateLimited, error }
104+
enum ChannelResponse {
105+
ok,
106+
timedOut,
107+
@Deprecated(
108+
'Client side rate limiting has been removed, and this enum value will never be returned.')
109+
rateLimited,
110+
error
111+
}
105112

106113
enum RealtimeListenTypes { postgresChanges, broadcast, presence }
107114

packages/supabase/lib/src/realtime_client_options.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ class RealtimeClientOptions {
77
/// How many events the RealtimeClient can push in a second
88
///
99
/// Defaults to 10 events per second
10+
@Deprecated(
11+
'Client side rate limit has been removed. This option will be ignored.')
1012
final int? eventsPerSecond;
1113

1214
/// Level of realtime server logs to to be logged

packages/supabase/lib/src/supabase_client.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,12 +255,10 @@ class SupabaseClient {
255255
RealtimeClient _initRealtimeClient({
256256
required RealtimeClientOptions options,
257257
}) {
258-
final eventsPerSecond = options.eventsPerSecond;
259258
return RealtimeClient(
260259
_realtimeUrl,
261260
params: {
262261
'apikey': _supabaseKey,
263-
if (eventsPerSecond != null) 'eventsPerSecond': '$eventsPerSecond'
264262
},
265263
headers: headers,
266264
logLevel: options.logLevel,

0 commit comments

Comments
 (0)