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

grpc: allow request header customization before dispatch #54

Merged
merged 1 commit into from
Sep 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 7 additions & 0 deletions include/envoy/grpc/rpc_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "envoy/common/optional.h"
#include "envoy/common/pure.h"
#include "envoy/http/header_map.h"

#include "google/protobuf/service.h"

Expand All @@ -16,6 +17,12 @@ class RpcChannelCallbacks {
public:
virtual ~RpcChannelCallbacks() {}

/**
* Called before the channel dispatches an HTTP/2 request. This can be used to customize the
* transport headers for the RPC.
*/
virtual void onPreRequestCustomizeHeaders(Http::HeaderMap& headers) PURE;

/**
* Called when the request has succeeded and the response object is populated.
*/
Expand Down
1 change: 1 addition & 0 deletions source/common/grpc/rpc_channel_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ void RpcChannelImpl::CallMethod(const proto::MethodDescriptor* method, proto::Rp
message->headers().addViaCopy(Http::Headers::get().ContentType, Common::GRPC_CONTENT_TYPE);
message->body(serializeBody(*grpc_request));

callbacks_.onPreRequestCustomizeHeaders(message->headers());
http_request_ = cm_.httpAsyncClientForCluster(cluster_).send(std::move(message), *this, timeout_);
}

Expand Down
1 change: 1 addition & 0 deletions source/common/ratelimit/ratelimit_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class GrpcClientImpl : public Client, public Grpc::RpcChannelCallbacks {

private:
// Grpc::RpcChannelCallbacks
void onPreRequestCustomizeHeaders(Http::HeaderMap&) override {}
void onSuccess() override;
void onFailure(const Optional<uint64_t>& grpc_status, const std::string& message) override;

Expand Down
18 changes: 17 additions & 1 deletion test/common/grpc/rpc_channel_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,16 @@ TEST_F(GrpcRequestImplTest, NoError) {
helloworld::HelloRequest request;
request.set_name("a name");
helloworld::HelloReply response;
EXPECT_CALL(grpc_callbacks_, onPreRequestCustomizeHeaders(_))
.WillOnce(Invoke([](Http::HeaderMap& headers) -> void { headers.addViaCopy("foo", "bar"); }));
service_.SayHello(nullptr, &request, &response, nullptr);

Http::HeaderMapImpl expected_request_headers{{":scheme", "http"},
{":method", "POST"},
{":path", "/helloworld.Greeter/SayHello"},
{":authority", "cluster"},
{"content-type", "application/grpc"}};
{"content-type", "application/grpc"},
{"foo", "bar"}};

EXPECT_THAT(http_request_->headers(), HeaderMapEqualRef(expected_request_headers));

Expand All @@ -77,6 +80,7 @@ TEST_F(GrpcRequestImplTest, Non200Response) {
helloworld::HelloRequest request;
request.set_name("a name");
helloworld::HelloReply response;
EXPECT_CALL(grpc_callbacks_, onPreRequestCustomizeHeaders(_));
service_.SayHello(nullptr, &request, &response, nullptr);

Http::MessagePtr response_http_message(new Http::ResponseMessageImpl(
Expand All @@ -96,6 +100,7 @@ TEST_F(GrpcRequestImplTest, NoResponseTrailers) {
helloworld::HelloRequest request;
request.set_name("a name");
helloworld::HelloReply response;
EXPECT_CALL(grpc_callbacks_, onPreRequestCustomizeHeaders(_));
service_.SayHello(nullptr, &request, &response, nullptr);

Http::MessagePtr response_http_message(new Http::ResponseMessageImpl(
Expand All @@ -111,6 +116,7 @@ TEST_F(GrpcRequestImplTest, BadGrpcStatusInHeaderOnlyResponse) {
helloworld::HelloRequest request;
request.set_name("a name");
helloworld::HelloReply response;
EXPECT_CALL(grpc_callbacks_, onPreRequestCustomizeHeaders(_));
service_.SayHello(nullptr, &request, &response, nullptr);

Http::MessagePtr response_http_message(new Http::ResponseMessageImpl(
Expand All @@ -126,6 +132,7 @@ TEST_F(GrpcRequestImplTest, HeaderOnlyFailure) {
helloworld::HelloRequest request;
request.set_name("a name");
helloworld::HelloReply response;
EXPECT_CALL(grpc_callbacks_, onPreRequestCustomizeHeaders(_));
service_.SayHello(nullptr, &request, &response, nullptr);

Http::MessagePtr response_http_message(
Expand All @@ -142,6 +149,7 @@ TEST_F(GrpcRequestImplTest, BadGrpcStatusInResponse) {
helloworld::HelloRequest request;
request.set_name("a name");
helloworld::HelloReply response;
EXPECT_CALL(grpc_callbacks_, onPreRequestCustomizeHeaders(_));
service_.SayHello(nullptr, &request, &response, nullptr);

Http::MessagePtr response_http_message(new Http::ResponseMessageImpl(
Expand All @@ -158,6 +166,7 @@ TEST_F(GrpcRequestImplTest, GrpcStatusNonZeroInResponse) {
helloworld::HelloRequest request;
request.set_name("a name");
helloworld::HelloReply response;
EXPECT_CALL(grpc_callbacks_, onPreRequestCustomizeHeaders(_));
service_.SayHello(nullptr, &request, &response, nullptr);

Http::MessagePtr response_http_message(new Http::ResponseMessageImpl(
Expand All @@ -175,6 +184,7 @@ TEST_F(GrpcRequestImplTest, ShortBodyInResponse) {
helloworld::HelloRequest request;
request.set_name("a name");
helloworld::HelloReply response;
EXPECT_CALL(grpc_callbacks_, onPreRequestCustomizeHeaders(_));
service_.SayHello(nullptr, &request, &response, nullptr);

Http::MessagePtr response_http_message(new Http::ResponseMessageImpl(
Expand All @@ -193,6 +203,7 @@ TEST_F(GrpcRequestImplTest, BadMessageInResponse) {
helloworld::HelloRequest request;
request.set_name("a name");
helloworld::HelloReply response;
EXPECT_CALL(grpc_callbacks_, onPreRequestCustomizeHeaders(_));
service_.SayHello(nullptr, &request, &response, nullptr);

Http::MessagePtr response_http_message(new Http::ResponseMessageImpl(
Expand All @@ -211,6 +222,7 @@ TEST_F(GrpcRequestImplTest, HttpAsyncRequestFailure) {
helloworld::HelloRequest request;
request.set_name("a name");
helloworld::HelloReply response;
EXPECT_CALL(grpc_callbacks_, onPreRequestCustomizeHeaders(_));
service_.SayHello(nullptr, &request, &response, nullptr);

EXPECT_CALL(grpc_callbacks_, onFailure(Optional<uint64_t>(), "stream reset"));
Expand All @@ -223,6 +235,7 @@ TEST_F(GrpcRequestImplTest, HttpAsyncRequestTimeout) {
helloworld::HelloRequest request;
request.set_name("a name");
helloworld::HelloReply response;
EXPECT_CALL(grpc_callbacks_, onPreRequestCustomizeHeaders(_));
service_.SayHello(nullptr, &request, &response, nullptr);

EXPECT_CALL(grpc_callbacks_, onFailure(Optional<uint64_t>(), "request timeout"));
Expand All @@ -243,6 +256,7 @@ TEST_F(GrpcRequestImplTest, NoHttpAsyncRequest) {
helloworld::HelloRequest request;
request.set_name("a name");
helloworld::HelloReply response;
EXPECT_CALL(grpc_callbacks_, onPreRequestCustomizeHeaders(_));
service_.SayHello(nullptr, &request, &response, nullptr);
}

Expand All @@ -252,6 +266,7 @@ TEST_F(GrpcRequestImplTest, Cancel) {
helloworld::HelloRequest request;
request.set_name("a name");
helloworld::HelloReply response;
EXPECT_CALL(grpc_callbacks_, onPreRequestCustomizeHeaders(_));
service_.SayHello(nullptr, &request, &response, nullptr);

EXPECT_CALL(http_async_client_request_, cancel());
Expand All @@ -267,6 +282,7 @@ TEST_F(GrpcRequestImplTest, RequestTimeoutSet) {
helloworld::HelloRequest request;
request.set_name("a name");
helloworld::HelloReply response;
EXPECT_CALL(grpc_callbacks_, onPreRequestCustomizeHeaders(_));
service_timeout.SayHello(nullptr, &request, &response, nullptr);

Http::MessagePtr response_http_message(new Http::ResponseMessageImpl(
Expand Down
1 change: 1 addition & 0 deletions test/mocks/grpc/mocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class MockRpcChannelCallbacks : public RpcChannelCallbacks {
MockRpcChannelCallbacks();
~MockRpcChannelCallbacks();

MOCK_METHOD1(onPreRequestCustomizeHeaders, void(Http::HeaderMap& headers));
MOCK_METHOD0(onSuccess, void());
MOCK_METHOD2(onFailure, void(const Optional<uint64_t>& grpc_status, const std::string& message));
};
Expand Down