Skip to content

Commit 70b393e

Browse files
authored
RSDK-8424 - respect ctx deadline when adding module resources (viamrobotics#423)
1 parent faa73b6 commit 70b393e

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

src/viam/examples/modules/complex/proto/buf.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ deps:
44
- remote: buf.build
55
owner: googleapis
66
repository: googleapis
7-
commit: 751cbe31638d43a9bfb6162cd2352e67
8-
digest: shake256:87f55470d9d124e2d1dedfe0231221f4ed7efbc55bc5268917c678e2d9b9c41573a7f9a557f6d8539044524d9fc5ca8fbb7db05eb81379d168285d76b57eb8a4
7+
commit: 61b203b9a9164be9a834f58c37be6f62
8+
digest: shake256:e619113001d6e284ee8a92b1561e5d4ea89a47b28bf0410815cb2fa23914df8be9f1a6a98dcf069f5bc2d829a2cfb1ac614863be45cd4f8a5ad8606c5f200224

src/viam/sdk/module/service.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ struct ModuleService::ServiceImpl : viam::module::v1::ModuleService::Service {
5454
ModuleService& parent;
5555

5656
// TODO(RSDK-6528) - to the extent possible, switch to using `server_helper`
57-
::grpc::Status AddResource(::grpc::ServerContext*,
57+
::grpc::Status AddResource(::grpc::ServerContext* ctx,
5858
const ::viam::module::v1::AddResourceRequest* request,
5959
::viam::module::v1::AddResourceResponse*) override {
6060
const viam::app::v1::ComponentConfig& proto = request->config();
@@ -74,7 +74,7 @@ struct ModuleService::ServiceImpl : viam::module::v1::ModuleService::Service {
7474
}
7575
};
7676
try {
77-
parent.server_->add_resource(res);
77+
parent.server_->add_resource(res, ctx->deadline());
7878
} catch (const std::exception& exc) {
7979
return grpc::Status(::grpc::INTERNAL, exc.what());
8080
}

src/viam/sdk/rpc/server.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,21 @@ void Server::register_service(grpc::Service* service) {
4444
builder_->RegisterService(service);
4545
}
4646

47+
namespace {
48+
bool deadline_exceeded(const std::chrono::system_clock::time_point& deadline) {
49+
return deadline < std::chrono::system_clock::now(); // deadline is before now
50+
}
51+
} // namespace
52+
4753
void Server::add_resource(std::shared_ptr<Resource> resource) {
54+
add_resource(std::move(resource), boost::none);
55+
}
56+
57+
void Server::add_resource(std::shared_ptr<Resource> resource,
58+
boost::optional<std::chrono::system_clock::time_point> deadline) {
59+
if (deadline && deadline_exceeded(*deadline)) {
60+
throw Exception(ErrorCondition::k_general, "Deadline expired");
61+
}
4862
auto api = resource->api();
4963
if (managed_servers_.find(api) == managed_servers_.end()) {
5064
std::ostringstream buffer;

src/viam/sdk/rpc/server.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ class Server {
5050
/// @throws `Exception` if a matching `ResourceServer` doesn't exist in the server.
5151
void add_resource(std::shared_ptr<Resource> resource);
5252

53+
/// @brief Adds a specific managed resource to the associated resource server
54+
/// @param resource The resource to add
55+
/// @param deadline Deadline after which to not add the resource
56+
/// @throws `Exception` if a matching `ResourceServer` doesn't exist in the server.
57+
/// @throws `Exception` if the deadline is not nil and has passed
58+
void add_resource(std::shared_ptr<Resource> resource,
59+
boost::optional<std::chrono::system_clock::time_point> deadline);
60+
5361
/// @brief Adds a listening port to the server.
5462
/// @param address The address to listen at.
5563
/// @param creds The server credentials; defaults to a insecure server credentials.

0 commit comments

Comments
 (0)