Skip to content

Commit

Permalink
feat: add configuration objects for TopicClient
Browse files Browse the repository at this point in the history
  • Loading branch information
anitarua committed Dec 14, 2023
1 parent 7bb7a91 commit 598ef12
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ and the Flutter guide for
4. `brew info dart` to determine where the dart sdk is installed
5. followed [these](https://fluttermaster.com/config-dart-sdk-inside-intellij-idea-on-macos/) instructions on how to setup sdk inside of intellij

In VSCode, there's an extension for Dart and Flutter files.
To install dependencies using CLI: `dart pub get`
Running tests: `dart test`
Linting and formatting: `dart format` and `dart fix`

TODO: Put a short description of the package here that helps potential users
know whether this package might be useful for them.

Expand All @@ -40,6 +45,17 @@ to `/example` folder.
const like = 'sample';
```

## Logging

We use the [Dart logging package](https://github.com/dart-lang/logging) to create internal Loggers for producing Momento-related logs. The default logger does not do anything with the logs, so you must configure the logging level and handler at the beginning of your program:

```
Logger.root.level = Level.ALL; // defaults to Level.INFO
Logger.root.onRecord.listen((record) {
print('${record.level.name}: ${record.time}: ${record.message}');
});
```

## Additional information

TODO: Tell users more about the package: where to find more information, how to
Expand Down
37 changes: 37 additions & 0 deletions lib/src/config/topic_configuration.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import 'transport/grpc_configuration.dart';
import 'transport/transport_strategy.dart';

abstract interface class TopicConfiguration {
/// Configures low-level options for network interactions with the Momento service
late TransportStrategy transportStrategy;

/// Constructor for a TopicConfiguration
TopicConfiguration(this.transportStrategy);

/// Copy constructor for overriding TransportStrategy
TopicConfiguration withTransportStrategy(TransportStrategy transportStrategy);

/// Convenience copy constructor that updates the client-side
/// timeout setting in the transport strategy
TopicConfiguration withDeadline(Duration deadline);
}

/// Configuration options for Momento TopicClient.
/// The easiest way to get a `TopicClientConfiguration` object is
/// to use one of the prebuilt TopicClientConfigurations classes.
class TopicClientConfiguration implements TopicConfiguration {
@override
late TransportStrategy transportStrategy;

TopicClientConfiguration(this.transportStrategy);

@override
TopicConfiguration withTransportStrategy(TransportStrategy transportStrategy) {
return TopicClientConfiguration(transportStrategy);
}

@override
TopicConfiguration withDeadline(Duration deadline) {
return TopicClientConfiguration(StaticTransportStrategy(StaticGrpcConfiguration(deadline)));
}
}
17 changes: 17 additions & 0 deletions lib/src/config/topic_configurations.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'topic_configuration.dart';
import 'transport/grpc_configuration.dart';
import 'transport/transport_strategy.dart';

/// Prebuilt configurations for Momento Topics clients
sealed class TopicClientConfigurations {}

/// Provides prebuilt configurations for the `TopicClient` on mobile platforms
sealed class Mobile extends TopicClientConfigurations {
static TopicClientConfiguration defaultConfig() {
return latest();
}

static TopicClientConfiguration latest() {
return TopicClientConfiguration(StaticTransportStrategy(StaticGrpcConfiguration(Duration(seconds: 15))));;
}
}
21 changes: 21 additions & 0 deletions lib/src/config/transport/grpc_configuration.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
abstract interface class GrpcConfiguration {
/// Duration of time the client is willing to wait for an RPC to
/// complete before it is terminated with a DeadlineExceeded error
late Duration deadline;

/// Copy constructor for overriding the client-side deadline
GrpcConfiguration withDeadline(Duration deadline);
}

/// Encapsulates gRPC configuration tunables
class StaticGrpcConfiguration implements GrpcConfiguration {
@override
late Duration deadline;

StaticGrpcConfiguration(this.deadline);

@override
GrpcConfiguration withDeadline(Duration deadline) {
return StaticGrpcConfiguration(deadline);
}
}
30 changes: 30 additions & 0 deletions lib/src/config/transport/transport_strategy.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import 'grpc_configuration.dart';

abstract interface class TransportStrategy {
/// Low-level gRPC settings for communication with the Momento server
late GrpcConfiguration grpcConfig;

/// Copy constructor to update the client-side timeout
TransportStrategy withGrpcConfig(GrpcConfiguration grpcConfig);

/// Copy constructor to update the client-side timeout
TransportStrategy withClientTimeout(Duration timeout);
}

class StaticTransportStrategy implements TransportStrategy {
@override
late GrpcConfiguration grpcConfig;

StaticTransportStrategy(this.grpcConfig);

@override
TransportStrategy withClientTimeout(Duration timeout) {
return StaticTransportStrategy(StaticGrpcConfiguration(timeout));
}

@override
TransportStrategy withGrpcConfig(GrpcConfiguration grpcConfig) {
return StaticTransportStrategy(grpcConfig);
}

}
2 changes: 2 additions & 0 deletions lib/src/topic_client.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:client_sdk_dart/src/auth/credential_provider.dart';
import 'package:logging/logging.dart';
import 'internal/pubsub_client.dart';
import 'messages/Values.dart';
import 'messages/responses/topics/topic_publish.dart';
Expand All @@ -10,6 +11,7 @@ abstract class ITopicClient {
class TopicClient implements ITopicClient {
ClientPubsub _pubsubClient;
CredentialProvider _credentialProvider;
final Logger _logger = Logger('MomentoTopicClient');

TopicClient(this._credentialProvider) : _pubsubClient = ClientPubsub(_credentialProvider);

Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ environment:
dependencies:
grpc: ^3.2.4
jwt_decoder: ^2.0.1
logging: ^1.2.0
protobuf: ^3.1.0
string_validator: ^1.0.2
# path: ^1.8.0
Expand Down

0 comments on commit 598ef12

Please sign in to comment.