Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++ propagation API #2813

Merged
merged 20 commits into from
Aug 6, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
C++ context propagation
  • Loading branch information
ctiller committed Aug 5, 2015
commit bb5361453f0d0517675bc369ef6e6a5d2f516b90
62 changes: 60 additions & 2 deletions include/grpc++/client_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,14 @@
#include <string>

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

struct grpc_call;
struct grpc_completion_queue;
struct census_context;

namespace grpc {
Expand All @@ -70,12 +69,68 @@ template <class R, class W>
class ClientAsyncReaderWriter;
template <class R>
class ClientAsyncResponseReader;
class ServerContext;

class PropagationOptions {
public:
PropagationOptions() : propagate_(GRPC_PROPAGATE_DEFAULTS) {}

PropagationOptions& enable_deadline_propagation() {
propagate_ |= GRPC_PROPAGATE_DEADLINE;
return *this;
}

PropagationOptions& disable_deadline_propagation() {
propagate_ &= ~GRPC_PROPAGATE_DEADLINE;
return *this;
}

PropagationOptions& enable_stats_propagation() {
propagate_ |= GRPC_PROPAGATE_STATS_CONTEXT;
return *this;
}

PropagationOptions& disable_stats_propagation() {
propagate_ &= ~GRPC_PROPAGATE_STATS_CONTEXT;
return *this;
}

PropagationOptions& enable_tracing_propagation() {
propagate_ |= GRPC_PROPAGATE_TRACING_CONTEXT;
return *this;
}

PropagationOptions& disable_tracing_propagation() {
propagate_ &= ~GRPC_PROPAGATE_TRACING_CONTEXT;
return *this;
}

PropagationOptions& enable_cancellation_propagation() {
propagate_ |= GRPC_PROPAGATE_CANCELLATION;
return *this;
}

PropagationOptions& disable_cancellation_propagation() {
propagate_ &= ~GRPC_PROPAGATE_CANCELLATION;
return *this;
}

gpr_uint32 c_bitmask() const { return propagate_; }

private:
gpr_uint32 propagate_;
};

class ClientContext {
public:
ClientContext();
~ClientContext();

/// Create a new ClientContext that propagates some or all of its attributes
static ClientContext FromServerContext(
const ServerContext& server_context,
PropagationOptions options = PropagationOptions());

void AddMetadata(const grpc::string& meta_key,
const grpc::string& meta_value);

Expand Down Expand Up @@ -181,6 +236,9 @@ class ClientContext {
std::multimap<grpc::string, grpc::string> recv_initial_metadata_;
std::multimap<grpc::string, grpc::string> trailing_metadata_;

grpc_call* propagate_from_call_;
PropagationOptions propagation_options_;

grpc_compression_algorithm compression_algorithm_;
};

Expand Down
2 changes: 2 additions & 0 deletions include/grpc++/server_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ struct census_context;

namespace grpc {

class ClientContext;
template <class W, class R>
class ServerAsyncReader;
template <class W>
Expand Down Expand Up @@ -158,6 +159,7 @@ class ServerContext {
friend class ServerStreamingHandler;
template <class ServiceType, class RequestType, class ResponseType>
friend class BidiStreamingHandler;
friend class ::grpc::ClientContext;

// Prevent copying.
ServerContext(const ServerContext&);
Expand Down
6 changes: 4 additions & 2 deletions src/cpp/client/channel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,12 @@ Call Channel::CreateCall(const RpcMethod& method, ClientContext* context,
const char* host_str = host_.empty() ? NULL : host_.c_str();
auto c_call = method.channel_tag() && context->authority().empty()
? grpc_channel_create_registered_call(
c_channel_, NULL, GRPC_PROPAGATE_DEFAULTS, cq->cq(),
c_channel_, context->propagate_from_call_,
context->propagation_options_.c_bitmask(), cq->cq(),
method.channel_tag(), context->raw_deadline())
: grpc_channel_create_call(
c_channel_, NULL, GRPC_PROPAGATE_DEFAULTS, cq->cq(),
c_channel_, context->propagate_from_call_,
context->propagation_options_.c_bitmask(), cq->cq(),
method.name(), context->authority().empty()
? host_str
: context->authority().c_str(),
Expand Down
12 changes: 11 additions & 1 deletion src/cpp/client/client_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <grpc/support/alloc.h>
#include <grpc/support/string_util.h>
#include <grpc++/credentials.h>
#include <grpc++/server_context.h>
#include <grpc++/time.h>

#include "src/core/channel/compress_filter.h"
Expand All @@ -48,7 +49,8 @@ ClientContext::ClientContext()
: initial_metadata_received_(false),
call_(nullptr),
cq_(nullptr),
deadline_(gpr_inf_future(GPR_CLOCK_REALTIME)) {}
deadline_(gpr_inf_future(GPR_CLOCK_REALTIME)),
propagate_from_call_(nullptr) {}

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

ClientContext ClientContext::FromServerContext(const ServerContext& context,
PropagationOptions options) {
ClientContext ctx;
ctx.propagate_from_call_ = context.call_;
ctx.propagation_options_ = options;
return ctx;
}

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