Skip to content

Commit

Permalink
feat grpc: switch to simplified service handlers interface
Browse files Browse the repository at this point in the history
commit_hash:a98a062527b439f7742bd5bde08d942b47b39f86
  • Loading branch information
kpavlov00 committed Oct 12, 2024
1 parent 6f57b87 commit 3a78ec6
Show file tree
Hide file tree
Showing 16 changed files with 202 additions and 171 deletions.
30 changes: 16 additions & 14 deletions grpc/benchmarks/base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};

Expand Down
72 changes: 41 additions & 31 deletions grpc/functional_tests/basic_chaos/service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,31 @@ class GreeterServiceComponent final
: api::GreeterServiceBase::Component(config, context),
prefix_(config["greeting-prefix"].As<std::string>()) {}

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();

private:
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") {
Expand All @@ -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;
Expand All @@ -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);
}
});
Expand All @@ -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() {
Expand Down
22 changes: 11 additions & 11 deletions grpc/functional_tests/basic_server/grpc_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ class GreeterServiceComponent final
http_client_(
context.FindComponent<components::HttpClient>().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();

Expand All @@ -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()
Expand All @@ -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() {
Expand Down
14 changes: 7 additions & 7 deletions grpc/functional_tests/metrics/src/greeter_service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,24 @@ class GreeterServiceComponent final
: samples::api::GreeterServiceBase::Component(config, context),
client_(context.FindComponent<GreeterClient>()) {}

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<ugrpc::server::ServiceComponentBase>(R"(
type: object
description: gRPC sample greater service component
Expand Down
36 changes: 20 additions & 16 deletions grpc/functional_tests/middleware_server/src/service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand Down
15 changes: 9 additions & 6 deletions grpc/functional_tests/middleware_server/src/service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};
Expand Down
Loading

0 comments on commit 3a78ec6

Please sign in to comment.