diff --git a/CMakeLists.txt b/CMakeLists.txt index f41068d..90f3c20 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -117,6 +117,7 @@ find_package(gRPC QUIET) if(gRPC_FOUND) set(GRPC_LIBRARIES gRPC::gpr gRPC::grpc gRPC::grpc++) get_target_property(GRPC_CPP_PLUGIN gRPC::grpc_cpp_plugin LOCATION) + get_target_property(GRPC_INCLUDE_DIR gRPC::grpc INTERFACE_INCLUDE_DIRECTORIES) else() include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/FindGRPC.cmake) set(GRPC_LIBRARIES ${GPR_LIBRARY} ${GRPC_LIBRARY} ${GRPC_GRPC++_LIBRARY}) @@ -137,6 +138,7 @@ add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/proto) include_directories(SYSTEM ${Boost_INCLUDE_DIR} ${CPPREST_INCLUDE_DIR} ${PROTOBUF_INCLUDE_DIRS} + ${GRPC_INCLUDE_DIR} ${OPENSSL_INCLUDE_DIR}) include_directories(${CMAKE_CURRENT_SOURCE_DIR}) if(NOT CMAKE_CXX_COMPILER_ID MATCHES "MSVC") diff --git a/etcd/Client.hpp b/etcd/Client.hpp index 2831044..2e76935 100644 --- a/etcd/Client.hpp +++ b/etcd/Client.hpp @@ -647,8 +647,8 @@ namespace etcd /** * Set a timeout value for grpc operations. */ - template - void set_grpc_timeout(std::chrono::duration const &timeout) { + template + void set_grpc_timeout(std::chrono::duration const &timeout) { this->client->set_grpc_timeout(timeout); } diff --git a/etcd/Response.hpp b/etcd/Response.hpp index 036ffdf..5a62322 100644 --- a/etcd/Response.hpp +++ b/etcd/Response.hpp @@ -21,6 +21,19 @@ namespace etcd { typedef std::vector Keys; + namespace detail { + // Compute the duration between given start time point and now + inline const std::chrono::microseconds duration_till_now( + std::chrono::high_resolution_clock::time_point const & start_timepoint) { + return std::chrono::duration_cast( + std::chrono::high_resolution_clock::now() - start_timepoint); + } + } + + // forward declaration + class KeepAlive; + class Watcher; + /** * The Reponse object received for the requests of etcd::Client */ @@ -33,10 +46,7 @@ namespace etcd { call->waitForResponse(); auto v3resp = call->ParseResponse(); - - auto duration = std::chrono::duration_cast( - std::chrono::high_resolution_clock::now() - call->startTimepoint()); - return etcd::Response(v3resp, duration); + return etcd::Response(v3resp, detail::duration_till_now(call->startTimepoint())); } template @@ -44,10 +54,7 @@ namespace etcd { call->waitForResponse(); auto v3resp = call->ParseResponse(); - - auto duration = std::chrono::duration_cast( - std::chrono::high_resolution_clock::now() - call->startTimepoint()); - return etcd::Response(v3resp, duration); + return etcd::Response(v3resp, detail::duration_till_now(call->startTimepoint())); } template @@ -56,10 +63,7 @@ namespace etcd { call->waitForResponse(callback); auto v3resp = call->ParseResponse(); - - auto duration = std::chrono::duration_cast( - std::chrono::high_resolution_clock::now() - call->startTimepoint()); - return etcd::Response(v3resp, duration); + return etcd::Response(v3resp, detail::duration_till_now(call->startTimepoint())); } template @@ -69,10 +73,7 @@ namespace etcd call->waitForResponse(); auto v3resp = call->ParseResponse(); - - auto duration = std::chrono::duration_cast( - std::chrono::high_resolution_clock::now() - call->startTimepoint()); - return etcd::Response(v3resp, duration); + return etcd::Response(v3resp, detail::duration_till_now(call->startTimepoint())); } template @@ -82,10 +83,7 @@ namespace etcd call->waitForResponse(); auto v3resp = call->ParseResponse(); - - auto duration = std::chrono::duration_cast( - std::chrono::high_resolution_clock::now() - call->startTimepoint()); - return etcd::Response(v3resp, duration); + return etcd::Response(v3resp, detail::duration_till_now(call->startTimepoint())); } Response(); @@ -233,6 +231,9 @@ namespace etcd friend class Client; friend class SyncClient; + friend class KeepAlive; + friend class Watcher; + friend class etcdv3::AsyncWatchAction; friend class etcdv3::AsyncLeaseKeepAliveAction; friend class etcdv3::AsyncObserveAction; diff --git a/etcd/SyncClient.hpp b/etcd/SyncClient.hpp index 83fac15..df0a04c 100644 --- a/etcd/SyncClient.hpp +++ b/etcd/SyncClient.hpp @@ -721,8 +721,8 @@ namespace etcd /** * Set a timeout value for grpc operations. */ - template - void set_grpc_timeout(std::chrono::duration const &timeout) { + template + void set_grpc_timeout(std::chrono::duration const &timeout) { grpc_timeout = std::chrono::duration_cast(timeout); } diff --git a/src/SyncClient.cpp b/src/SyncClient.cpp index 654b724..e6044a3 100644 --- a/src/SyncClient.cpp +++ b/src/SyncClient.cpp @@ -7,6 +7,8 @@ #include #include #include +#include + #include "etcd/Value.hpp" #if defined(_WIN32) @@ -31,6 +33,8 @@ #include #include +#include + #include "proto/rpc.grpc.pb.h" #include "proto/v3lock.grpc.pb.h" #include "proto/v3election.grpc.pb.h" @@ -163,9 +167,8 @@ static std::string read_from_file(std::string const &filename) { return std::string{}; } -static grpc::SslCredentialsOptions make_ssl_credentials(std::string const &ca, - std::string const &cert, - std::string const &key) { +static grpc::SslCredentialsOptions make_ssl_credentials( + std::string const &ca, std::string const &cert, std::string const &key) { grpc::SslCredentialsOptions options; options.pem_root_certs = read_from_file(ca); options.pem_cert_chain = read_from_file(cert); diff --git a/src/v3/AsyncLeaseAction.cpp b/src/v3/AsyncLeaseAction.cpp index 2e97d9b..8e6f34e 100644 --- a/src/v3/AsyncLeaseAction.cpp +++ b/src/v3/AsyncLeaseAction.cpp @@ -1,5 +1,6 @@ #include "etcd/v3/AsyncLeaseAction.hpp" +#include "etcd/Response.hpp" #include "etcd/v3/action_constants.hpp" #include "etcd/v3/Transaction.hpp" @@ -101,8 +102,7 @@ etcd::Response etcdv3::AsyncLeaseKeepAliveAction::Refresh() auto start_timepoint = std::chrono::high_resolution_clock::now(); if (isCancelled) { status = grpc::Status::CANCELLED; - return etcd::Response(ParseResponse(), std::chrono::duration_cast( - std::chrono::high_resolution_clock::now() - start_timepoint)); + return etcd::Response(ParseResponse(), etcd::detail::duration_till_now(start_timepoint)); } LeaseKeepAliveRequest leasekeepalive_request; @@ -130,8 +130,8 @@ etcd::Response etcdv3::AsyncLeaseKeepAliveAction::Refresh() } } if (!status.ok()) { - return etcd::Response(ParseResponse(), std::chrono::duration_cast( - std::chrono::high_resolution_clock::now() - start_timepoint)); + this->CancelKeepAlive(); + return etcd::Response(ParseResponse(), etcd::detail::duration_till_now(start_timepoint)); } stream->Read(&reply, (void*)etcdv3::KEEPALIVE_READ); @@ -152,8 +152,8 @@ etcd::Response etcdv3::AsyncLeaseKeepAliveAction::Refresh() break; } } - return etcd::Response(ParseResponse(), std::chrono::duration_cast( - std::chrono::high_resolution_clock::now() - start_timepoint)); + this->CancelKeepAlive(); + return etcd::Response(ParseResponse(), etcd::detail::duration_till_now(start_timepoint)); } else { stream->Write(leasekeepalive_request, (void *)etcdv3::KEEPALIVE_WRITE); // wait write finish @@ -161,10 +161,10 @@ etcd::Response etcdv3::AsyncLeaseKeepAliveAction::Refresh() stream->Read(&reply, (void*)etcdv3::KEEPALIVE_READ); // wait read finish if (cq_.Next(&got_tag, &ok) && ok && got_tag == (void *)etcdv3::KEEPALIVE_READ) { - return etcd::Response(ParseResponse(), std::chrono::duration_cast( - std::chrono::high_resolution_clock::now() - start_timepoint)); + return etcd::Response(ParseResponse(), etcd::detail::duration_till_now(start_timepoint)); } } + this->CancelKeepAlive(); return etcd::Response(grpc::StatusCode::ABORTED, "Failed to create a lease keep-alive connection"); } }