Skip to content

Commit de030d1

Browse files
committed
feat grpc: add CallOptions::SetDeadline
commit_hash:fd3d0146e823072fecc84d6d547697d0fc7d6440
1 parent 69f6e81 commit de030d1

File tree

6 files changed

+22
-16
lines changed

6 files changed

+22
-16
lines changed

grpc/include/userver/ugrpc/client/call_options.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <grpcpp/client_context.h>
1212
#include <grpcpp/support/config.h>
1313

14+
#include <userver/engine/deadline.hpp>
1415
#include <userver/utils/move_only_function.hpp>
1516

1617
USERVER_NAMESPACE_BEGIN
@@ -39,6 +40,12 @@ class CallOptions {
3940
std::chrono::milliseconds GetTimeout() const;
4041
/// @}
4142

43+
/// @{
44+
/// Set and get operation deadline.
45+
void SetDeadline(engine::Deadline deadline);
46+
engine::Deadline GetDeadline() const;
47+
/// @}
48+
4249
/// Add the (\a meta_key, \a meta_value) pair to the metadata associated with a client call.
4350
void AddMetadata(std::string_view meta_key, std::string_view meta_value);
4451

@@ -56,6 +63,7 @@ class CallOptions {
5663
int attempts_{0};
5764

5865
std::chrono::milliseconds timeout_{std::chrono::milliseconds::max()};
66+
engine::Deadline deadline_;
5967

6068
std::vector<std::pair<grpc::string, grpc::string>> metadata_;
6169

grpc/include/userver/ugrpc/time_utils.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ USERVER_NAMESPACE_BEGIN
88

99
namespace ugrpc {
1010

11-
/// Converts `engine::Deadline::Duration` to `gpr_timespec`.
11+
/// Converts `engine::Deadline::Duration` to `gpr_timespec` (with `GPR_TIMESPAN` clock_type).
1212
gpr_timespec DurationToTimespec(const engine::Deadline::Duration& duration) noexcept;
1313

1414
template <typename Rep, typename Period>
@@ -19,7 +19,7 @@ gpr_timespec DurationToTimespec(const std::chrono::duration<Rep, Period>& durati
1919
/// Converts `gpr_timespec` to `engine::Deadline::Duration`
2020
engine::Deadline::Duration TimespecToDuration(gpr_timespec t) noexcept;
2121

22-
/// Converts `engine::Deadline` to `gpr_timespec`.
22+
/// Converts `engine::Deadline` to `gpr_timespec` (with `GPR_CLOCK_MONOTONIC` clock_type).
2323
gpr_timespec DeadlineToTimespec(const engine::Deadline& deadline) noexcept;
2424

2525
/// Converts `gpr_timespec` to `engine::Deadline`

grpc/src/ugrpc/client/call_options.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ void CallOptions::SetTimeout(std::chrono::milliseconds timeout) { timeout_ = tim
1414

1515
std::chrono::milliseconds CallOptions::GetTimeout() const { return timeout_; }
1616

17+
void CallOptions::SetDeadline(engine::Deadline deadline) { deadline_ = deadline; }
18+
19+
engine::Deadline CallOptions::GetDeadline() const { return deadline_; }
20+
1721
void CallOptions::AddMetadata(std::string_view meta_key, std::string_view meta_value) {
1822
metadata_.emplace_back(ugrpc::impl::ToGrpcString(meta_key), ugrpc::impl::ToGrpcString(meta_value));
1923
}

grpc/src/ugrpc/client/impl/call_options_accessor.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ std::unique_ptr<grpc::ClientContext> CallOptionsAccessor::CreateClientContext(co
1010
auto client_context = call_options.client_context_factory_ ? call_options.client_context_factory_()
1111
: std::make_unique<grpc::ClientContext>();
1212

13-
const auto timeout = call_options.GetTimeout();
14-
if (std::chrono::milliseconds::max() != timeout) {
15-
client_context->set_deadline(ugrpc::DurationToTimespec(timeout));
13+
// default timeout_ gives unreachable deadline
14+
// engine::Deadline::FromDuration(std::chrono::milliseconds::max()) -> !IsReachable
15+
const auto deadline = std::min(call_options.deadline_, engine::Deadline::FromDuration(call_options.timeout_));
16+
if (deadline.IsReachable()) {
17+
client_context->set_deadline(deadline);
1618
}
1719

1820
for (const auto& [meta_key, meta_value] : call_options.metadata_) {

grpc/src/ugrpc/client/middlewares/deadline_propagation/middleware.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ void UpdateDeadline(MiddlewareCallContext& context) {
5151
span.AddTag("deadline_updated", true);
5252
state.SetDeadlinePropagated();
5353

54-
client_context.set_deadline(ugrpc::DurationToTimespec(task_time_left));
54+
client_context.set_deadline(task_deadline);
5555

5656
AddTimeoutMsToSpan(span, task_time_left);
5757
} else {

grpc/tests/deadline_test.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -323,11 +323,7 @@ UTEST_F(GrpcTestInheritedDedline, TestServerDataExist) {
323323
const engine::Deadline deadline = engine::Deadline::FromDuration(tests::kLongTimeout);
324324

325325
GetService().SetClientInitialTimeout(tests::kLongTimeout);
326-
call_options.SetClientContextFactory([deadline] {
327-
auto client_context = std::make_unique<grpc::ClientContext>();
328-
client_context->set_deadline(deadline);
329-
return client_context;
330-
});
326+
call_options.SetDeadline(deadline);
331327

332328
// In tests the gpr_timespec <> steady_clock conversions were giving
333329
// ~0.5ms precision loss once in 10k runs. Thus the 10ms delay.
@@ -345,11 +341,7 @@ UTEST_F(GrpcTestInheritedDedline, TestDeadlineExpiresBeforeCall) {
345341

346342
ugrpc::client::CallOptions call_options;
347343
const engine::Deadline deadline = engine::Deadline::FromDuration(tests::kShortTimeout);
348-
call_options.SetClientContextFactory([deadline] {
349-
auto client_context = std::make_unique<grpc::ClientContext>();
350-
client_context->set_deadline(deadline);
351-
return client_context;
352-
});
344+
call_options.SetDeadline(deadline);
353345

354346
// Test that the time between client context
355347
// construction and client request is measured.

0 commit comments

Comments
 (0)