Fixes the timeout parameter and some improvements to find non-standard installed deps.

Signed-off-by: Tao He <sighingnow@gmail.com>
This commit is contained in:
Tao He 2022-10-17 11:52:53 +08:00
parent 62884c7e38
commit 5d1769a344
6 changed files with 41 additions and 35 deletions

View File

@ -117,6 +117,7 @@ find_package(gRPC QUIET)
if(gRPC_FOUND) if(gRPC_FOUND)
set(GRPC_LIBRARIES gRPC::gpr gRPC::grpc gRPC::grpc++) set(GRPC_LIBRARIES gRPC::gpr gRPC::grpc gRPC::grpc++)
get_target_property(GRPC_CPP_PLUGIN gRPC::grpc_cpp_plugin LOCATION) get_target_property(GRPC_CPP_PLUGIN gRPC::grpc_cpp_plugin LOCATION)
get_target_property(GRPC_INCLUDE_DIR gRPC::grpc INTERFACE_INCLUDE_DIRECTORIES)
else() else()
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/FindGRPC.cmake) include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/FindGRPC.cmake)
set(GRPC_LIBRARIES ${GPR_LIBRARY} ${GRPC_LIBRARY} ${GRPC_GRPC++_LIBRARY}) 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} include_directories(SYSTEM ${Boost_INCLUDE_DIR}
${CPPREST_INCLUDE_DIR} ${CPPREST_INCLUDE_DIR}
${PROTOBUF_INCLUDE_DIRS} ${PROTOBUF_INCLUDE_DIRS}
${GRPC_INCLUDE_DIR}
${OPENSSL_INCLUDE_DIR}) ${OPENSSL_INCLUDE_DIR})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}) include_directories(${CMAKE_CURRENT_SOURCE_DIR})
if(NOT CMAKE_CXX_COMPILER_ID MATCHES "MSVC") if(NOT CMAKE_CXX_COMPILER_ID MATCHES "MSVC")

View File

@ -647,8 +647,8 @@ namespace etcd
/** /**
* Set a timeout value for grpc operations. * Set a timeout value for grpc operations.
*/ */
template <typename Rep = std::micro> template <typename Period = std::micro>
void set_grpc_timeout(std::chrono::duration<Rep> const &timeout) { void set_grpc_timeout(std::chrono::duration<std::chrono::microseconds::rep, Period> const &timeout) {
this->client->set_grpc_timeout(timeout); this->client->set_grpc_timeout(timeout);
} }

View File

@ -21,6 +21,19 @@ namespace etcd
{ {
typedef std::vector<std::string> Keys; typedef std::vector<std::string> 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::microseconds>(
std::chrono::high_resolution_clock::now() - start_timepoint);
}
}
// forward declaration
class KeepAlive;
class Watcher;
/** /**
* The Reponse object received for the requests of etcd::Client * The Reponse object received for the requests of etcd::Client
*/ */
@ -33,10 +46,7 @@ namespace etcd
{ {
call->waitForResponse(); call->waitForResponse();
auto v3resp = call->ParseResponse(); auto v3resp = call->ParseResponse();
return etcd::Response(v3resp, detail::duration_till_now(call->startTimepoint()));
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::high_resolution_clock::now() - call->startTimepoint());
return etcd::Response(v3resp, duration);
} }
template <typename T> template <typename T>
@ -44,10 +54,7 @@ namespace etcd
{ {
call->waitForResponse(); call->waitForResponse();
auto v3resp = call->ParseResponse(); auto v3resp = call->ParseResponse();
return etcd::Response(v3resp, detail::duration_till_now(call->startTimepoint()));
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::high_resolution_clock::now() - call->startTimepoint());
return etcd::Response(v3resp, duration);
} }
template <typename T> template <typename T>
@ -56,10 +63,7 @@ namespace etcd
{ {
call->waitForResponse(callback); call->waitForResponse(callback);
auto v3resp = call->ParseResponse(); auto v3resp = call->ParseResponse();
return etcd::Response(v3resp, detail::duration_till_now(call->startTimepoint()));
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::high_resolution_clock::now() - call->startTimepoint());
return etcd::Response(v3resp, duration);
} }
template <typename T> template <typename T>
@ -69,10 +73,7 @@ namespace etcd
call->waitForResponse(); call->waitForResponse();
auto v3resp = call->ParseResponse(); auto v3resp = call->ParseResponse();
return etcd::Response(v3resp, detail::duration_till_now(call->startTimepoint()));
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::high_resolution_clock::now() - call->startTimepoint());
return etcd::Response(v3resp, duration);
} }
template <typename T> template <typename T>
@ -82,10 +83,7 @@ namespace etcd
call->waitForResponse(); call->waitForResponse();
auto v3resp = call->ParseResponse(); auto v3resp = call->ParseResponse();
return etcd::Response(v3resp, detail::duration_till_now(call->startTimepoint()));
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::high_resolution_clock::now() - call->startTimepoint());
return etcd::Response(v3resp, duration);
} }
Response(); Response();
@ -233,6 +231,9 @@ namespace etcd
friend class Client; friend class Client;
friend class SyncClient; friend class SyncClient;
friend class KeepAlive;
friend class Watcher;
friend class etcdv3::AsyncWatchAction; friend class etcdv3::AsyncWatchAction;
friend class etcdv3::AsyncLeaseKeepAliveAction; friend class etcdv3::AsyncLeaseKeepAliveAction;
friend class etcdv3::AsyncObserveAction; friend class etcdv3::AsyncObserveAction;

View File

