Skip to content

Commit

Permalink
Add reconnects & subscriptions metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
PlugFox committed Aug 3, 2023
1 parent d82894f commit fc479ae
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 18 deletions.
17 changes: 13 additions & 4 deletions lib/src/client/spinify.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ abstract base class SpinifyBase implements ISpinify {
late final ServerSubscriptionManager _serverSubscriptionManager =
ServerSubscriptionManager(_transport);

@override
({
Map<String, SpinifyClientSubscription> client,
Map<String, SpinifyServerSubscription> server,
}) get subscriptions => (
client: _clientSubscriptionManager.subscriptions,
server: _serverSubscriptionManager.subscriptions
);

/// Init spinify client, override this method to add custom logic.
/// This method is called in constructor.
/// {@nodoc}
Expand Down Expand Up @@ -527,10 +536,6 @@ base mixin SpinifyClientSubscriptionMixin on SpinifyBase, SpinifyErrorsMixin {
return _clientSubscriptionManager.newSubscription(channel, config);
}

@override
Map<String, SpinifyClientSubscription> get subscriptions =>
_clientSubscriptionManager.subscriptions;

@override
SpinifyClientSubscription? getSubscription(String channel) =>
_clientSubscriptionManager[channel];
Expand Down Expand Up @@ -742,6 +747,10 @@ base mixin SpinifyMetricsMixin on SpinifyBase {
timestamp: timestamp,
lastUrl: wsMetrics.lastUrl,
reconnects: (successful: _connectsSuccessful, total: _connectsTotal),
subscriptions: (
client: _clientSubscriptionManager.count,
server: _serverSubscriptionManager.count,
),
state: state,
receivedCount: wsMetrics.receivedCount,
receivedSize: wsMetrics.receivedSize,
Expand Down
22 changes: 15 additions & 7 deletions lib/src/client/spinify_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ abstract interface class ISpinify
ISpinifyAsyncMessageSender,
ISpinifyPublicationSender,
ISpinifyEventReceiver,
ISpinifyClientSubscriptionsManager,
ISpinifySubscriptionsManager,
ISpinifyPresenceOwner,
ISpinifyHistoryOwner,
ISpinifyRemoteProcedureCall,
Expand Down Expand Up @@ -75,7 +75,7 @@ abstract interface class ISpinifyEventReceiver {
}

/// Spinify client subscriptions manager interface.
abstract interface class ISpinifyClientSubscriptionsManager {
abstract interface class ISpinifySubscriptionsManager {
/// Create new client-side subscription.
/// `newSubscription(channel, config)` allocates a new Subscription
/// in the registry or throws an exception if the Subscription
Expand All @@ -93,15 +93,23 @@ abstract interface class ISpinifyClientSubscriptionsManager {
/// in the channel.
SpinifyClientSubscription? getSubscription(String channel);

/// Remove the [Subscription] from internal registry
/// Remove the [SpinifySubscription] from internal registry
/// and unsubscribe from [SpinifyClientSubscription.channel].
Future<void> removeSubscription(SpinifyClientSubscription subscription);

/// Get map wirth all registered client-side subscriptions.
/// Get map wirth all registered client-side & server-side subscriptions.
/// Returns all registered subscriptions,
/// so you can iterate over all and do some action if required
/// (for example, you want to unsubscribe/remove all subscriptions).
Map<String, SpinifyClientSubscription> get subscriptions;
/// so you can iterate over all and do some action if required.
///
/// For example:
/// ```dart
/// final subscription = spinify.subscriptions.client['chat']!;
/// await subscription.unsubscribe();
/// ```
({
Map<String, SpinifyClientSubscription> client,
Map<String, SpinifyServerSubscription> server,
}) get subscriptions;
}

/// Spinify presence owner interface.
Expand Down
33 changes: 33 additions & 0 deletions lib/src/model/metrics.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import 'package:meta/meta.dart';
import 'package:spinify/src/client/state.dart';

/// Subscription count
/// - total
/// - unsubscribed
/// - subscribing
/// - subscribed
typedef SpinifySubscriptionCount = ({
int total,
int unsubscribed,
int subscribing,
int subscribed
});

/// {@template metrics}
/// Metrics of Spinify client.
/// {@endtemplate}
Expand All @@ -15,6 +27,7 @@ final class SpinifyMetrics implements Comparable<SpinifyMetrics> {
required this.transferredSize,
required this.receivedSize,
required this.reconnects,
required this.subscriptions,
required this.transferredCount,
required this.receivedCount,
required this.lastUrl,
Expand All @@ -35,6 +48,12 @@ final class SpinifyMetrics implements Comparable<SpinifyMetrics> {
/// The total number of times the connection has been re-established.
final ({int successful, int total}) reconnects;

/// The number of subscriptions.
final ({
SpinifySubscriptionCount client,
SpinifySubscriptionCount server
}) subscriptions;

/// The total number of messages sent.
final BigInt transferredCount;

Expand All @@ -55,6 +74,20 @@ final class SpinifyMetrics implements Comparable<SpinifyMetrics> {
'successful': reconnects.successful,
'total': reconnects.total,
},
'subscriptions': <String, Map<String, int>>{
'client': {
'total': subscriptions.client.total,
'unsubscribed': subscriptions.client.unsubscribed,
'subscribing': subscriptions.client.subscribing,
'subscribed': subscriptions.client.subscribed,
},
'server': {
'total': subscriptions.server.total,
'unsubscribed': subscriptions.server.unsubscribed,
'subscribing': subscriptions.server.subscribing,
'subscribed': subscriptions.server.subscribed,
},
},
'transferredSize': transferredSize,
'receivedSize': receivedSize,
'transferredCount': transferredCount,
Expand Down
2 changes: 1 addition & 1 deletion lib/src/subscription/client_subscription_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ final class SpinifyClientSubscriptionImpl extends SpinifyClientSubscriptionBase
/// {@nodoc}
@internal
abstract base class SpinifyClientSubscriptionBase
implements SpinifyClientSubscription {
extends SpinifyClientSubscription {
/// {@nodoc}
SpinifyClientSubscriptionBase({
required this.channel,
Expand Down
23 changes: 23 additions & 0 deletions lib/src/subscription/client_subscription_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:spinify/src/model/exception.dart';
import 'package:spinify/src/subscription/client_subscription_impl.dart';
import 'package:spinify/src/subscription/subscription.dart';
import 'package:spinify/src/subscription/subscription_config.dart';
import 'package:spinify/src/subscription/subscription_state.dart';
import 'package:spinify/src/transport/transport_interface.dart';

/// Responsible for managing client-side subscriptions.
Expand All @@ -20,6 +21,28 @@ final class ClientSubscriptionManager {
/// {@nodoc}
final WeakReference<ISpinifyTransport> _transportWeakRef;

/// Subscriptions count.
({int total, int unsubscribed, int subscribing, int subscribed}) get count {
var total = 0, unsubscribed = 0, subscribing = 0, subscribed = 0;
for (final entry in _channelSubscriptions.values) {
total++;
switch (entry.state) {
case SpinifySubscriptionState$Unsubscribed _:
unsubscribed++;
case SpinifySubscriptionState$Subscribing _:
subscribing++;
case SpinifySubscriptionState$Subscribed _:
subscribed++;
}
}
return (
total: total,
unsubscribed: unsubscribed,
subscribing: subscribing,
subscribed: subscribed,
);
}

/// Subscriptions registry (channel -> subscription).
/// Channel : SpinifyClientSubscription
/// {@nodoc}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/subscription/server_subscription_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ final class SpinifyServerSubscriptionImpl extends SpinifyServerSubscriptionBase
/// {@nodoc}
@internal
abstract base class SpinifyServerSubscriptionBase
implements SpinifyServerSubscription {
extends SpinifyServerSubscription {
/// {@nodoc}
SpinifyServerSubscriptionBase({
required this.channel,
Expand Down
23 changes: 23 additions & 0 deletions lib/src/subscription/server_subscription_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:spinify/src/model/subscribe.dart';
import 'package:spinify/src/model/unsubscribe.dart';
import 'package:spinify/src/subscription/server_subscription_impl.dart';
import 'package:spinify/src/subscription/subscription.dart';
import 'package:spinify/src/subscription/subscription_state.dart';
import 'package:spinify/src/transport/transport_interface.dart';

/// Responsible for managing client-side subscriptions.
Expand All @@ -20,6 +21,28 @@ final class ServerSubscriptionManager {
/// {@nodoc}
final WeakReference<ISpinifyTransport> _transportWeakRef;

/// Subscriptions count.
({int total, int unsubscribed, int subscribing, int subscribed}) get count {
var total = 0, unsubscribed = 0, subscribing = 0, subscribed = 0;
for (final entry in _channelSubscriptions.values) {
total++;
switch (entry.state) {
case SpinifySubscriptionState$Unsubscribed _:
unsubscribed++;
case SpinifySubscriptionState$Subscribing _:
subscribing++;
case SpinifySubscriptionState$Subscribed _:
subscribed++;
}
}
return (
total: total,
unsubscribed: unsubscribed,
subscribing: subscribing,
subscribed: subscribed,
);
}

/// Subscriptions registry (channel -> subscription).
/// Channel : SpinifyClientSubscription
/// {@nodoc}
Expand Down
8 changes: 3 additions & 5 deletions lib/src/subscription/subscription.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import 'package:spinify/src/subscription/subscription_states_stream.dart';
/// - For server-side subscriptions see [SpinifyServerSubscription].
/// {@endtemplate}
/// {@category Subscription}
abstract interface class SpinifySubscription {
sealed class SpinifySubscription {
/// Channel name.
abstract final String channel;

Expand Down Expand Up @@ -129,8 +129,7 @@ abstract interface class SpinifySubscription {
/// {@endtemplate}
/// {@category Subscription}
/// {@subCategory Client-side}
abstract interface class SpinifyClientSubscription
implements SpinifySubscription {
abstract class SpinifyClientSubscription extends SpinifySubscription {
/// Start subscribing to a channel
Future<void> subscribe();

Expand All @@ -155,5 +154,4 @@ abstract interface class SpinifyClientSubscription
/// {@endtemplate}
/// {@category Subscription}
/// {@subCategory Server-side}
abstract interface class SpinifyServerSubscription
implements SpinifySubscription {}
abstract class SpinifyServerSubscription extends SpinifySubscription {}

0 comments on commit fc479ae

Please sign in to comment.