From 538157ec5d69d41ca3426ff45278032498c3a41e Mon Sep 17 00:00:00 2001 From: Naveed Jooma Date: Wed, 30 Oct 2024 13:39:57 -0400 Subject: [PATCH] Improved error handling in sessions (#284) --- lib/src/robot/client.dart | 1 + lib/src/robot/sessions_client.dart | 22 +++++++++++++++------- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/lib/src/robot/client.dart b/lib/src/robot/client.dart index 63d0dcdc506..27a08b849c1 100644 --- a/lib/src/robot/client.dart +++ b/lib/src/robot/client.dart @@ -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(); diff --git a/lib/src/robot/sessions_client.dart b/lib/src/robot/sessions_client.dart index 1817183c5d8..e6956cdaacf 100644 --- a/lib/src/robot/sessions_client.dart +++ b/lib/src/robot/sessions_client.dart @@ -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(); @@ -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) { @@ -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; @@ -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'); @@ -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 @@ -93,14 +101,14 @@ class SessionsClient implements ResourceRPCClient { } Future _heartbeatTask() async { - while (_supported) { + while (_supported == true) { await _heartbeatTick(); await Future.delayed(_heartbeatInterval); } } Future _heartbeatTick() async { - if (!_supported) return; + if (_supported == false) return; final request = SendSessionHeartbeatRequest()..id = _currentId;