Skip to content

Commit 62617f3

Browse files
committed
unshare viam channel and remove grpc channel member
1 parent d7e41da commit 62617f3

File tree

5 files changed

+35
-34
lines changed

5 files changed

+35
-34
lines changed

src/viam/sdk/robot/client.cpp

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ void RobotClient::close() {
131131
should_refresh_.store(false);
132132
threads_.clear();
133133
stop_all();
134-
viam_channel_->close();
134+
viam_channel_.close();
135135
}
136136

137137
bool is_error_response(const grpc::Status& response) {
@@ -209,7 +209,7 @@ void RobotClient::refresh() {
209209
if (rs) {
210210
try {
211211
const std::shared_ptr<Resource> rpc_client =
212-
rs->create_rpc_client(name.name(), channel_);
212+
rs->create_rpc_client(name.name(), viam_channel_.channel());
213213
const Name name_({name.namespace_(), name.type(), name.subtype()}, "", name.name());
214214
new_resources.emplace(name_, rpc_client);
215215
} catch (const std::exception& exc) {
@@ -248,11 +248,10 @@ void RobotClient::refresh_every() {
248248
}
249249
};
250250

251-
RobotClient::RobotClient(std::shared_ptr<ViamChannel> channel)
252-
: channel_(channel->channel()),
253-
viam_channel_(std::move(channel)),
251+
RobotClient::RobotClient(ViamChannel channel)
252+
: viam_channel_(std::move(channel)),
254253
should_close_channel_(false),
255-
impl_(std::make_unique<impl>(RobotService::NewStub(channel_))) {}
254+
impl_(std::make_unique<impl>(RobotService::NewStub(viam_channel_.channel()))) {}
256255

257256
std::vector<Name> RobotClient::resource_names() const {
258257
const std::lock_guard<std::mutex> lock(lock_);
@@ -284,9 +283,9 @@ void RobotClient::log(const std::string& name,
284283
}
285284
}
286285

287-
std::shared_ptr<RobotClient> RobotClient::with_channel(std::shared_ptr<ViamChannel> channel,
286+
std::shared_ptr<RobotClient> RobotClient::with_channel(ViamChannel channel,
288287
const Options& options) {
289-
std::shared_ptr<RobotClient> robot = std::make_shared<RobotClient>(std::move(channel));
288+
auto robot = std::make_shared<RobotClient>(std::move(channel));
290289
robot->refresh_interval_ = options.refresh_interval();
291290
robot->should_refresh_ = (robot->refresh_interval_ > 0);
292291
if (robot->should_refresh_) {
@@ -305,8 +304,8 @@ std::shared_ptr<RobotClient> RobotClient::with_channel(std::shared_ptr<ViamChann
305304
std::shared_ptr<RobotClient> RobotClient::at_address(const std::string& address,
306305
const Options& options) {
307306
const char* uri = address.c_str();
308-
auto channel = ViamChannel::dial_initial(uri, options.dial_options());
309-
std::shared_ptr<RobotClient> robot = RobotClient::with_channel(channel, options);
307+
std::shared_ptr<RobotClient> robot =
308+
RobotClient::with_channel(ViamChannel::dial_initial(uri, options.dial_options()), options);
310309
robot->should_close_channel_ = true;
311310

312311
return robot;
@@ -315,10 +314,9 @@ std::shared_ptr<RobotClient> RobotClient::at_address(const std::string& address,
315314
std::shared_ptr<RobotClient> RobotClient::at_local_socket(const std::string& address,
316315
const Options& options) {
317316
const std::string addr = "unix://" + address;
318-
const std::shared_ptr<grpc::Channel> channel =
319-
sdk::impl::create_viam_channel(addr, grpc::InsecureChannelCredentials());
320-
std::shared_ptr<RobotClient> robot =
321-
RobotClient::with_channel(std::make_shared<ViamChannel>(channel), options);
317+
std::shared_ptr<RobotClient> robot = RobotClient::with_channel(
318+
ViamChannel(sdk::impl::create_viam_channel(addr, grpc::InsecureChannelCredentials())),
319+
options);
322320
robot->should_close_channel_ = true;
323321

324322
return robot;

src/viam/sdk/robot/client.hpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,9 @@ class RobotClient {
8787
/// @param options Options for connecting and refreshing.
8888
/// Connects directly to a pre-existing channel. A robot created this way must be
8989
/// `close()`d manually.
90-
static std::shared_ptr<RobotClient> with_channel(std::shared_ptr<ViamChannel> channel,
91-
const Options& options);
90+
static std::shared_ptr<RobotClient> with_channel(ViamChannel channel, const Options& options);
9291

93-
RobotClient(std::shared_ptr<ViamChannel> channel);
92+
RobotClient(ViamChannel channel);
9493

9594
std::vector<Name> resource_names() const;
9695

@@ -170,8 +169,7 @@ class RobotClient {
170169
std::atomic<bool> should_refresh_;
171170
unsigned int refresh_interval_;
172171

173-
std::shared_ptr<GrpcChannel> channel_;
174-
std::shared_ptr<ViamChannel> viam_channel_;
172+
ViamChannel viam_channel_;
175173
bool should_close_channel_;
176174

177175
struct impl;

src/viam/sdk/rpc/dial.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ ViamChannel::ViamChannel(std::shared_ptr<grpc::Channel> channel, const char* pat
5050

5151
ViamChannel::ViamChannel(std::shared_ptr<grpc::Channel> channel) : channel_(std::move(channel)) {}
5252

53+
ViamChannel::ViamChannel(ViamChannel&&) noexcept = default;
54+
55+
ViamChannel& ViamChannel::operator=(ViamChannel&&) noexcept = default;
56+
5357
ViamChannel::~ViamChannel() {
5458
close();
5559
}
@@ -125,8 +129,8 @@ bool DialOptions::allows_insecure_downgrade() const {
125129
return allow_insecure_downgrade_;
126130
}
127131

128-
std::shared_ptr<ViamChannel> ViamChannel::dial_initial(
129-
const char* uri, const boost::optional<DialOptions>& options) {
132+
ViamChannel ViamChannel::dial_initial(const char* uri,
133+
const boost::optional<DialOptions>& options) {
130134
DialOptions opts = options.get_value_or(DialOptions());
131135
auto timeout = opts.timeout();
132136
auto attempts_remaining = opts.initial_connection_attempts();
@@ -149,11 +153,10 @@ std::shared_ptr<ViamChannel> ViamChannel::dial_initial(
149153
}
150154
// the while loop will run until we either return or throw an error, so we can never reach this
151155
// point
152-
BOOST_UNREACHABLE_RETURN(nullptr)
156+
BOOST_UNREACHABLE_RETURN(ViamChannel(nullptr))
153157
}
154158

155-
std::shared_ptr<ViamChannel> ViamChannel::dial(const char* uri,
156-
const boost::optional<DialOptions>& options) {
159+
ViamChannel ViamChannel::dial(const char* uri, const boost::optional<DialOptions>& options) {
157160
void* ptr = init_rust_runtime();
158161
const DialOptions opts = options.get_value_or(DialOptions());
159162
const std::chrono::duration<float> float_timeout = opts.timeout();
@@ -177,9 +180,10 @@ std::shared_ptr<ViamChannel> ViamChannel::dial(const char* uri,
177180

178181
std::string address("unix://");
179182
address += socket_path;
180-
const std::shared_ptr<grpc::Channel> channel =
181-
sdk::impl::create_viam_channel(address, grpc::InsecureChannelCredentials());
182-
return std::make_shared<ViamChannel>(channel, socket_path, ptr);
183+
184+
return ViamChannel(sdk::impl::create_viam_channel(address, grpc::InsecureChannelCredentials()),
185+
socket_path,
186+
ptr);
183187
}
184188

185189
const std::shared_ptr<grpc::Channel>& ViamChannel::channel() const {

src/viam/sdk/rpc/dial.hpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@ namespace sdk {
1313

1414
class DialOptions;
1515
class ViamChannel {
16+
ViamChannel(std::shared_ptr<GrpcChannel> channel, const char* path, void* runtime);
17+
1618
public:
1719
explicit ViamChannel(std::shared_ptr<GrpcChannel> channel);
1820

19-
ViamChannel(std::shared_ptr<GrpcChannel> channel, const char* path, void* runtime);
21+
ViamChannel(ViamChannel&&) noexcept;
22+
23+
ViamChannel& operator=(ViamChannel&&) noexcept;
2024

2125
~ViamChannel();
2226

@@ -26,8 +30,7 @@ class ViamChannel {
2630
/// In general, use of this method is discouraged. `RobotClient::at_address(...)` is the
2731
/// preferred method to connect to a robot, and creates the channel itself.
2832
/// @throws Exception if it is unable to establish a connection to the provided URI
29-
static std::shared_ptr<ViamChannel> dial(const char* uri,
30-
const boost::optional<DialOptions>& options);
33+
static ViamChannel dial(const char* uri, const boost::optional<DialOptions>& options);
3134

3235
// @brief Dials to a robot at the given URI address, using the provided dial options (or default
3336
// options is none are provided). Additionally specifies that this dial is an initial connection
@@ -36,8 +39,7 @@ class ViamChannel {
3639
/// preferred method to connect to a robot, and creates the channel itself.
3740
/// @throws Exception if it is unable to establish a connection to the provided URI within
3841
/// the given number of initial connection attempts
39-
static std::shared_ptr<ViamChannel> dial_initial(const char* uri,
40-
const boost::optional<DialOptions>& options);
42+
static ViamChannel dial_initial(const char* uri, const boost::optional<DialOptions>& options);
4143

4244
const std::shared_ptr<GrpcChannel>& channel() const;
4345

src/viam/sdk/tests/test_robot.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ void robot_client_to_mocks_pipeline(F&& test_case) {
5050
// in-process gRPC channel.
5151
auto test_server = TestServer(server);
5252
auto grpc_channel = test_server.grpc_in_process_channel();
53-
auto viam_channel = std::make_shared<ViamChannel>(grpc_channel);
54-
auto client = RobotClient::with_channel(viam_channel, Options(0, boost::none));
53+
auto client = RobotClient::with_channel(ViamChannel(grpc_channel), Options(0, boost::none));
5554

5655
// Run the passed-in test case on the created stack and give access to the
5756
// created RobotClient and MockRobotService.

0 commit comments

Comments
 (0)