Skip to content

Commit

Permalink
client context out of utils
Browse files Browse the repository at this point in the history
  • Loading branch information
lia-viam committed Dec 17, 2024
1 parent 7d26e94 commit 772efcb
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 58 deletions.
36 changes: 36 additions & 0 deletions src/viam/sdk/common/client_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

#include <cstdlib>

#include <grpcpp/client_context.h>

#include <boost/log/trivial.hpp>

#include <viam/sdk/common/private/version_metadata.hpp>

namespace viam {
namespace sdk {

Expand All @@ -16,5 +20,37 @@ namespace client_helper_details {
}

} // namespace client_helper_details

ClientContext::ClientContext() : wrapped_context_(std::make_unique<grpc::ClientContext>()) {
set_client_ctx_authority_();
add_viam_client_version_();
}

ClientContext::~ClientContext() = default;

ClientContext::operator const grpc::ClientContext*() const {
return wrapped_context_.get();
}

ClientContext::operator grpc::ClientContext*() {
return wrapped_context_.get();
}

void ClientContext::try_cancel() {
wrapped_context_->TryCancel();
}

void ClientContext::set_debug_key(const std::string& debug_key) {
wrapped_context_->AddMetadata("dtname", debug_key);
}

void ClientContext::set_client_ctx_authority_() {
wrapped_context_->set_authority("viam-placeholder");
}

void ClientContext::add_viam_client_version_() {
wrapped_context_->AddMetadata("viam_client", impl::k_version);
}

} // namespace sdk
} // namespace viam
24 changes: 23 additions & 1 deletion src/viam/sdk/common/client_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <viam/sdk/common/grpc_client_fwd.hpp>
#include <viam/sdk/common/private/utils.hpp>
#include <viam/sdk/common/proto_value.hpp>
#include <viam/sdk/common/utils.hpp>

namespace grpc {

Expand All @@ -21,6 +20,29 @@ namespace client_helper_details {

} // namespace client_helper_details

// the authority on a grpc::ClientContext is sometimes set to an invalid uri on mac, causing
// `rust-utils` to fail to process gRPC requests. This class provides a convenience wrapper around a
// grpc ClientContext that allows us to make any necessary modifications to authority or else where
// to avoid runtime issues.
// For more details, see https://viam.atlassian.net/browse/RSDK-5194.
class ClientContext {
public:
ClientContext();
~ClientContext();

void try_cancel();

operator grpc::ClientContext*();
operator const grpc::ClientContext*() const;

void set_debug_key(const std::string& debug_key);

private:
void set_client_ctx_authority_();
void add_viam_client_version_();
std::unique_ptr<grpc::ClientContext> wrapped_context_;
};

// Method type for a gRPC call that returns a response message type.
template <typename StubType, typename RequestType, typename ResponseType>
using SyncMethodType = ::grpc::Status (StubType::*)(::grpc::ClientContext*,
Expand Down
32 changes: 0 additions & 32 deletions src/viam/sdk/common/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include <viam/api/common/v1/common.pb.h>

#include <viam/sdk/common/private/utils.hpp>
#include <viam/sdk/common/private/version_metadata.hpp>
#include <viam/sdk/components/component.hpp>
#include <viam/sdk/registry/registry.hpp>

Expand Down Expand Up @@ -151,37 +150,6 @@ ProtoStruct with_debug_entry(ProtoStruct&& map) {
return map;
}

ClientContext::ClientContext() : wrapped_context_(std::make_unique<grpc::ClientContext>()) {
set_client_ctx_authority_();
add_viam_client_version_();
}

ClientContext::~ClientContext() = default;

ClientContext::operator const grpc::ClientContext*() const {
return wrapped_context_.get();
}

ClientContext::operator grpc::ClientContext*() {
return wrapped_context_.get();
}

void ClientContext::try_cancel() {
wrapped_context_->TryCancel();
}

void ClientContext::set_debug_key(const std::string& debug_key) {
wrapped_context_->AddMetadata("dtname", debug_key);
}

void ClientContext::set_client_ctx_authority_() {
wrapped_context_->set_authority("viam-placeholder");
}

void ClientContext::add_viam_client_version_() {
wrapped_context_->AddMetadata("viam_client", impl::k_version);
}

bool from_dm_from_extra(const ProtoStruct& extra) {
auto pos = extra.find("fromDataManagement");
if (pos != extra.end()) {
Expand Down
24 changes: 0 additions & 24 deletions src/viam/sdk/common/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include <boost/optional/optional.hpp>

#include <viam/sdk/common/grpc_client_fwd.hpp>
#include <viam/sdk/common/proto_value.hpp>
#include <viam/sdk/components/component.hpp>
#include <viam/sdk/resource/resource_api.hpp>
Expand Down Expand Up @@ -82,29 +81,6 @@ struct from_proto<common::v1::ResponseMetadata> {
std::vector<unsigned char> string_to_bytes(std::string const& s);
std::string bytes_to_string(std::vector<unsigned char> const& b);

// the authority on a grpc::ClientContext is sometimes set to an invalid uri on mac, causing
// `rust-utils` to fail to process gRPC requests. This class provides a convenience wrapper around a
// grpc ClientContext that allows us to make any necessary modifications to authority or else where
// to avoid runtime issues.
// For more details, see https://viam.atlassian.net/browse/RSDK-5194.
class ClientContext {
public:
ClientContext();
~ClientContext();

void try_cancel();

operator grpc::ClientContext*();
operator const grpc::ClientContext*() const;

void set_debug_key(const std::string& debug_key);

private:
void set_client_ctx_authority_();
void add_viam_client_version_();
std::unique_ptr<grpc::ClientContext> wrapped_context_;
};

/// @brief Given a fully qualified resource name, returns remote name (or "" if no remote name
/// exists) and short name
std::pair<std::string, std::string> long_name_to_remote_and_short(const std::string& long_name);
Expand Down
1 change: 1 addition & 0 deletions src/viam/sdk/robot/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <viam/api/robot/v1/robot.grpc.pb.h>
#include <viam/api/robot/v1/robot.pb.h>

#include <viam/sdk/common/client_helper.hpp>
#include <viam/sdk/common/private/repeated_ptr_convert.hpp>
#include <viam/sdk/common/proto_value.hpp>
#include <viam/sdk/common/utils.hpp>
Expand Down
2 changes: 1 addition & 1 deletion src/viam/sdk/services/private/mlmodel_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

#include <grpcpp/channel.h>

#include <viam/sdk/common/utils.hpp>
#include <viam/sdk/common/client_helper.hpp>
#include <viam/sdk/services/private/mlmodel.hpp>

#include <viam/sdk/common/exception.hpp>
Expand Down

0 comments on commit 772efcb

Please sign in to comment.