Skip to content

Commit

Permalink
refactor: Deprecate eventsPerSecond on Realtime (#838)
Browse files Browse the repository at this point in the history
* deprecate eventsPerSecond

* Add deprecation flag to rateLimited enum value
  • Loading branch information
dshukertjr authored Feb 19, 2024
1 parent e241e76 commit 4238387
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 45 deletions.
6 changes: 1 addition & 5 deletions packages/realtime_client/lib/src/push.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class Push {
Map<String, dynamic>? _receivedResp;
final List<Hook> _recHooks = [];
String? _refEvent;
bool rateLimited = false;

/// The channel
final RealtimeChannel _channel;
Expand Down Expand Up @@ -59,7 +58,7 @@ class Push {
}
startTimeout();
sent = true;
final status = _channel.socket.push(
_channel.socket.push(
Message(
topic: _channel.topic,
event: _event,
Expand All @@ -68,9 +67,6 @@ class Push {
joinRef: _channel.joinRef,
),
);
if (status == 'rate limited') {
rateLimited = true;
}
}

void updatePayload(Map<String, dynamic> payload) {
Expand Down
4 changes: 0 additions & 4 deletions packages/realtime_client/lib/src/realtime_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -518,10 +518,6 @@ class RealtimeChannel {
opts['timeout'] ?? _timeout,
);

if (push.rateLimited) {
completer.complete(ChannelResponse.rateLimited);
}

if (payload['type'] == 'broadcast' &&
(params['config']?['broadcast']?['ack'] == null ||
params['config']?['broadcast']?['ack'] == false)) {
Expand Down
34 changes: 1 addition & 33 deletions packages/realtime_client/lib/src/realtime_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ class RealtimeClient {
};
int longpollerTimeout = 20000;
SocketStates? connState;
int eventsPerSecondLimitMs = 100;
bool inThrottle = false;

/// Initializes the Socket
///
Expand Down Expand Up @@ -122,11 +120,6 @@ class RealtimeClient {
if (headers != null) ...headers,
},
transport = transport ?? createWebSocketClient {
final eventsPerSecond = params['eventsPerSecond'];
if (eventsPerSecond != null) {
eventsPerSecondLimitMs = (1000 / int.parse(eventsPerSecond)).floor();
}

final customJWT = this.headers['Authorization']?.split(' ').last;
accessToken = customJWT ?? params['apikey'];

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

if (isConnected) {
if ([
ChannelEvents.broadcast,
ChannelEvents.presence,
ChannelEvents.postgresChanges
].contains(event)) {
final isThrottled = _throttle(callback)();
if (isThrottled) {
return 'rate limited';
}
} else {
callback();
}
callback();
} else {
sendBuffer.add(callback);
}
Expand Down Expand Up @@ -487,17 +468,4 @@ class RealtimeClient {
));
setAuth(accessToken);
}

bool Function() _throttle(Function callback, [int? eventsPerSecondLimit]) {
return () {
if (inThrottle) return true;
callback();
inThrottle = true;
Timer(
Duration(
milliseconds: eventsPerSecondLimit ?? eventsPerSecondLimitMs),
() => inThrottle = false);
return false;
};
}
}
9 changes: 8 additions & 1 deletion packages/realtime_client/lib/src/types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,14 @@ class ChannelFilter {
}
}

enum ChannelResponse { ok, timedOut, rateLimited, error }
enum ChannelResponse {
ok,
timedOut,
@Deprecated(
'Client side rate limiting has been removed, and this enum value will never be returned.')
rateLimited,
error
}

enum RealtimeListenTypes { postgresChanges, broadcast, presence }

Expand Down
2 changes: 2 additions & 0 deletions packages/supabase/lib/src/realtime_client_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class RealtimeClientOptions {
/// How many events the RealtimeClient can push in a second
///
/// Defaults to 10 events per second
@Deprecated(
'Client side rate limit has been removed. This option will be ignored.')
final int? eventsPerSecond;

/// Level of realtime server logs to to be logged
Expand Down
2 changes: 0 additions & 2 deletions packages/supabase/lib/src/supabase_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,10 @@ class SupabaseClient {
RealtimeClient _initRealtimeClient({
required RealtimeClientOptions options,
}) {
final eventsPerSecond = options.eventsPerSecond;
return RealtimeClient(
_realtimeUrl,
params: {
'apikey': _supabaseKey,
if (eventsPerSecond != null) 'eventsPerSecond': '$eventsPerSecond'
},
headers: headers,
logLevel: options.logLevel,
Expand Down

0 comments on commit 4238387

Please sign in to comment.