Skip to content

Conversation

@stuqdog
Copy link
Member

@stuqdog stuqdog commented Feb 28, 2025

No description provided.

@stuqdog stuqdog requested a review from a team as a code owner February 28, 2025 20:41
@stuqdog stuqdog requested review from lia-viam and njooma and removed request for a team February 28, 2025 20:41
}

void DialOptions::set_initial_connection_attempt_timeout(std::chrono::duration<float> timeout) {
initial_connection_attempt_timeout_ = std::move(timeout);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor but you can remove the std::move here, a chrono::duration<T> is a T under the hood, no need to std::move a float.

I am seeing that this was also how it was done in set_timeout above, so maybe that can be a drive-by

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if clang-tidy will think of it as a missed move? Personally, I always move things like these, since otherwise it feels like I'm making assumptions about implementation.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I generally err on the side of move unless clang explicitly tells me not to do so just because it's simpler and makes me less likely to miss a move that is relevant but I don't feel particularly strongly. I'm happy to go with whatever makes sense to y'all!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ended up going with @lia-viam's suggestion just because it was the one that came in first. But again, I'm not picky and my slight inclination if anything is to keep moveing things even if it's unnecessary. So if you two land on the other approach I'm happy to change it back.

bool allows_insecure_downgrade() const;
const std::chrono::duration<float>& timeout() const;
int initial_connection_attempts() const;
const std::chrono::duration<float>& initial_connection_attempt_timeout() const;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's return this by value instead of ref to const; see other comment

auto connection = dial(uri, opts);
opts.set_timeout(timeout);
return connection;
} catch (...) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just catch const std::exception& here, a catch (...) is generally inadvisable except at application boundaries.

const boost::optional<DialOptions>& options,
bool initial_attempt) {
if (!initial_attempt) {
return dial(std::move(uri), options);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return dial(std::move(uri), options);
return dial(uri, options);

Copy link
Member

@acmorrow acmorrow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just taking a look, random thoughts, feel free to do with them as you see fit.

void set_credentials(boost::optional<Credentials> creds);
void set_allow_insecure_downgrade(bool allow);
void set_timeout(std::chrono::duration<float> timeout);
void set_initial_connection_attempts(int attempts);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A small proposal: these and the other set_x methods should return DialOptions& so you can chain them:

DialOptions options{};
options.set_entity(...).set_timeout(...)....;


// allows for specifying that this dial is an initial connection attempt, indicating that
// initial connection options should be used.
static std::shared_ptr<ViamChannel> dial(const char* uri,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have something of a personal dislike for bool parameters like this, and it makes me wonder why this initial aspect isn't either just a setting on the DialOptions, or a differently named method.

My distaste arrises because at the call site, you end up like this:

ViamChannel::dial(my_uri, my_options, false);

What does false mean?

Whereas either of these makes the intention clear at the call site without needing to chase down the declaration / documentation for dial:

ViamChannel::dial(my_uri, my_options.as_initial());

or

ViamChannel::dial_initial(my_uri, my_options)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the look of the as_initial approach but I think it requires creating a copy of the options. So, went with dial_initial.

}

void DialOptions::set_initial_connection_attempt_timeout(std::chrono::duration<float> timeout) {
initial_connection_attempt_timeout_ = std::move(timeout);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if clang-tidy will think of it as a missed move? Personally, I always move things like these, since otherwise it feels like I'm making assumptions about implementation.

@stuqdog stuqdog requested review from acmorrow and lia-viam March 3, 2025 21:17
}
}

throw Exception(ErrorCondition::k_connection, "Unable to establish connection");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this maybe instead throw whatever the last exception was, so that the information about the cause of the failure isn't lost?

static std::shared_ptr<ViamChannel> dial(const char* uri,
const boost::optional<DialOptions>& options);

// Specifies that this dial is an initial connection attempt and that initial connection options
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems as good a time as any to doc comment this function, as well as dial above.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done; added a clause as well clarifying that use of dial is discouraged and that RobotClient::at_address(...) is preferred.

@stuqdog stuqdog requested a review from acmorrow March 4, 2025 18:40
return connection;
} catch (const std::exception& e) {
attempts_remaining -= 1;
exception = e;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going to slice the exception, I think (i.e. only the std::exception part will be copied, but any subtype will be lost). I think you can just look at attempts_remaining inside the catch and throw if there are non left.

@stuqdog stuqdog requested a review from acmorrow March 4, 2025 19:03
Copy link
Member

@acmorrow acmorrow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@stuqdog stuqdog merged commit 3d58df1 into viamrobotics:main Mar 5, 2025
4 checks passed
@stuqdog stuqdog deleted the RSDK-6158-initial-connection-options branch March 5, 2025 16:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants