Skip to content

Commit

Permalink
fix grpc: migrate to new unary RPC API for clients (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton3 authored Dec 28, 2024
1 parent 2193366 commit e8dda6b
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/hello.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ std::string SayHelloTo(std::string_view name, UserType type) {
return fmt::format("Hi again, {}!\n", name);
}

UASSERT(false);
UINVARIANT(false, "Invalid user type");
}

void AppendHello(userver::components::ComponentList& component_list) {
Expand Down
16 changes: 2 additions & 14 deletions src/hello_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,8 @@ std::string HelloClient::SayHello(std::string name) {
handlers::api::HelloRequest request;
request.set_name(std::move(name));

// Deadline must be set manually for each RPC
auto context = std::make_unique<grpc::ClientContext>();
context->set_deadline(
userver::engine::Deadline::FromDuration(std::chrono::seconds{20}));

// Initiate the RPC. No actual actions have been taken thus far besides
// preparing to send the request.
auto stream = client_.SayHello(request, std::move(context));

// Complete the unary RPC by sending the request and receiving the response.
// The client should call `Finish` (in case of single response) or `Read`
// until `false` (in case of response stream), otherwise the RPC will be
// cancelled.
handlers::api::HelloResponse response = stream.Finish();
// Perform RPC by sending the request and receiving the response.
auto response = client_.SayHello(request);

return std::move(*response.mutable_text());
}
Expand Down

0 comments on commit e8dda6b

Please sign in to comment.