Skip to content

Commit

Permalink
Improved error handling in sessions (#284)
Browse files Browse the repository at this point in the history
  • Loading branch information
njooma authored Oct 30, 2024
1 parent 57cb7be commit 538157e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
1 change: 1 addition & 0 deletions lib/src/robot/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ class RobotClient {
client._options = options;
client._channel = await dial(url, options.dialOptions, () => client._sessionsClient.metadata());
client._sessionsClient = SessionsClient(client._channel, options.enableSessions);
client._sessionsClient.start();
client._client = rpb.RobotServiceClient(client._channel);
client._streamManager = StreamManager(client._channel as WebRtcClientChannel);
await client.refresh();
Expand Down
22 changes: 15 additions & 7 deletions lib/src/robot/sessions_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:grpc/grpc_connection_interface.dart';
import 'package:logger/logger.dart';
import 'package:viam_sdk/protos/robot/robot.dart';

import '../gen/google/rpc/code.pbenum.dart';
import '../resource/base.dart';

final _logger = Logger();
Expand Down Expand Up @@ -32,7 +33,7 @@ class SessionsClient implements ResourceRPCClient {

String _currentId = '';
final bool _enabled;
bool _supported = false;
bool? _supported;
late Duration _heartbeatInterval;

SessionsClient(this.channel, this._enabled) {
Expand All @@ -42,6 +43,7 @@ class SessionsClient implements ResourceRPCClient {
/// Retrieve metadata associated with the session (e.g. whether sessions are supported, the current ID of the session)
String metadata() {
if (!_enabled) return '';
if (_supported == false) return '';

if (_currentId != '') return _currentId;

Expand All @@ -60,9 +62,14 @@ class SessionsClient implements ResourceRPCClient {
microseconds: response.heartbeatWindow.nanos ~/ 1.8,
);

_heartbeatTask();

return _currentId;
}, onError: (error, _) {
if (error is GrpcError && error.code == Code.UNIMPLEMENTED.value) {
_supported = false;
} else {
_logger.e('Error starting session: $error');
}
return '';
});
} catch (e) {
_logger.e('Error starting session: $e');
Expand All @@ -76,15 +83,16 @@ class SessionsClient implements ResourceRPCClient {
void reset() {
_logger.d('Resetting current session with ID: $_currentId');
_currentId = '';
_supported = false;
_supported = null;
metadata();
_heartbeatTask();
}

/// Stop the session client and heartbeat tasks
void stop() {
_logger.d('Stopping SessionClient');
_currentId = '';
_supported = false;
_supported = null;
}

/// Start the session client
Expand All @@ -93,14 +101,14 @@ class SessionsClient implements ResourceRPCClient {
}

Future<void> _heartbeatTask() async {
while (_supported) {
while (_supported == true) {
await _heartbeatTick();
await Future.delayed(_heartbeatInterval);
}
}

Future<void> _heartbeatTick() async {
if (!_supported) return;
if (_supported == false) return;

final request = SendSessionHeartbeatRequest()..id = _currentId;

Expand Down

0 comments on commit 538157e

Please sign in to comment.