diff --git a/grpc/benchmarks/base.cpp b/grpc/benchmarks/base.cpp index 382bc114752e..2f8a0ab07d0f 100644 --- a/grpc/benchmarks/base.cpp +++ b/grpc/benchmarks/base.cpp @@ -32,47 +32,49 @@ namespace { class UnitTestService final : public sample::ugrpc::UnitTestServiceBase { public: - void SayHello(SayHelloCall& call, - sample::ugrpc::GreetingRequest&& request) override { + SayHelloResult SayHello(CallContext& /*context*/, + sample::ugrpc::GreetingRequest&& request) override { sample::ugrpc::GreetingResponse response; response.set_name("Hello " + request.name()); - call.Finish(response); + return response; } - void ReadMany(ReadManyCall& call, - sample::ugrpc::StreamGreetingRequest&& request) override { + ReadManyResult ReadMany(CallContext& /*context*/, + sample::ugrpc::StreamGreetingRequest&& request, + ReadManyWriter& writer) override { sample::ugrpc::StreamGreetingResponse response; response.set_name("Hello again " + request.name()); for (int i = 0; i < request.number(); ++i) { response.set_number(i); - call.Write(response); + writer.Write(response); } - call.Finish(); + return grpc::Status::OK; } - void WriteMany(WriteManyCall& call) override { + WriteManyResult WriteMany(CallContext& /*context*/, + WriteManyReader& reader) override { sample::ugrpc::StreamGreetingRequest request; int count = 0; - while (call.Read(request)) { + while (reader.Read(request)) { ++count; } sample::ugrpc::StreamGreetingResponse response; response.set_name("Hello"); response.set_number(count); - call.Finish(response); + return response; } - void Chat(ChatCall& call) override { + ChatResult Chat(CallContext& /*context*/, ChatReaderWriter& stream) override { sample::ugrpc::StreamGreetingRequest request; sample::ugrpc::StreamGreetingResponse response; int count = 0; - while (call.Read(request)) { + while (stream.Read(request)) { ++count; response.set_number(count); response.set_name("Hello " + request.name()); - call.Write(response); + stream.Write(response); } - call.Finish(); + return grpc::Status::OK; } }; diff --git a/grpc/functional_tests/basic_chaos/service.hpp b/grpc/functional_tests/basic_chaos/service.hpp index 7d42f9245aa9..4afa8afae671 100644 --- a/grpc/functional_tests/basic_chaos/service.hpp +++ b/grpc/functional_tests/basic_chaos/service.hpp @@ -32,18 +32,22 @@ class GreeterServiceComponent final : api::GreeterServiceBase::Component(config, context), prefix_(config["greeting-prefix"].As()) {} - inline void SayHello(SayHelloCall& call, - api::GreetingRequest&& request) final; + inline SayHelloResult SayHello(CallContext& context, + api::GreetingRequest&& request) final; - inline void SayHelloResponseStream(SayHelloResponseStreamCall& call, - api::GreetingRequest&& request) final; + inline SayHelloResponseStreamResult SayHelloResponseStream( + CallContext& context, api::GreetingRequest&& request, + SayHelloResponseStreamWriter& writer) final; - inline void SayHelloRequestStream(SayHelloRequestStreamCall& call) final; + inline SayHelloRequestStreamResult SayHelloRequestStream( + CallContext& context, SayHelloRequestStreamReader& reader) final; - inline void SayHelloStreams(SayHelloStreamsCall& call) final; + inline SayHelloStreamsResult SayHelloStreams( + CallContext& context, SayHelloStreamsReaderWriter& stream) final; - inline void SayHelloIndependentStreams( - SayHelloIndependentStreamsCall& call) final; + inline SayHelloIndependentStreamsResult SayHelloIndependentStreams( + CallContext& context, + SayHelloIndependentStreamsReaderWriter& stream) final; inline static yaml_config::Schema GetStaticConfigSchema(); @@ -51,9 +55,8 @@ class GreeterServiceComponent final const std::string prefix_; }; -void GreeterServiceComponent::SayHello( - api::GreeterServiceBase::SayHelloCall& call, - api::GreetingRequest&& request) { +GreeterServiceComponent::SayHelloResult GreeterServiceComponent::SayHello( + CallContext& /*context*/, api::GreetingRequest&& request) { api::GreetingResponse response; if (request.name() == "test_payload_cancellation") { @@ -65,11 +68,13 @@ void GreeterServiceComponent::SayHello( } response.set_greeting(fmt::format("{}, {}!", prefix_, request.name())); - call.Finish(response); + return response; } -void GreeterServiceComponent::SayHelloResponseStream( - SayHelloResponseStreamCall& call, api::GreetingRequest&& request) { +GreeterServiceComponent::SayHelloResponseStreamResult +GreeterServiceComponent::SayHelloResponseStream( + CallContext& /*context*/, api::GreetingRequest&& request, + SayHelloResponseStreamWriter& writer) { std::string message = fmt::format("{}, {}", prefix_, request.name()); api::GreetingResponse response; constexpr auto kCountSend = 5; @@ -78,60 +83,64 @@ void GreeterServiceComponent::SayHelloResponseStream( message.push_back('!'); response.set_greeting(grpc::string(message)); engine::SleepFor(kTimeInterval); - call.Write(response); + writer.Write(response); } - call.Finish(); + return grpc::Status::OK; } -void GreeterServiceComponent::SayHelloRequestStream( - SayHelloRequestStreamCall& call) { +GreeterServiceComponent::SayHelloRequestStreamResult +GreeterServiceComponent::SayHelloRequestStream( + CallContext& /*context*/, SayHelloRequestStreamReader& reader) { std::string income_message; api::GreetingRequest request; - while (call.Read(request)) { + while (reader.Read(request)) { income_message.append(request.name()); } api::GreetingResponse response; response.set_greeting(fmt::format("{}, {}", prefix_, income_message)); - call.Finish(response); + return response; } -void GreeterServiceComponent::SayHelloStreams(SayHelloStreamsCall& call) { +GreeterServiceComponent::SayHelloStreamsResult +GreeterServiceComponent::SayHelloStreams(CallContext& /*context*/, + SayHelloStreamsReaderWriter& stream) { constexpr std::chrono::milliseconds kTimeInterval{200}; std::string income_message; api::GreetingRequest request; api::GreetingResponse response; - while (call.Read(request)) { + while (stream.Read(request)) { income_message.append(request.name()); response.set_greeting(fmt::format("{}, {}", prefix_, income_message)); engine::SleepFor(kTimeInterval); - call.Write(response); + stream.Write(response); } - call.Finish(); + return grpc::Status::OK; } -void GreeterServiceComponent::SayHelloIndependentStreams( - SayHelloIndependentStreamsCall& call) { +GreeterServiceComponent::SayHelloIndependentStreamsResult +GreeterServiceComponent::SayHelloIndependentStreams( + CallContext& /*context*/, SayHelloIndependentStreamsReaderWriter& stream) { constexpr std::chrono::milliseconds kTimeIntervalRead{200}; constexpr std::chrono::milliseconds kTimeIntervalWrite{300}; std::string final_string{}; auto read_task = - engine::AsyncNoSpan([&final_string, &call, &kTimeIntervalRead] { + engine::AsyncNoSpan([&final_string, &stream, &kTimeIntervalRead] { api::GreetingRequest request; - while (call.Read(request)) { + while (stream.Read(request)) { final_string.append(request.name()); engine::SleepFor(kTimeIntervalRead); } }); - auto write_task = engine::AsyncNoSpan([&call, prefix = prefix_, + auto write_task = engine::AsyncNoSpan([&stream, prefix = prefix_, &kTimeIntervalWrite] { api::GreetingResponse response; std::array kNames = {"Python", "C++", "linux", "userver", "grpc", "kernel", "developer", "core", "anonymous", "user"}; for (const auto& name : kNames) { response.set_greeting(fmt::format("{}, {}", prefix, name)); - call.Write(response); + stream.Write(response); engine::SleepFor(kTimeIntervalWrite); } }); @@ -141,7 +150,8 @@ void GreeterServiceComponent::SayHelloIndependentStreams( api::GreetingResponse response; response.set_greeting(grpc::string(final_string)); - call.WriteAndFinish(response); + stream.Write(response); + return grpc::Status::OK; } yaml_config::Schema GreeterServiceComponent::GetStaticConfigSchema() { diff --git a/grpc/functional_tests/basic_server/grpc_service.cpp b/grpc/functional_tests/basic_server/grpc_service.cpp index 3e76da5105a7..3cdeced72d39 100644 --- a/grpc/functional_tests/basic_server/grpc_service.cpp +++ b/grpc/functional_tests/basic_server/grpc_service.cpp @@ -37,10 +37,11 @@ class GreeterServiceComponent final http_client_( context.FindComponent().GetHttpClient()) {} - void SayHello(SayHelloCall& call, api::GreetingRequest&& request) override; + SayHelloResult SayHello(CallContext& context, + api::GreetingRequest&& request) override; - void CallEchoNobody(CallEchoNobodyCall& call, - api::GreetingRequest&& request) override; + CallEchoNobodyResult CallEchoNobody(CallContext& context, + api::GreetingRequest&& request) override; static yaml_config::Schema GetStaticConfigSchema(); @@ -49,17 +50,16 @@ class GreeterServiceComponent final clients::http::Client& http_client_; }; -void GreeterServiceComponent::SayHello( - api::GreeterServiceBase::SayHelloCall& call, - api::GreetingRequest&& request) { +GreeterServiceComponent::SayHelloResult GreeterServiceComponent::SayHello( + CallContext& /*context*/, api::GreetingRequest&& request) { api::GreetingResponse response; response.set_greeting(fmt::format("Hello, {}!", request.name())); - call.Finish(response); + return response; } -void GreeterServiceComponent::CallEchoNobody( - api::GreeterServiceBase::CallEchoNobodyCall& call, - samples::api::GreetingRequest&&) { +GreeterServiceComponent::CallEchoNobodyResult +GreeterServiceComponent::CallEchoNobody(CallContext& /*context*/, + api::GreetingRequest&& /*request*/) { api::GreetingResponse response; response.set_greeting("Call Echo Nobody"); auto http_response = http_client_.CreateRequest() @@ -68,7 +68,7 @@ void GreeterServiceComponent::CallEchoNobody( .timeout(std::chrono::seconds{5}) .perform(); http_response->raise_for_status(); - call.Finish(response); + return response; } yaml_config::Schema GreeterServiceComponent::GetStaticConfigSchema() { diff --git a/grpc/functional_tests/metrics/src/greeter_service.hpp b/grpc/functional_tests/metrics/src/greeter_service.hpp index 3430701fb87e..258b72eb19d8 100644 --- a/grpc/functional_tests/metrics/src/greeter_service.hpp +++ b/grpc/functional_tests/metrics/src/greeter_service.hpp @@ -20,24 +20,24 @@ class GreeterServiceComponent final : samples::api::GreeterServiceBase::Component(config, context), client_(context.FindComponent()) {} - void SayHello(SayHelloCall& call, - samples::api::GreetingRequest&& request) override; + inline SayHelloResult SayHello( + CallContext& context, samples::api::GreetingRequest&& request) override; - static yaml_config::Schema GetStaticConfigSchema(); + inline static yaml_config::Schema GetStaticConfigSchema(); private: GreeterClient& client_; }; -inline void GreeterServiceComponent::SayHello( - SayHelloCall& call, samples::api::GreetingRequest&& request) { +GreeterServiceComponent::SayHelloResult GreeterServiceComponent::SayHello( + CallContext& /*context*/, samples::api::GreetingRequest&& request) { samples::api::GreetingResponse response; const auto greeting = client_.SayHello(request.name()); response.set_greeting("FWD: " + greeting); - call.Finish(response); + return response; } -inline yaml_config::Schema GreeterServiceComponent::GetStaticConfigSchema() { +yaml_config::Schema GreeterServiceComponent::GetStaticConfigSchema() { return yaml_config::MergeSchemas(R"( type: object description: gRPC sample greater service component diff --git a/grpc/functional_tests/middleware_server/src/service.cpp b/grpc/functional_tests/middleware_server/src/service.cpp index 9e19de92a3a2..f8bb5cc4a965 100644 --- a/grpc/functional_tests/middleware_server/src/service.cpp +++ b/grpc/functional_tests/middleware_server/src/service.cpp @@ -10,16 +10,17 @@ namespace functional_tests { -void GreeterServiceComponent::SayHello( - samples::api::GreeterServiceBase::SayHelloCall& call, - samples::api::GreetingRequest&& request) { +GreeterServiceComponent::SayHelloResult GreeterServiceComponent::SayHello( + CallContext& /*context*/, samples::api::GreetingRequest&& request) { samples::api::GreetingResponse response; response.set_greeting(fmt::format("Hello, {}!", request.name())); - call.Finish(response); + return response; } -void GreeterServiceComponent::SayHelloResponseStream( - SayHelloResponseStreamCall& call, samples::api::GreetingRequest&& request) { +GreeterServiceComponent::SayHelloResponseStreamResult +GreeterServiceComponent::SayHelloResponseStream( + CallContext& /*context*/, samples::api::GreetingRequest&& request, + SayHelloResponseStreamWriter& writer) { std::string message = fmt::format("{}, {}", "Hello", request.name()); samples::api::GreetingResponse response; constexpr auto kCountSend = 5; @@ -28,35 +29,38 @@ void GreeterServiceComponent::SayHelloResponseStream( message.push_back('!'); response.set_greeting(grpc::string(message)); engine::SleepFor(kTimeInterval); - call.Write(response); + writer.Write(response); } - call.Finish(); + return grpc::Status::OK; } -void GreeterServiceComponent::SayHelloRequestStream( - SayHelloRequestStreamCall& call) { +GreeterServiceComponent::SayHelloRequestStreamResult +GreeterServiceComponent::SayHelloRequestStream( + CallContext& /*context*/, SayHelloRequestStreamReader& reader) { std::string income_message; samples::api::GreetingRequest request; - while (call.Read(request)) { + while (reader.Read(request)) { income_message.append(request.name()); } samples::api::GreetingResponse response; response.set_greeting(fmt::format("{}, {}", "Hello", income_message)); - call.Finish(response); + return response; } -void GreeterServiceComponent::SayHelloStreams(SayHelloStreamsCall& call) { +GreeterServiceComponent::SayHelloStreamsResult +GreeterServiceComponent::SayHelloStreams(CallContext& /*context*/, + SayHelloStreamsReaderWriter& stream) { constexpr std::chrono::milliseconds kTimeInterval{200}; std::string income_message; samples::api::GreetingRequest request; samples::api::GreetingResponse response; - while (call.Read(request)) { + while (stream.Read(request)) { income_message.append(request.name()); response.set_greeting(fmt::format("{}, {}", "Hello", income_message)); engine::SleepFor(kTimeInterval); - call.Write(response); + stream.Write(response); } - call.Finish(); + return grpc::Status::OK; } yaml_config::Schema GreeterServiceComponent::GetStaticConfigSchema() { diff --git a/grpc/functional_tests/middleware_server/src/service.hpp b/grpc/functional_tests/middleware_server/src/service.hpp index 006f411fcb29..f4f877011aed 100644 --- a/grpc/functional_tests/middleware_server/src/service.hpp +++ b/grpc/functional_tests/middleware_server/src/service.hpp @@ -15,15 +15,18 @@ class GreeterServiceComponent final const components::ComponentContext& context) : samples::api::GreeterServiceBase::Component(config, context) {} - void SayHello(SayHelloCall& call, - samples::api::GreetingRequest&& request) final; + SayHelloResult SayHello(CallContext& context, + samples::api::GreetingRequest&& request) final; - void SayHelloResponseStream(SayHelloResponseStreamCall& call, - samples::api::GreetingRequest&& request) final; + SayHelloResponseStreamResult SayHelloResponseStream( + CallContext& context, samples::api::GreetingRequest&& request, + SayHelloResponseStreamWriter& writer) final; - void SayHelloRequestStream(SayHelloRequestStreamCall& call) final; + SayHelloRequestStreamResult SayHelloRequestStream( + CallContext& context, SayHelloRequestStreamReader& reader) final; - void SayHelloStreams(SayHelloStreamsCall& call) final; + SayHelloStreamsResult SayHelloStreams( + CallContext& context, SayHelloStreamsReaderWriter& stream) final; static yaml_config::Schema GetStaticConfigSchema(); }; diff --git a/grpc/handlers/include/userver/ugrpc/server/health/health.hpp b/grpc/handlers/include/userver/ugrpc/server/health/health.hpp index e8507c115f5d..97fd5ac2d525 100644 --- a/grpc/handlers/include/userver/ugrpc/server/health/health.hpp +++ b/grpc/handlers/include/userver/ugrpc/server/health/health.hpp @@ -13,8 +13,8 @@ class HealthHandler final : public grpc::health::v1::HealthBase { public: explicit HealthHandler(const components::ComponentContext& context); - void Check(CheckCall& call, - ::grpc::health::v1::HealthCheckRequest&& request) override; + CheckResult Check(CallContext& context, + ::grpc::health::v1::HealthCheckRequest&& request) override; private: const components::State components_; diff --git a/grpc/handlers/src/ugrpc/server/health/health.cpp b/grpc/handlers/src/ugrpc/server/health/health.cpp index c0d4d7aab744..580ca72ef064 100644 --- a/grpc/handlers/src/ugrpc/server/health/health.cpp +++ b/grpc/handlers/src/ugrpc/server/health/health.cpp @@ -9,14 +9,16 @@ namespace ugrpc::server { HealthHandler::HealthHandler(const components::ComponentContext& context) : components_(context) {} -void HealthHandler::Check(CheckCall& call, - ::grpc::health::v1::HealthCheckRequest&&) { +HealthHandler::CheckResult HealthHandler::Check( + [[maybe_unused]] CallContext& context, + ::grpc::health::v1::HealthCheckRequest&&) { ::grpc::health::v1::HealthCheckResponse response; - if (components_.IsAnyComponentInFatalState()) + if (components_.IsAnyComponentInFatalState()) { response.set_status(::grpc::health::v1::HealthCheckResponse::NOT_SERVING); - else + } else { response.set_status(::grpc::health::v1::HealthCheckResponse::SERVING); - call.Finish(response); + } + return response; } } // namespace ugrpc::server diff --git a/otlp/tests/service_test.cpp b/otlp/tests/service_test.cpp index 880833a34036..929b169ad92f 100644 --- a/otlp/tests/service_test.cpp +++ b/otlp/tests/service_test.cpp @@ -40,8 +40,8 @@ class Service : public ugrpc::tests::ServiceBase { class LogService final : public opentelemetry::proto::collector::logs::v1::LogsServiceBase { public: - void Export( - ExportCall& call, + ExportResult Export( + CallContext& /*context*/, ::opentelemetry::proto::collector::logs::v1::ExportLogsServiceRequest&& request) override { // Don't emit new traces to avoid recursive traces/logs @@ -55,7 +55,8 @@ class LogService final } } - call.Finish({}); + return ::opentelemetry::proto::collector::logs::v1:: + ExportLogsServiceResponse{}; } // no sync as there is only a single grpc client @@ -65,8 +66,8 @@ class LogService final class TraceService final : public opentelemetry::proto::collector::trace::v1::TraceServiceBase { public: - void Export( - ExportCall& call, + ExportResult Export( + CallContext& /*context*/, ::opentelemetry::proto::collector::trace::v1::ExportTraceServiceRequest&& request) override { // Don't emit new traces to avoid recursive traces/logs @@ -80,7 +81,8 @@ class TraceService final } } - call.Finish({}); + return ::opentelemetry::proto::collector::trace::v1:: + ExportTraceServiceResponse{}; } // no sync as there is only a single grpc client diff --git a/samples/grpc-generic-proxy/src/proxy_service.cpp b/samples/grpc-generic-proxy/src/proxy_service.cpp index 453c4329214c..4acf09933dc1 100644 --- a/samples/grpc-generic-proxy/src/proxy_service.cpp +++ b/samples/grpc-generic-proxy/src/proxy_service.cpp @@ -47,7 +47,8 @@ ProxyService::ProxyService(const components::ComponentConfig& config, ugrpc::client::GenericClient>>("generic-client") .GetClient()) {} -void ProxyService::Handle(Call& call) { +ProxyService::GenericResult ProxyService::Handle(GenericCallContext& context, + GenericReaderWriter& stream) { // In this example we proxy any unary RPC to client_, adding some metadata. // By default, generic service metrics are written with labels corresponding @@ -55,36 +56,33 @@ void ProxyService::Handle(Call& call) { // In this example, we accept the OOM potential and store metrics per // the actual call name. // Read docs on ugrpc::server::GenericServiceBase for details. - call.SetMetricsCallName(call.GetCallName()); + context.SetMetricsCallName(context.GetCallName()); grpc::ByteBuffer request_bytes; // Read might throw on a broken RPC, just rethrow then. - if (!call.Read(request_bytes)) { + if (!stream.Read(request_bytes)) { // The client has already called WritesDone. // We expect exactly 1 request, so that's an error for us. - call.FinishWithError(grpc::Status{grpc::StatusCode::INVALID_ARGUMENT, - "Expected exactly 1 request, given: 0"}); - return; + return grpc::Status{grpc::StatusCode::INVALID_ARGUMENT, + "Expected exactly 1 request, given: 0"}; } grpc::ByteBuffer ignored_request_bytes; // Wait until the client calls WritesDone before proceeding so that we know // that no misuse will occur later. For unary RPCs, clients will essentially // call WritesDone implicitly. - if (call.Read(ignored_request_bytes)) { - call.FinishWithError( - grpc::Status{grpc::StatusCode::INVALID_ARGUMENT, - "Expected exactly 1 request, given: at least 2"}); - return; + if (stream.Read(ignored_request_bytes)) { + return grpc::Status{grpc::StatusCode::INVALID_ARGUMENT, + "Expected exactly 1 request, given: at least 2"}; } auto client_context = std::make_unique(); - ProxyRequestMetadata(call.GetContext(), *client_context); + ProxyRequestMetadata(context.GetServerContext(), *client_context); // Deadline propagation will work, as we've registered the DP middleware // in the config of grpc-server component. // Optionally, we can set an additional timeout using GenericOptions::qos. - auto client_rpc = client_.UnaryCall(call.GetCallName(), request_bytes, + auto client_rpc = client_.UnaryCall(context.GetCallName(), request_bytes, std::move(client_context)); grpc::ByteBuffer response_bytes; @@ -92,24 +90,24 @@ void ProxyService::Handle(Call& call) { response_bytes = client_rpc.Finish(); } catch (const ugrpc::client::ErrorWithStatus& ex) { // Proxy the error returned from client. - ProxyTrailingResponseMetadata(client_rpc.GetContext(), call.GetContext()); - call.FinishWithError(ex.GetStatus()); - return; + ProxyTrailingResponseMetadata(client_rpc.GetContext(), + context.GetServerContext()); + return ex.GetStatus(); } catch (const ugrpc::client::RpcError& ex) { // Either the upstream client has cancelled our server RPC, or a network // failure has occurred, or the deadline has expired. See: // * ugrpc::client::RpcInterruptedError // * ugrpc::client::RpcCancelledError LOG_WARNING() << "Client RPC has failed: " << ex; - call.FinishWithError(grpc::Status{grpc::StatusCode::UNAVAILABLE, - "Failed to proxy the request"}); - return; + return grpc::Status{grpc::StatusCode::UNAVAILABLE, + "Failed to proxy the request"}; } - ProxyTrailingResponseMetadata(client_rpc.GetContext(), call.GetContext()); + ProxyTrailingResponseMetadata(client_rpc.GetContext(), + context.GetServerContext()); - // WriteAndFinish might throw on a broken RPC, just rethrow then. - call.WriteAndFinish(response_bytes); + // on success just return response from client + return response_bytes; } } // namespace samples diff --git a/samples/grpc-generic-proxy/src/proxy_service.hpp b/samples/grpc-generic-proxy/src/proxy_service.hpp index d655d078cb01..7514e8624ce6 100644 --- a/samples/grpc-generic-proxy/src/proxy_service.hpp +++ b/samples/grpc-generic-proxy/src/proxy_service.hpp @@ -16,7 +16,8 @@ class ProxyService final : public ugrpc::server::GenericServiceBase::Component { ProxyService(const components::ComponentConfig& config, const components::ComponentContext& context); - void Handle(Call& call) override; + GenericResult Handle(GenericCallContext& context, + GenericReaderWriter& stream) override; private: ugrpc::client::GenericClient& client_; diff --git a/samples/grpc_middleware_service/src/service/view.cpp b/samples/grpc_middleware_service/src/service/view.cpp index 1bf144daa961..ba92abeb3f74 100644 --- a/samples/grpc_middleware_service/src/service/view.cpp +++ b/samples/grpc_middleware_service/src/service/view.cpp @@ -11,13 +11,12 @@ GreeterServiceComponent::GreeterServiceComponent( : api::GreeterServiceBase::Component(config, context), prefix_(config["greeting-prefix"].As()) {} -void GreeterServiceComponent::SayHello( - api::GreeterServiceBase::SayHelloCall& call, - api::GreetingRequest&& request) { +GreeterServiceComponent::SayHelloResult GreeterServiceComponent::SayHello( + CallContext& /*context*/, api::GreetingRequest&& request) { api::GreetingResponse response; response.set_greeting(fmt::format("{}, {}!", prefix_, request.name())); - call.Finish(response); + return response; } yaml_config::Schema GreeterServiceComponent::GetStaticConfigSchema() { diff --git a/samples/grpc_middleware_service/src/service/view.hpp b/samples/grpc_middleware_service/src/service/view.hpp index 86bd5b397188..e00134c1ae33 100644 --- a/samples/grpc_middleware_service/src/service/view.hpp +++ b/samples/grpc_middleware_service/src/service/view.hpp @@ -17,7 +17,8 @@ class GreeterServiceComponent final GreeterServiceComponent(const components::ComponentConfig& config, const components::ComponentContext& context); - void SayHello(SayHelloCall& call, api::GreetingRequest&& request) override; + SayHelloResult SayHello(CallContext& context, + api::GreetingRequest&& request) override; static yaml_config::Schema GetStaticConfigSchema(); diff --git a/samples/grpc_service/src/greeter_service.cpp b/samples/grpc_service/src/greeter_service.cpp index a200a0635d63..e6ef9211f539 100644 --- a/samples/grpc_service/src/greeter_service.cpp +++ b/samples/grpc_service/src/greeter_service.cpp @@ -24,63 +24,63 @@ GreeterService::GreeterService(std::string prefix) : prefix_(std::move(prefix)) {} /// [server RPC handling] -void GreeterService::SayHello(api::GreeterServiceBase::SayHelloCall& call, - api::GreetingRequest&& request) { +GreeterService::SayHelloResult GreeterService::SayHello( + CallContext& /*context*/, api::GreetingRequest&& request) { // Authentication checking could have gone here. // Or even better, use a global gRPC authentication middleware. api::GreetingResponse response; response.set_greeting(fmt::format("{}, {}!", prefix_, request.name())); - // Complete the RPC by sending the response. The service should complete - // each request by calling `Finish` or `FinishWithError`, otherwise the - // client will receive an Internal Error (500) response. - call.Finish(response); + // Complete the RPC by returning the response. + return response; } /// [server RPC handling] /// [server RPC handling response_stream] -void GreeterService::SayHelloResponseStream( - api::GreeterServiceBase::SayHelloResponseStreamCall& call, - api::GreetingRequest&& request) { +GreeterService::SayHelloResponseStreamResult +GreeterService::SayHelloResponseStream(CallContext& /*context*/, + api::GreetingRequest&& request, + SayHelloResponseStreamWriter& writer) { std::string message = fmt::format("{}, {}", prefix_, request.name()); api::GreetingResponse response; constexpr auto kCountSend = 5; for (auto i = 0; i < kCountSend; ++i) { message.push_back('!'); response.set_greeting(grpc::string(message)); - call.Write(response); + writer.Write(response); } - call.Finish(); + return grpc::Status::OK; } /// [server RPC handling response_stream] /// [server RPC handling request_stream] -void GreeterService::SayHelloRequestStream( - api::GreeterServiceBase::SayHelloRequestStreamCall& call) { +GreeterService::SayHelloRequestStreamResult +GreeterService::SayHelloRequestStream(CallContext& /*context*/, + SayHelloRequestStreamReader& reader) { std::string message{}; api::GreetingRequest request; - while (call.Read(request)) { + while (reader.Read(request)) { message.append(request.name()); } api::GreetingResponse response; response.set_greeting(fmt::format("{}, {}", prefix_, message)); - call.Finish(response); + return response; } /// [server RPC handling request_stream] /// [server RPC handling streams] -void GreeterService::SayHelloStreams( - api::GreeterServiceBase::SayHelloStreamsCall& call) { +GreeterService::SayHelloStreamsResult GreeterService::SayHelloStreams( + CallContext& /*context*/, SayHelloStreamsReaderWriter& stream) { std::string message; api::GreetingRequest request; api::GreetingResponse response; - while (call.Read(request)) { + while (stream.Read(request)) { message.append(request.name()); response.set_greeting(fmt::format("{}, {}", prefix_, message)); - call.Write(response); + stream.Write(response); } - call.Finish(); + return grpc::Status::OK; } /// [server RPC handling streams] diff --git a/samples/grpc_service/src/greeter_service.hpp b/samples/grpc_service/src/greeter_service.hpp index 9407b39ea8b8..d3c42e35be37 100644 --- a/samples/grpc_service/src/greeter_service.hpp +++ b/samples/grpc_service/src/greeter_service.hpp @@ -17,14 +17,18 @@ class GreeterService final : public api::GreeterServiceBase { public: explicit GreeterService(std::string prefix); - void SayHello(SayHelloCall& call, api::GreetingRequest&& request) override; + SayHelloResult SayHello(CallContext& context, + api::GreetingRequest&& request) override; - void SayHelloResponseStream(SayHelloResponseStreamCall& call, - api::GreetingRequest&& request) override; + SayHelloResponseStreamResult SayHelloResponseStream( + CallContext& context, api::GreetingRequest&& request, + SayHelloResponseStreamWriter& writer) override; - void SayHelloRequestStream(SayHelloRequestStreamCall& call) override; + SayHelloRequestStreamResult SayHelloRequestStream( + CallContext& context, SayHelloRequestStreamReader& reader) override; - void SayHelloStreams(SayHelloStreamsCall& call) override; + SayHelloStreamsResult SayHelloStreams( + CallContext& context, SayHelloStreamsReaderWriter& stream) override; private: const std::string prefix_; diff --git a/samples/grpc_service/unittests/greeter_service_test.cpp b/samples/grpc_service/unittests/greeter_service_test.cpp index dac46a46a01a..c6de1171f3e2 100644 --- a/samples/grpc_service/unittests/greeter_service_test.cpp +++ b/samples/grpc_service/unittests/greeter_service_test.cpp @@ -95,18 +95,21 @@ namespace { class GreeterMock final : public samples::api::GreeterServiceBase { public: - void SayHello(SayHelloCall& call, - samples::api::GreetingRequest&& /*request*/) override { + SayHelloResult SayHello( + CallContext& /*context*/, + ::samples::api::GreetingRequest&& /*request*/) override { samples::api::GreetingResponse response; response.set_greeting("Mocked response"); - call.Finish(response); + return response; } - void SayHelloResponseStream( - SayHelloResponseStreamCall& call, - samples::api::GreetingRequest&& /*request*/) override; - void SayHelloRequestStream(SayHelloRequestStreamCall& call) override; - void SayHelloStreams(SayHelloStreamsCall& call) override; + SayHelloResponseStreamResult SayHelloResponseStream( + CallContext& context, ::samples::api::GreetingRequest&& request, + SayHelloResponseStreamWriter& writer) override; + SayHelloRequestStreamResult SayHelloRequestStream( + CallContext& context, SayHelloRequestStreamReader& reader) override; + SayHelloStreamsResult SayHelloStreams( + CallContext& context, SayHelloStreamsReaderWriter& stream) override; }; // Default-constructs GreeterMock. @@ -123,9 +126,9 @@ UTEST_F(GreeterClientTest, SayHelloMockedServiceCustomClient) { /// [client tests] /// [client tests response stream] -void GreeterMock::SayHelloResponseStream( - SayHelloResponseStreamCall& call, - samples::api::GreetingRequest&& /*request*/) { +GreeterMock::SayHelloResponseStreamResult GreeterMock::SayHelloResponseStream( + CallContext& /*context*/, ::samples::api::GreetingRequest&& /*request*/, + SayHelloResponseStreamWriter& writer) { samples::api::GreetingResponse response; std::string message = "Mocked response"; @@ -133,9 +136,9 @@ void GreeterMock::SayHelloResponseStream( for (auto i = 0; i < kCountSend; ++i) { message.push_back('!'); response.set_greeting(grpc::string(message)); - call.Write(response); + writer.Write(response); } - call.Finish(); + return grpc::Status::OK; } UTEST_F(GreeterClientTest, SayHelloResponseStreamMockedServiceCustomClient) { @@ -150,13 +153,14 @@ UTEST_F(GreeterClientTest, SayHelloResponseStreamMockedServiceCustomClient) { /// [client tests response stream] /// [client tests request stream] -void GreeterMock::SayHelloRequestStream(SayHelloRequestStreamCall& call) { +GreeterMock::SayHelloRequestStreamResult GreeterMock::SayHelloRequestStream( + CallContext& /*context*/, SayHelloRequestStreamReader& reader) { samples::api::GreetingResponse response; samples::api::GreetingRequest request; - while (call.Read(request)) { + while (reader.Read(request)) { } response.set_greeting("Mocked response!!!"); - call.Finish(response); + return response; } UTEST_F(GreeterClientTest, SayHelloRequestStreamMockedServiceCustomClient) { @@ -169,16 +173,17 @@ UTEST_F(GreeterClientTest, SayHelloRequestStreamMockedServiceCustomClient) { /// [client tests request stream] /// [client tests streams] -void GreeterMock::SayHelloStreams(SayHelloStreamsCall& call) { +GreeterMock::SayHelloStreamsResult GreeterMock::SayHelloStreams( + CallContext& /*context*/, SayHelloStreamsReaderWriter& stream) { samples::api::GreetingResponse response; std::string message = "Mocked response"; samples::api::GreetingRequest request; - while (call.Read(request)) { + while (stream.Read(request)) { response.set_greeting(grpc::string(message)); - call.Write(response); + stream.Write(response); message.push_back('!'); } - call.Finish(); + return grpc::Status::OK; } UTEST_F(GreeterClientTest, SayHelloStreamsMockedServiceCustomClient) {