@ -721,8 +721,8 @@ namespace etcd
/** /**
* Set a timeout value for grpc operations. * Set a timeout value for grpc operations.
*/ */
template <typename Rep = std::micro> template <typename Period = std::micro>
void set_grpc_timeout(std::chrono::duration<Rep> const &timeout) { void set_grpc_timeout(std::chrono::duration<std::chrono::microseconds::rep, Period> const &timeout) {
grpc_timeout = std::chrono::duration_cast<std::chrono::milliseconds>(timeout); grpc_timeout = std::chrono::duration_cast<std::chrono::milliseconds>(timeout);
} }

View File

@ -7,6 +7,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <sys/types.h> #include <sys/types.h>
#include <cstddef> #include <cstddef>
#include <ratio>
#include "etcd/Value.hpp" #include "etcd/Value.hpp"
#if defined(_WIN32) #if defined(_WIN32)
@ -31,6 +33,8 @@
#include <grpc++/grpc++.h> #include <grpc++/grpc++.h>
#include <grpc++/security/credentials.h> #include <grpc++/security/credentials.h>
#include <grpc++/support/status_code_enum.h>
#include "proto/rpc.grpc.pb.h" #include "proto/rpc.grpc.pb.h"
#include "proto/v3lock.grpc.pb.h" #include "proto/v3lock.grpc.pb.h"
#include "proto/v3election.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{}; return std::string{};
} }
static grpc::SslCredentialsOptions make_ssl_credentials(std::string const &ca, static grpc::SslCredentialsOptions make_ssl_credentials(
std::string const &cert, std::string const &ca, std::string const &cert, std::string const &key) {
std::string const &key) {
grpc::SslCredentialsOptions options; grpc::SslCredentialsOptions options;
options.pem_root_certs = read_from_file(ca); options.pem_root_certs = read_from_file(ca);
options.pem_cert_chain = read_from_file(cert); options.pem_cert_chain = read_from_file(cert);

View File

@ -1,5 +1,6 @@
#include "etcd/v3/AsyncLeaseAction.hpp" #include "etcd/v3/AsyncLeaseAction.hpp"
#include "etcd/Response.hpp"
#include "etcd/v3/action_constants.hpp" #include "etcd/v3/action_constants.hpp"
#include "etcd/v3/Transaction.hpp" #include "etcd/v3/Transaction.hpp"
@ -101,8 +102,7 @@ etcd::Response etcdv3::AsyncLeaseKeepAliveAction::Refresh()
auto start_timepoint = std::chrono::high_resolution_clock::now(); auto start_timepoint = std::chrono::high_resolution_clock::now();
if (isCancelled) { if (isCancelled) {
status = grpc::Status::CANCELLED; status = grpc::Status::CANCELLED;
return etcd::Response(ParseResponse(), std::chrono::duration_cast<std::chrono::microseconds>( return etcd::Response(ParseResponse(), etcd::detail::duration_till_now(start_timepoint));
std::chrono::high_resolution_clock::now() - start_timepoint));
} }
LeaseKeepAliveRequest leasekeepalive_request; LeaseKeepAliveRequest leasekeepalive_request;
@ -130,8 +130,8 @@ etcd::Response etcdv3::AsyncLeaseKeepAliveAction::Refresh()
} }
} }
if (!status.ok()) { if (!status.ok()) {
return etcd::Response(ParseResponse(), std::chrono::duration_cast<std::chrono::microseconds>( this->CancelKeepAlive();
std::chrono::high_resolution_clock::now() - start_timepoint)); return etcd::Response(ParseResponse(), etcd::detail::duration_till_now(start_timepoint));
} }
stream->Read(&reply, (void*)etcdv3::KEEPALIVE_READ); stream->Read(&reply, (void*)etcdv3::KEEPALIVE_READ);
@ -152,8 +152,8 @@ etcd::Response etcdv3::AsyncLeaseKeepAliveAction::Refresh()
break; break;
} }
} }
return etcd::Response(ParseResponse(), std::chrono::duration_cast<std::chrono::microseconds>( this->CancelKeepAlive();
std::chrono::high_resolution_clock::now() - start_timepoint)); return etcd::Response(ParseResponse(), etcd::detail::duration_till_now(start_timepoint));
} else { } else {
stream->Write(leasekeepalive_request, (void *)etcdv3::KEEPALIVE_WRITE); stream->Write(leasekeepalive_request, (void *)etcdv3::KEEPALIVE_WRITE);
// wait write finish // wait write finish
@ -161,10 +161,10 @@ etcd::Response etcdv3::AsyncLeaseKeepAliveAction::Refresh()
stream->Read(&reply, (void*)etcdv3::KEEPALIVE_READ); stream->Read(&reply, (void*)etcdv3::KEEPALIVE_READ);
// wait read finish // wait read finish
if (cq_.Next(&got_tag, &ok) && ok && got_tag == (void *)etcdv3::KEEPALIVE_READ) { if (cq_.Next(&got_tag, &ok) && ok && got_tag == (void *)etcdv3::KEEPALIVE_READ) {
return etcd::Response(ParseResponse(), std::chrono::duration_cast<std::chrono::microseconds>( return etcd::Response(ParseResponse(), etcd::detail::duration_till_now(start_timepoint));
std::chrono::high_resolution_clock::now() - start_timepoint));
} }
} }
this->CancelKeepAlive();
return etcd::Response(grpc::StatusCode::ABORTED, "Failed to create a lease keep-alive connection"); return etcd::Response(grpc::StatusCode::ABORTED, "Failed to create a lease keep-alive connection");
} }
} }