diff --git a/include/grpc++/client_context.h b/include/grpc++/client_context.h index ec1a4c27825d1e0bf905b4af622dedcaa1bd9b7e..0cf6bdc647e7e8c48ff2faf723e5100a8bc1a5f6 100644 --- a/include/grpc++/client_context.h +++ b/include/grpc++/client_context.h @@ -38,8 +38,9 @@ #include <string> #include <vector> -#include <grpc++/config.h> #include <grpc/support/log.h> +#include <grpc/support/time.h> +#include <grpc++/config.h> using std::chrono::system_clock; @@ -78,9 +79,11 @@ class ClientContext { grpc_completion_queue *cq() { return cq_; } void set_cq(grpc_completion_queue *cq) { cq_ = cq; } + gpr_timespec RawDeadline() { return absolute_deadline_; } + grpc_call *call_; grpc_completion_queue *cq_; - system_clock::time_point absolute_deadline_; + gpr_timespec absolute_deadline_; std::vector<std::pair<grpc::string, grpc::string> > metadata_; }; diff --git a/src/cpp/client/channel.cc b/src/cpp/client/channel.cc index 3792869d83df7fa1f7d72ad9d1674ebd90e176e8..3686596e77bd977ae98e28ab5e878c5e730ec15e 100644 --- a/src/cpp/client/channel.cc +++ b/src/cpp/client/channel.cc @@ -39,12 +39,10 @@ #include <grpc/grpc.h> #include <grpc/support/log.h> #include <grpc/support/slice.h> -#include <grpc/support/time.h> #include "src/cpp/rpc_method.h" #include "src/cpp/proto/proto_utils.h" #include "src/cpp/stream/stream_context.h" -#include "src/cpp/util/time.h" #include <grpc++/config.h> #include <google/protobuf/message.h> #include <grpc++/client_context.h> @@ -80,12 +78,10 @@ Status Channel::StartBlockingRpc(const RpcMethod& method, const google::protobuf::Message& request, google::protobuf::Message* result) { Status status; - gpr_timespec absolute_deadline; - AbsoluteDeadlineTimepoint2Timespec(context->absolute_deadline(), - &absolute_deadline); - grpc_call* call = grpc_channel_create_call(c_channel_, method.name(), - // FIXME(yangg) - "localhost", absolute_deadline); + grpc_call* call = + grpc_channel_create_call(c_channel_, method.name(), + // FIXME(yangg) + "localhost", context->RawDeadline()); context->set_call(call); grpc_event* ev; void* finished_tag = reinterpret_cast<char*>(call); @@ -156,12 +152,10 @@ StreamContextInterface* Channel::CreateStream(const RpcMethod& method, ClientContext* context, const google::protobuf::Message* request, google::protobuf::Message* result) { - gpr_timespec absolute_deadline; - AbsoluteDeadlineTimepoint2Timespec(context->absolute_deadline(), - &absolute_deadline); - grpc_call* call = grpc_channel_create_call(c_channel_, method.name(), - // FIXME(yangg) - "localhost", absolute_deadline); + grpc_call* call = + grpc_channel_create_call(c_channel_, method.name(), + // FIXME(yangg) + "localhost", context->RawDeadline()); context->set_call(call); grpc_completion_queue* cq = grpc_completion_queue_create(); context->set_cq(cq); diff --git a/src/cpp/client/client_context.cc b/src/cpp/client/client_context.cc index 58a8ad252bf8adf83767557b26ec1d49aaa25349..505b7d89b46a9925a5b01429d0bbe1f312f72f84 100644 --- a/src/cpp/client/client_context.cc +++ b/src/cpp/client/client_context.cc @@ -34,15 +34,14 @@ #include <grpc++/client_context.h> #include <grpc/grpc.h> +#include "src/cpp/util/time.h" using std::chrono::system_clock; namespace grpc { ClientContext::ClientContext() - : call_(nullptr), - cq_(nullptr), - absolute_deadline_(system_clock::time_point::max()) {} + : call_(nullptr), cq_(nullptr), absolute_deadline_(gpr_inf_future) {} ClientContext::~ClientContext() { if (call_) { @@ -64,11 +63,11 @@ ClientContext::~ClientContext() { void ClientContext::set_absolute_deadline( const system_clock::time_point& deadline) { - absolute_deadline_ = deadline; + Timepoint2Timespec(deadline, &absolute_deadline_); } system_clock::time_point ClientContext::absolute_deadline() { - return absolute_deadline_; + return Timespec2Timepoint(absolute_deadline_); } void ClientContext::AddMetadata(const grpc::string& meta_key, diff --git a/src/cpp/server/completion_queue.cc b/src/cpp/server/completion_queue.cc index 04eb301f7ec0e5813da7003a4842ee2ff49b3a77..56d165c9a6855a9903d7637c6a11679cdfe0bfa2 100644 --- a/src/cpp/server/completion_queue.cc +++ b/src/cpp/server/completion_queue.cc @@ -86,10 +86,10 @@ CompletionQueue::CompletionType CompletionQueue::Next(void** tag) { if (!ev->call) { *tag = nullptr; } else { - *tag = new AsyncServerContext(ev->call, ev->data.server_rpc_new.method, - ev->data.server_rpc_new.host, - AbsoluteDeadlineTimespec2Timepoint( - ev->data.server_rpc_new.deadline)); + *tag = new AsyncServerContext( + ev->call, ev->data.server_rpc_new.method, + ev->data.server_rpc_new.host, + Timespec2Timepoint(ev->data.server_rpc_new.deadline)); } return_type = SERVER_RPC_NEW; break; diff --git a/src/cpp/util/time.cc b/src/cpp/util/time.cc index 207bebf568751710fae6065fc0ce6ad849813d90..150ec8ed5acfa4aafd54a004a404a9578eb5147b 100644 --- a/src/cpp/util/time.cc +++ b/src/cpp/util/time.cc @@ -42,8 +42,9 @@ using std::chrono::system_clock; namespace grpc { -void AbsoluteDeadlineTimepoint2Timespec(const system_clock::time_point& from, - gpr_timespec* to) { +// TODO(yangg) prevent potential overflow. +void Timepoint2Timespec(const system_clock::time_point& from, + gpr_timespec* to) { system_clock::duration deadline = from.time_since_epoch(); seconds secs = duration_cast<seconds>(deadline); nanoseconds nsecs = duration_cast<nanoseconds>(deadline - secs); @@ -51,7 +52,7 @@ void AbsoluteDeadlineTimepoint2Timespec(const system_clock::time_point& from, to->tv_nsec = nsecs.count(); } -system_clock::time_point AbsoluteDeadlineTimespec2Timepoint(gpr_timespec t) { +system_clock::time_point Timespec2Timepoint(gpr_timespec t) { system_clock::time_point tp; tp += seconds(t.tv_sec); tp += nanoseconds(t.tv_nsec); diff --git a/src/cpp/util/time.h b/src/cpp/util/time.h index c21fba7ec31f8d2010ef604798639bd094cbe9b5..338c4f5119b08a968dbae2f9b395803731b07d77 100644 --- a/src/cpp/util/time.h +++ b/src/cpp/util/time.h @@ -41,11 +41,10 @@ namespace grpc { // from and to should be absolute time. -void AbsoluteDeadlineTimepoint2Timespec( - const std::chrono::system_clock::time_point& from, gpr_timespec* to); +void Timepoint2Timespec(const std::chrono::system_clock::time_point& from, + gpr_timespec* to); -std::chrono::system_clock::time_point AbsoluteDeadlineTimespec2Timepoint( - gpr_timespec t); +std::chrono::system_clock::time_point Timespec2Timepoint(gpr_timespec t); } // namespace grpc diff --git a/test/cpp/util/time_test.cc b/test/cpp/util/time_test.cc index 4c633f34f5a63f8b720dc4c437a1690be7e34aef..97499fed28ac3273bce45ae662ffb709722b1df5 100644 --- a/test/cpp/util/time_test.cc +++ b/test/cpp/util/time_test.cc @@ -52,14 +52,12 @@ TEST_F(TimeTest, AbsolutePointTest) { long us = 10000000L; gpr_timespec ts = gpr_time_from_micros(us); system_clock::time_point tp{microseconds(us)}; - system_clock::time_point tp_converted = - AbsoluteDeadlineTimespec2Timepoint(ts); + system_clock::time_point tp_converted = Timespec2Timepoint(ts); gpr_timespec ts_converted; - AbsoluteDeadlineTimepoint2Timespec(tp_converted, &ts_converted); + Timepoint2Timespec(tp_converted, &ts_converted); EXPECT_TRUE(ts.tv_sec == ts_converted.tv_sec); EXPECT_TRUE(ts.tv_nsec == ts_converted.tv_nsec); - system_clock::time_point tp_converted_2 = - AbsoluteDeadlineTimespec2Timepoint(ts_converted); + system_clock::time_point tp_converted_2 = Timespec2Timepoint(ts_converted); EXPECT_TRUE(tp == tp_converted); EXPECT_TRUE(tp == tp_converted_2); }