Skip to content

Commit 82c577a

Browse files
committed
cleanup
1 parent 8509daa commit 82c577a

File tree

5 files changed

+68
-15
lines changed

5 files changed

+68
-15
lines changed

src/viam/sdk/module/service.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,9 @@ struct ModuleService::ServiceImpl : viam::module::v1::ModuleService::Service {
194194
auto new_parent_addr = request->parent_address();
195195
if (parent.parent_addr_ != new_parent_addr) {
196196
parent.parent_addr_ = std::move(new_parent_addr);
197-
parent.parent_ = RobotClient::at_local_socket(parent.parent_addr_, {0, boost::none});
197+
Options opts{0, boost::none};
198+
opts.set_check_every_interval(5).set_reconnect_every_interval(1);
199+
parent.parent_ = RobotClient::at_local_socket(parent.parent_addr_, opts);
198200
parent.parent_->connect_logging();
199201
}
200202
response->set_ready(parent.module_->ready());

src/viam/sdk/robot/client.cpp

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
#include <chrono>
44
#include <cstddef>
5-
#include <cstdlib>
65
#include <memory>
76
#include <mutex>
87
#include <string>
@@ -145,7 +144,7 @@ RobotClient::~RobotClient() {
145144

146145
void RobotClient::close() {
147146
should_refresh_.store(false);
148-
should_check_connection_ = false;
147+
should_check_connection_.store(false);
149148

150149
if (refresh_thread_.joinable()) {
151150
refresh_thread_.join();
@@ -238,12 +237,20 @@ void RobotClient::refresh_every() {
238237
};
239238

240239
void RobotClient::check_connection() {
241-
std::exception connection_error;
240+
unsigned int check_every = check_every_interval_;
241+
unsigned int reconnect_every = reconnect_every_interval_;
242+
if (check_every == 0) {
243+
check_every = reconnect_every;
244+
}
245+
if (check_every == 0 && reconnect_every == 0) {
246+
should_check_connection_.store(false);
247+
}
242248
bool connected(true);
243249
while (should_check_connection_) {
250+
std::exception connection_error;
244251
for (int i = 0; i < 3; ++i) {
245252
try {
246-
std::this_thread::sleep_for(std::chrono::seconds{10});
253+
std::this_thread::sleep_for(std::chrono::seconds{check_every});
247254
impl::client_helper(impl_, &RobotService::Stub::ResourceNames).invoke([](auto&) {
248255
return;
249256
});
@@ -258,9 +265,10 @@ void RobotClient::check_connection() {
258265
if (connected) {
259266
continue;
260267
}
261-
const auto* uri = viam_channel_.uri_;
262-
VIAM_SDK_LOG(error)
263-
<< "Lost connection to machine. Attempting to reconnect to every second";
268+
const auto* uri = viam_channel_.get_channel_addr();
269+
VIAM_SDK_LOG(error) << "Lost connection to machine at address " << uri << " with error "
270+
<< connection_error.what() << ". Attempting to reconnect to every "
271+
<< reconnect_every << "second(s)";
264272
viam_channel_.close();
265273

266274
for (int i = 0; i < 3; ++i) {
@@ -274,12 +282,12 @@ void RobotClient::check_connection() {
274282
connected = true;
275283
} catch (const std::exception& e) {
276284
viam_channel_.close();
277-
std::this_thread::sleep_for(std::chrono::seconds{1});
285+
std::this_thread::sleep_for(std::chrono::seconds{reconnect_every});
278286
}
279287
}
280288
if (!connected) {
281289
// NOLINTNEXTLINE
282-
exit(0);
290+
close();
283291
}
284292
}
285293
}
@@ -334,6 +342,9 @@ std::shared_ptr<RobotClient> RobotClient::with_channel(ViamChannel channel,
334342

335343
robot->should_check_connection_ = true;
336344

345+
robot->check_every_interval_ = options.check_every_interval();
346+
robot->reconnect_every_interval_ = options.reconnect_every_interval();
347+
337348
robot->check_connection_thread_ = std::thread{&RobotClient::check_connection, robot.get()};
338349

339350
robot->refresh();

src/viam/sdk/robot/client.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,12 @@ class RobotClient {
187187
void check_connection();
188188

189189
std::thread refresh_thread_;
190-
// CR erodkin: lots of checks in this thread, it takes 10 seconds to run, shutdown is now slow
191-
// on client code
192190
std::thread check_connection_thread_;
193191
std::atomic<bool> should_refresh_;
194-
bool should_check_connection_;
192+
std::atomic<bool> should_check_connection_;
195193
std::chrono::seconds refresh_interval_;
194+
unsigned int check_every_interval_;
195+
unsigned int reconnect_every_interval_;
196196

197197
ViamChannel viam_channel_;
198198

src/viam/sdk/rpc/dial.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,26 @@ void ViamChannel::close() {
203203
pimpl_.reset();
204204
}
205205

206+
const char* ViamChannel::get_channel_addr() const {
207+
return uri_;
208+
}
209+
Options& Options::set_check_every_interval(unsigned int interval) {
210+
check_every_interval_ = interval;
211+
return *this;
212+
}
213+
Options& Options::set_reconnect_every_interval(unsigned int interval) {
214+
reconnect_every_interval_ = interval;
215+
return *this;
216+
}
217+
218+
unsigned int Options::check_every_interval() const {
219+
return check_every_interval_;
220+
}
221+
222+
unsigned int Options::reconnect_every_interval() const {
223+
return reconnect_every_interval_;
224+
}
225+
206226
unsigned int Options::refresh_interval() const {
207227
return refresh_interval_;
208228
}

src/viam/sdk/rpc/dial.hpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ class ViamChannel {
4545

4646
void close();
4747

48-
// NOLINTNEXTLINE
49-
const char* uri_;
48+
const char* get_channel_addr() const;
5049

5150
private:
51+
const char* uri_;
5252
struct impl;
5353

5454
std::shared_ptr<GrpcChannel> channel_;
@@ -120,12 +120,32 @@ class Options {
120120
: refresh_interval_(std::move(refresh_interval)), dial_options_(std::move(dial_options)) {}
121121

122122
unsigned int refresh_interval() const;
123+
unsigned int check_every_interval() const;
124+
unsigned int reconnect_every_interval() const;
125+
126+
/// @brief Sets the frequency (in seconds) to verify connectivity
127+
Options& set_check_every_interval(unsigned int interval);
128+
129+
/// @brief Sets the frequency (in seconds) to attempt to reconnect when connectivity is lost
130+
Options& set_reconnect_every_interval(unsigned int interval);
123131
const boost::optional<DialOptions>& dial_options() const;
124132

125133
private:
126134
/// @brief How often to refresh the status/parts of the robot, in seconds. If set to 0, the
127135
/// robot will not automatically refresh.
128136
unsigned int refresh_interval_;
137+
138+
/// @brief How often to verify connectivity to the robot, in seconds. If set to 0, will not
139+
/// check, will default to the `reconnect_every_interval_` value. Defaults to 0.
140+
/// @note Setting to a non-zero value is useful in modules but may result in delays shutting
141+
/// down client code
142+
unsigned int check_every_interval_ = 0;
143+
144+
/// @brief How often to attempt to reconnect to the robot when disconnected. If set to 0,
145+
/// will not attempt to reconnect. Defaults to 0.
146+
/// @note Setting to a non-zero value is useful in modules but may result in delays shutting
147+
/// down client code
148+
unsigned int reconnect_every_interval_ = 0;
129149
boost::optional<DialOptions> dial_options_;
130150
};
131151

0 commit comments

Comments
 (0)