Skip to content

Commit

Permalink
Merge pull request grpc#1227 from nicolasnoble/the-purge-2
Browse files Browse the repository at this point in the history
Refactoring std::chrono out.
  • Loading branch information
ctiller committed Apr 25, 2015
2 parents 1eef5aa + fd2bf67 commit 7305dc6
Show file tree
Hide file tree
Showing 24 changed files with 149 additions and 97 deletions.
4 changes: 2 additions & 2 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,6 @@ cc_library(
"src/cpp/client/channel.h",
"src/cpp/proto/proto_utils.h",
"src/cpp/server/thread_pool.h",
"src/cpp/util/time.h",
"src/cpp/client/secure_credentials.cc",
"src/cpp/server/secure_server_credentials.cc",
"src/cpp/client/channel.cc",
Expand Down Expand Up @@ -641,6 +640,7 @@ cc_library(
"include/grpc++/status_code_enum.h",
"include/grpc++/stream.h",
"include/grpc++/thread_pool_interface.h",
"include/grpc++/time.h",
],
includes = [
"include",
Expand All @@ -660,7 +660,6 @@ cc_library(
"src/cpp/client/channel.h",
"src/cpp/proto/proto_utils.h",
"src/cpp/server/thread_pool.h",
"src/cpp/util/time.h",
"src/cpp/client/channel.cc",
"src/cpp/client/channel_arguments.cc",
"src/cpp/client/client_context.cc",
Expand Down Expand Up @@ -720,6 +719,7 @@ cc_library(
"include/grpc++/status_code_enum.h",
"include/grpc++/stream.h",
"include/grpc++/thread_pool_interface.h",
"include/grpc++/time.h",
],
includes = [
"include",
Expand Down
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3598,6 +3598,7 @@ PUBLIC_HEADERS_CXX += \
include/grpc++/status_code_enum.h \
include/grpc++/stream.h \
include/grpc++/thread_pool_interface.h \
include/grpc++/time.h \

LIBGRPC++_OBJS = $(addprefix $(OBJDIR)/$(CONFIG)/, $(addsuffix .o, $(basename $(LIBGRPC++_SRC))))

Expand Down Expand Up @@ -3829,6 +3830,7 @@ PUBLIC_HEADERS_CXX += \
include/grpc++/status_code_enum.h \
include/grpc++/stream.h \
include/grpc++/thread_pool_interface.h \
include/grpc++/time.h \

LIBGRPC++_UNSECURE_OBJS = $(addprefix $(OBJDIR)/$(CONFIG)/, $(addsuffix .o, $(basename $(LIBGRPC++_UNSECURE_SRC))))

Expand Down
6 changes: 3 additions & 3 deletions build.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@
"include/grpc++/status.h",
"include/grpc++/status_code_enum.h",
"include/grpc++/stream.h",
"include/grpc++/thread_pool_interface.h"
"include/grpc++/thread_pool_interface.h",
"include/grpc++/time.h"
],
"headers": [
"src/cpp/client/channel.h",
"src/cpp/proto/proto_utils.h",
"src/cpp/server/thread_pool.h",
"src/cpp/util/time.h"
"src/cpp/server/thread_pool.h"
],
"src": [
"src/cpp/client/channel.cc",
Expand Down
1 change: 0 additions & 1 deletion examples/pubsub/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
*
*/

#include <chrono>
#include <fstream>
#include <memory>
#include <sstream>
Expand Down
23 changes: 15 additions & 8 deletions include/grpc++/client_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,13 @@
#ifndef GRPCXX_CLIENT_CONTEXT_H
#define GRPCXX_CLIENT_CONTEXT_H

#include <chrono>
#include <map>
#include <string>

#include <grpc/support/log.h>
#include <grpc/support/time.h>
#include <grpc++/config.h>

using std::chrono::system_clock;
#include <grpc++/time.h>

struct grpc_call;
struct grpc_completion_queue;
Expand Down Expand Up @@ -87,8 +85,19 @@ class ClientContext {
return trailing_metadata_;
}

void set_absolute_deadline(const system_clock::time_point& deadline);
system_clock::time_point absolute_deadline();
template <typename T>
void set_deadline(const T& deadline) {
TimePoint<T> deadline_tp(deadline);
deadline_ = deadline_tp.raw_time();
}

#ifndef GRPC_CXX0X_NO_CHRONO
std::chrono::system_clock::time_point deadline() {
return Timespec2Timepoint(deadline_);
}
#endif // !GRPC_CXX0X_NO_CHRONO

gpr_timespec raw_deadline() { return deadline_; }

void set_authority(const grpc::string& authority) { authority_ = authority; }

Expand Down Expand Up @@ -125,14 +134,12 @@ class ClientContext {
grpc_completion_queue* cq() { return cq_; }
void set_cq(grpc_completion_queue* cq) { cq_ = cq; }

gpr_timespec RawDeadline() { return absolute_deadline_; }

grpc::string authority() { return authority_; }

bool initial_metadata_received_;
grpc_call* call_;
grpc_completion_queue* cq_;
gpr_timespec absolute_deadline_;
gpr_timespec deadline_;
grpc::string authority_;
std::multimap<grpc::string, grpc::string> send_initial_metadata_;
std::multimap<grpc::string, grpc::string> recv_initial_metadata_;
Expand Down
13 changes: 8 additions & 5 deletions include/grpc++/completion_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
#ifndef GRPCXX_COMPLETION_QUEUE_H
#define GRPCXX_COMPLETION_QUEUE_H

#include <chrono>
#include <grpc/support/time.h>
#include <grpc++/impl/client_unary_call.h>
#include <grpc++/impl/grpc_library.h>
#include <grpc/support/time.h>
#include <grpc++/time.h>

struct grpc_completion_queue;

Expand Down Expand Up @@ -83,10 +83,13 @@ class CompletionQueue : public GrpcLibrary {

// Nonblocking (until deadline) read from queue.
// Cannot rely on result of tag or ok if return is TIMEOUT
NextStatus AsyncNext(void** tag, bool* ok,
std::chrono::system_clock::time_point deadline);
template<typename T>
NextStatus AsyncNext(void** tag, bool* ok, const T& deadline) {
TimePoint<T> deadline_tp(deadline);
return AsyncNextInternal(tag, ok, deadline_tp.raw_time());
}

// Blocking (until deadline) read from queue.
// Blocking read from queue.
// Returns false if the queue is ready for destruction, true if event

bool Next(void** tag, bool* ok) {
Expand Down
17 changes: 8 additions & 9 deletions include/grpc++/credentials.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
#ifndef GRPCXX_CREDENTIALS_H
#define GRPCXX_CREDENTIALS_H

#include <chrono>
#include <memory>

#include <grpc++/config.h>
Expand Down Expand Up @@ -99,20 +98,20 @@ std::unique_ptr<Credentials> ComputeEngineCredentials();
// Builds service account credentials.
// json_key is the JSON key string containing the client's private key.
// scope is a space-delimited list of the requested permissions.
// token_lifetime is the lifetime of each token acquired through this service
// account credentials. It should be positive and should not exceed
// grpc_max_auth_token_lifetime or will be cropped to this value.
// token_lifetime_seconds is the lifetime in seconds of each token acquired
// through this service account credentials. It should be positive and should
// not exceed grpc_max_auth_token_lifetime or will be cropped to this value.
std::unique_ptr<Credentials> ServiceAccountCredentials(
const grpc::string& json_key, const grpc::string& scope,
std::chrono::seconds token_lifetime);
long token_lifetime_seconds);

// Builds JWT credentials.
// json_key is the JSON key string containing the client's private key.
// token_lifetime is the lifetime of each Json Web Token (JWT) created with
// this credentials. It should not exceed grpc_max_auth_token_lifetime or
// will be cropped to this value.
// token_lifetime_seconds is the lifetime in seconds of each Json Web Token
// (JWT) created with this credentials. It should not exceed
// grpc_max_auth_token_lifetime or will be cropped to this value.
std::unique_ptr<Credentials> JWTCredentials(
const grpc::string& json_key, std::chrono::seconds token_lifetime);
const grpc::string& json_key, long token_lifetime_seconds);

// Builds refresh token credentials.
// json_refresh_token is the JSON string containing the refresh token along
Expand Down
13 changes: 9 additions & 4 deletions include/grpc++/server_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@
#ifndef GRPCXX_SERVER_CONTEXT_H
#define GRPCXX_SERVER_CONTEXT_H

#include <chrono>
#include <map>

#include <grpc/support/time.h>
#include <grpc++/config.h>
#include <grpc++/time.h>

struct gpr_timespec;
struct grpc_metadata;
Expand Down Expand Up @@ -71,9 +72,13 @@ class ServerContext {
ServerContext(); // for async calls
~ServerContext();

std::chrono::system_clock::time_point absolute_deadline() {
return deadline_;
#ifndef GRPC_CXX0X_NO_CHRONO
std::chrono::system_clock::time_point deadline() {
return Timespec2Timepoint(deadline_);
}
#endif // !GRPC_CXX0X_NO_CHRONO

gpr_timespec raw_deadline() { return deadline_; }

void AddInitialMetadata(const grpc::string& key, const grpc::string& value);
void AddTrailingMetadata(const grpc::string& key, const grpc::string& value);
Expand Down Expand Up @@ -110,7 +115,7 @@ class ServerContext {

CompletionOp* completion_op_;

std::chrono::system_clock::time_point deadline_;
gpr_timespec deadline_;
grpc_call* call_;
CompletionQueue* cq_;
bool sent_initial_metadata_;
Expand Down
61 changes: 58 additions & 3 deletions src/cpp/util/time.h → include/grpc++/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,50 @@
*
*/

#ifndef GRPC_INTERNAL_CPP_UTIL_TIME_H
#define GRPC_INTERNAL_CPP_UTIL_TIME_H
#ifndef GRPCXX_TIME_H
#define GRPCXX_TIME_H

#include <grpc++/config.h>

namespace grpc {

/* If you are trying to use CompletionQueue::AsyncNext with a time class that
isn't either gpr_timespec or std::chrono::system_clock::time_point, you
will most likely be looking at this comment as your compiler will have
fired an error below. In order to fix this issue, you have two potential
solutions:
1. Use gpr_timespec or std::chrono::system_clock::time_point instead
2. Specialize the TimePoint class with whichever time class that you
want to use here. See below for two examples of how to do this.
*/

template <typename T>
class TimePoint {
public:
TimePoint(const T& time) {
you_need_a_specialization_of_TimePoint();
}
gpr_timespec raw_time() {
gpr_timespec t;
return t;
}
private:
void you_need_a_specialization_of_TimePoint();
};

template<>
class TimePoint<gpr_timespec> {
public:
TimePoint(const gpr_timespec& time) : time_(time) { }
gpr_timespec raw_time() { return time_; }
private:
gpr_timespec time_;
};

} // namespace grpc

#ifndef GRPC_CXX0X_NO_CHRONO

#include <chrono>

Expand All @@ -46,6 +88,19 @@ void Timepoint2Timespec(const std::chrono::system_clock::time_point& from,

std::chrono::system_clock::time_point Timespec2Timepoint(gpr_timespec t);

template <>
class TimePoint<std::chrono::system_clock::time_point> {
public:
TimePoint(const std::chrono::system_clock::time_point& time) {
Timepoint2Timespec(time, &time_);
}
gpr_timespec raw_time() const { return time_; }
private:
gpr_timespec time_;
};

} // namespace grpc

#endif // GRPC_INTERNAL_CPP_UTIL_TIME_H
#endif // !GRPC_CXX0X_NO_CHRONO

#endif // GRPCXX_TIME_H
5 changes: 2 additions & 3 deletions src/cpp/client/channel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@

#include "src/cpp/client/channel.h"

#include <chrono>
#include <memory>

#include <grpc/grpc.h>
Expand Down Expand Up @@ -65,12 +64,12 @@ Call Channel::CreateCall(const RpcMethod& method, ClientContext* context,
method.channel_tag()
? grpc_channel_create_registered_call(c_channel_, cq->cq(),
method.channel_tag(),
context->RawDeadline())
context->raw_deadline())
: grpc_channel_create_call(c_channel_, cq->cq(), method.name(),
context->authority().empty()
? target_.c_str()
: context->authority().c_str(),
context->RawDeadline());
context->raw_deadline());
GRPC_TIMER_MARK(CALL_CREATED, c_call);
context->set_call(c_call);
return Call(c_call, this, cq);
Expand Down
15 changes: 2 additions & 13 deletions src/cpp/client/client_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,15 @@
#include <grpc++/client_context.h>

#include <grpc/grpc.h>
#include "src/cpp/util/time.h"

using std::chrono::system_clock;
#include <grpc++/time.h>

namespace grpc {

ClientContext::ClientContext()
: initial_metadata_received_(false),
call_(nullptr),
cq_(nullptr),
absolute_deadline_(gpr_inf_future) {}
deadline_(gpr_inf_future) {}

ClientContext::~ClientContext() {
if (call_) {
Expand All @@ -64,15 +62,6 @@ ClientContext::~ClientContext() {
}
}

void ClientContext::set_absolute_deadline(
const system_clock::time_point& deadline) {
Timepoint2Timespec(deadline, &absolute_deadline_);
}

system_clock::time_point ClientContext::absolute_deadline() {
return Timespec2Timepoint(absolute_deadline_);
}

void ClientContext::AddMetadata(const grpc::string& meta_key,
const grpc::string& meta_value) {
send_initial_metadata_.insert(std::make_pair(meta_key, meta_value));
Expand Down
12 changes: 6 additions & 6 deletions src/cpp/client/secure_credentials.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,27 +81,27 @@ std::unique_ptr<Credentials> ComputeEngineCredentials() {
// Builds service account credentials.
std::unique_ptr<Credentials> ServiceAccountCredentials(
const grpc::string& json_key, const grpc::string& scope,
std::chrono::seconds token_lifetime) {
if (token_lifetime.count() <= 0) {
long token_lifetime_seconds) {
if (token_lifetime_seconds <= 0) {
gpr_log(GPR_ERROR,
"Trying to create ServiceAccountCredentials "
"with non-positive lifetime");
return WrapCredentials(nullptr);
}
gpr_timespec lifetime = gpr_time_from_seconds(token_lifetime.count());
gpr_timespec lifetime = gpr_time_from_seconds(token_lifetime_seconds);
return WrapCredentials(grpc_service_account_credentials_create(
json_key.c_str(), scope.c_str(), lifetime));
}

// Builds JWT credentials.
std::unique_ptr<Credentials> JWTCredentials(
const grpc::string& json_key, std::chrono::seconds token_lifetime) {
if (token_lifetime.count() <= 0) {
const grpc::string& json_key, long token_lifetime_seconds) {
if (token_lifetime_seconds <= 0) {
gpr_log(GPR_ERROR,
"Trying to create JWTCredentials with non-positive lifetime");
return WrapCredentials(nullptr);
}
gpr_timespec lifetime = gpr_time_from_seconds(token_lifetime.count());
gpr_timespec lifetime = gpr_time_from_seconds(token_lifetime_seconds);
return WrapCredentials(
grpc_jwt_credentials_create(json_key.c_str(), lifetime));
}
Expand Down
Loading

0 comments on commit 7305dc6

Please sign in to comment.