Skip to content

Commit 50be893

Browse files
authored
Merge pull request grpc#6915 from markdroth/filter_api
Implement C++ API for defining channel filters.
2 parents 220d14e + 00e9c3b commit 50be893

27 files changed

+1290
-13
lines changed

BUILD

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,6 +1251,7 @@ cc_library(
12511251
"src/cpp/common/secure_auth_context.h",
12521252
"src/cpp/server/secure_server_credentials.h",
12531253
"src/cpp/client/create_channel_internal.h",
1254+
"src/cpp/common/channel_filter.h",
12541255
"src/cpp/server/dynamic_thread_pool.h",
12551256
"src/cpp/server/thread_pool_interface.h",
12561257
"src/cpp/client/secure_credentials.cc",
@@ -1268,6 +1269,7 @@ cc_library(
12681269
"src/cpp/client/generic_stub.cc",
12691270
"src/cpp/client/insecure_credentials.cc",
12701271
"src/cpp/common/channel_arguments.cc",
1272+
"src/cpp/common/channel_filter.cc",
12711273
"src/cpp/common/completion_queue.cc",
12721274
"src/cpp/common/core_codegen.cc",
12731275
"src/cpp/common/rpc_method.cc",
@@ -1482,6 +1484,7 @@ cc_library(
14821484
name = "grpc++_unsecure",
14831485
srcs = [
14841486
"src/cpp/client/create_channel_internal.h",
1487+
"src/cpp/common/channel_filter.h",
14851488
"src/cpp/server/dynamic_thread_pool.h",
14861489
"src/cpp/server/thread_pool_interface.h",
14871490
"src/cpp/common/insecure_create_auth_context.cc",
@@ -1494,6 +1497,7 @@ cc_library(
14941497
"src/cpp/client/generic_stub.cc",
14951498
"src/cpp/client/insecure_credentials.cc",
14961499
"src/cpp/common/channel_arguments.cc",
1500+
"src/cpp/common/channel_filter.cc",
14971501
"src/cpp/common/completion_queue.cc",
14981502
"src/cpp/common/core_codegen.cc",
14991503
"src/cpp/common/rpc_method.cc",

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,7 @@ add_library(grpc++
10001000
src/cpp/client/generic_stub.cc
10011001
src/cpp/client/insecure_credentials.cc
10021002
src/cpp/common/channel_arguments.cc
1003+
src/cpp/common/channel_filter.cc
10031004
src/cpp/common/completion_queue.cc
10041005
src/cpp/common/core_codegen.cc
10051006
src/cpp/common/rpc_method.cc
@@ -1255,6 +1256,7 @@ add_library(grpc++_unsecure
12551256
src/cpp/client/generic_stub.cc
12561257
src/cpp/client/insecure_credentials.cc
12571258
src/cpp/common/channel_arguments.cc
1259+
src/cpp/common/channel_filter.cc
12581260
src/cpp/common/completion_queue.cc
12591261
src/cpp/common/core_codegen.cc
12601262
src/cpp/common/rpc_method.cc

Makefile

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,6 +1039,7 @@ cxx_slice_test: $(BINDIR)/$(CONFIG)/cxx_slice_test
10391039
cxx_string_ref_test: $(BINDIR)/$(CONFIG)/cxx_string_ref_test
10401040
cxx_time_test: $(BINDIR)/$(CONFIG)/cxx_time_test
10411041
end2end_test: $(BINDIR)/$(CONFIG)/end2end_test
1042+
filter_end2end_test: $(BINDIR)/$(CONFIG)/filter_end2end_test
10421043
generic_end2end_test: $(BINDIR)/$(CONFIG)/generic_end2end_test
10431044
golden_file_test: $(BINDIR)/$(CONFIG)/golden_file_test
10441045
grpc_cli: $(BINDIR)/$(CONFIG)/grpc_cli
@@ -1402,6 +1403,7 @@ buildtests_cxx: privatelibs_cxx \
14021403
$(BINDIR)/$(CONFIG)/cxx_string_ref_test \
14031404
$(BINDIR)/$(CONFIG)/cxx_time_test \
14041405
$(BINDIR)/$(CONFIG)/end2end_test \
1406+
$(BINDIR)/$(CONFIG)/filter_end2end_test \
14051407
$(BINDIR)/$(CONFIG)/generic_end2end_test \
14061408
$(BINDIR)/$(CONFIG)/golden_file_test \
14071409
$(BINDIR)/$(CONFIG)/grpc_cli \
@@ -1487,6 +1489,7 @@ buildtests_cxx: privatelibs_cxx \
14871489
$(BINDIR)/$(CONFIG)/cxx_string_ref_test \
14881490
$(BINDIR)/$(CONFIG)/cxx_time_test \
14891491
$(BINDIR)/$(CONFIG)/end2end_test \
1492+
$(BINDIR)/$(CONFIG)/filter_end2end_test \
14901493
$(BINDIR)/$(CONFIG)/generic_end2end_test \
14911494
$(BINDIR)/$(CONFIG)/golden_file_test \
14921495
$(BINDIR)/$(CONFIG)/grpc_cli \
@@ -1775,6 +1778,8 @@ test_cxx: buildtests_cxx
17751778
$(Q) $(BINDIR)/$(CONFIG)/cxx_time_test || ( echo test cxx_time_test failed ; exit 1 )
17761779
$(E) "[RUN] Testing end2end_test"
17771780
$(Q) $(BINDIR)/$(CONFIG)/end2end_test || ( echo test end2end_test failed ; exit 1 )
1781+
$(E) "[RUN] Testing filter_end2end_test"
1782+
$(Q) $(BINDIR)/$(CONFIG)/filter_end2end_test || ( echo test filter_end2end_test failed ; exit 1 )
17781783
$(E) "[RUN] Testing generic_end2end_test"
17791784
$(Q) $(BINDIR)/$(CONFIG)/generic_end2end_test || ( echo test generic_end2end_test failed ; exit 1 )
17801785
$(E) "[RUN] Testing golden_file_test"
@@ -3581,6 +3586,7 @@ LIBGRPC++_SRC = \
35813586
src/cpp/client/generic_stub.cc \
35823587
src/cpp/client/insecure_credentials.cc \
35833588
src/cpp/common/channel_arguments.cc \
3589+
src/cpp/common/channel_filter.cc \
35843590
src/cpp/common/completion_queue.cc \
35853591
src/cpp/common/core_codegen.cc \
35863592
src/cpp/common/rpc_method.cc \
@@ -4117,6 +4123,7 @@ LIBGRPC++_UNSECURE_SRC = \
41174123
src/cpp/client/generic_stub.cc \
41184124
src/cpp/client/insecure_credentials.cc \
41194125
src/cpp/common/channel_arguments.cc \
4126+
src/cpp/common/channel_filter.cc \
41204127
src/cpp/common/completion_queue.cc \
41214128
src/cpp/common/core_codegen.cc \
41224129
src/cpp/common/rpc_method.cc \
@@ -11168,6 +11175,49 @@ endif
1116811175
endif
1116911176

1117011177

11178+
FILTER_END2END_TEST_SRC = \
11179+
test/cpp/end2end/filter_end2end_test.cc \
11180+
11181+
FILTER_END2END_TEST_OBJS = $(addprefix $(OBJDIR)/$(CONFIG)/, $(addsuffix .o, $(basename $(FILTER_END2END_TEST_SRC))))
11182+
ifeq ($(NO_SECURE),true)
11183+
11184+
# You can't build secure targets if you don't have OpenSSL.
11185+
11186+
$(BINDIR)/$(CONFIG)/filter_end2end_test: openssl_dep_error
11187+
11188+
else
11189+
11190+
11191+
11192+
11193+
ifeq ($(NO_PROTOBUF),true)
11194+
11195+
# You can't build the protoc plugins or protobuf-enabled targets if you don't have protobuf 3.0.0+.
11196+
11197+
$(BINDIR)/$(CONFIG)/filter_end2end_test: protobuf_dep_error
11198+
11199+
else
11200+
11201+
$(BINDIR)/$(CONFIG)/filter_end2end_test: $(PROTOBUF_DEP) $(FILTER_END2END_TEST_OBJS) $(LIBDIR)/$(CONFIG)/libgrpc++_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a
11202+
$(E) "[LD] Linking $@"
11203+
$(Q) mkdir -p `dirname $@`
11204+
$(Q) $(LDXX) $(LDFLAGS) $(FILTER_END2END_TEST_OBJS) $(LIBDIR)/$(CONFIG)/libgrpc++_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a $(LDLIBSXX) $(LDLIBS_PROTOBUF) $(LDLIBS) $(LDLIBS_SECURE) $(GTEST_LIB) -o $(BINDIR)/$(CONFIG)/filter_end2end_test
11205+
11206+
endif
11207+
11208+
endif
11209+
11210+
$(OBJDIR)/$(CONFIG)/test/cpp/end2end/filter_end2end_test.o: $(LIBDIR)/$(CONFIG)/libgrpc++_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc_test_util.a $(LIBDIR)/$(CONFIG)/libgrpc++.a $(LIBDIR)/$(CONFIG)/libgrpc.a $(LIBDIR)/$(CONFIG)/libgpr_test_util.a $(LIBDIR)/$(CONFIG)/libgpr.a
11211+
11212+
deps_filter_end2end_test: $(FILTER_END2END_TEST_OBJS:.o=.dep)
11213+
11214+
ifneq ($(NO_SECURE),true)
11215+
ifneq ($(NO_DEPS),true)
11216+
-include $(FILTER_END2END_TEST_OBJS:.o=.dep)
11217+
endif
11218+
endif
11219+
11220+
1117111221
GENERIC_END2END_TEST_SRC = \
1117211222
test/cpp/end2end/generic_end2end_test.cc \
1117311223

build.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,7 @@ filegroups:
692692
- include/grpc++/support/time.h
693693
headers:
694694
- src/cpp/client/create_channel_internal.h
695+
- src/cpp/common/channel_filter.h
695696
- src/cpp/server/dynamic_thread_pool.h
696697
- src/cpp/server/thread_pool_interface.h
697698
src:
@@ -704,6 +705,7 @@ filegroups:
704705
- src/cpp/client/generic_stub.cc
705706
- src/cpp/client/insecure_credentials.cc
706707
- src/cpp/common/channel_arguments.cc
708+
- src/cpp/common/channel_filter.cc
707709
- src/cpp/common/completion_queue.cc
708710
- src/cpp/common/core_codegen.cc
709711
- src/cpp/common/rpc_method.cc
@@ -2666,6 +2668,19 @@ targets:
26662668
- grpc
26672669
- gpr_test_util
26682670
- gpr
2671+
- name: filter_end2end_test
2672+
gtest: true
2673+
build: test
2674+
language: c++
2675+
src:
2676+
- test/cpp/end2end/filter_end2end_test.cc
2677+
deps:
2678+
- grpc++_test_util
2679+
- grpc_test_util
2680+
- grpc++
2681+
- grpc
2682+
- gpr_test_util
2683+
- gpr
26692684
- name: generic_end2end_test
26702685
gtest: true
26712686
build: test

src/core/lib/channel/channel_stack.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@
5151
#include "src/core/lib/iomgr/polling_entity.h"
5252
#include "src/core/lib/transport/transport.h"
5353

54+
#ifdef __cplusplus
55+
extern "C" {
56+
#endif
57+
5458
typedef struct grpc_channel_element grpc_channel_element;
5559
typedef struct grpc_call_element grpc_call_element;
5660

@@ -291,4 +295,8 @@ extern int grpc_trace_channel;
291295
#define GRPC_CALL_LOG_OP(sev, elem, op) \
292296
if (grpc_trace_channel) grpc_call_log_op(sev, elem, op)
293297

298+
#ifdef __cplusplus
299+
}
300+
#endif
301+
294302
#endif /* GRPC_CORE_LIB_CHANNEL_CHANNEL_STACK_H */

src/core/lib/channel/channel_stack_builder.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939
#include "src/core/lib/channel/channel_args.h"
4040
#include "src/core/lib/channel/channel_stack.h"
4141

42+
#ifdef __cplusplus
43+
extern "C" {
44+
#endif
45+
4246
/// grpc_channel_stack_builder offers a programmatic interface to selected
4347
/// and order channel filters
4448
typedef struct grpc_channel_stack_builder grpc_channel_stack_builder;
@@ -158,4 +162,8 @@ void grpc_channel_stack_builder_destroy(grpc_channel_stack_builder *builder);
158162

159163
extern int grpc_trace_channel_stack_builder;
160164

165+
#ifdef __cplusplus
166+
}
167+
#endif
168+
161169
#endif /* GRPC_CORE_LIB_CHANNEL_CHANNEL_STACK_BUILDER_H */

src/core/lib/channel/context.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,19 @@
3434
#ifndef GRPC_CORE_LIB_CHANNEL_CONTEXT_H
3535
#define GRPC_CORE_LIB_CHANNEL_CONTEXT_H
3636

37-
/* Call object context pointers */
37+
/// Call object context pointers.
38+
39+
/// Call context is represented as an array of \a grpc_call_context_elements.
40+
/// This enum represents the indexes into the array, where each index
41+
/// contains a different type of value.
3842
typedef enum {
43+
/// Value is either a \a grpc_client_security_context or a
44+
/// \a grpc_server_security_context.
3945
GRPC_CONTEXT_SECURITY = 0,
46+
47+
/// Value is a \a census_context.
4048
GRPC_CONTEXT_TRACING,
49+
4150
GRPC_CONTEXT_COUNT
4251
} grpc_context_index;
4352

src/core/lib/security/context/security_context.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
#include "src/core/lib/iomgr/pollset.h"
3838
#include "src/core/lib/security/credentials/credentials.h"
3939

40+
#ifdef __cplusplus
41+
extern "C" {
42+
#endif
43+
4044
/* --- grpc_auth_context ---
4145
4246
High level authentication context object. Can optionally be chained. */
@@ -111,4 +115,8 @@ grpc_auth_context *grpc_auth_context_from_arg(const grpc_arg *arg);
111115
grpc_auth_context *grpc_find_auth_context_in_args(
112116
const grpc_channel_args *args);
113117

118+
#ifdef __cplusplus
119+
}
120+
#endif
121+
114122
#endif /* GRPC_CORE_LIB_SECURITY_CONTEXT_SECURITY_CONTEXT_H */

src/core/lib/surface/channel_init.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@
4040

4141
#define GRPC_CHANNEL_INIT_BUILTIN_PRIORITY 10000
4242

43+
#ifdef __cplusplus
44+
extern "C" {
45+
#endif
46+
4347
/// This module provides a way for plugins (and the grpc core library itself)
4448
/// to register mutators for channel stacks.
4549
/// It also provides a universal entry path to run those mutators to build
@@ -84,4 +88,8 @@ bool grpc_channel_init_create_stack(grpc_exec_ctx *exec_ctx,
8488
grpc_channel_stack_builder *builder,
8589
grpc_channel_stack_type type);
8690

91+
#ifdef __cplusplus
92+
}
93+
#endif
94+
8795
#endif /* GRPC_CORE_LIB_SURFACE_CHANNEL_INIT_H */

src/core/lib/surface/server.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ static void shutdown_cleanup(grpc_exec_ctx *exec_ctx, void *arg,
272272
}
273273

274274
static void send_shutdown(grpc_exec_ctx *exec_ctx, grpc_channel *channel,
275-
int send_goaway, grpc_error *send_disconnect) {
275+
bool send_goaway, grpc_error *send_disconnect) {
276276
grpc_transport_op op;
277277
struct shutdown_cleanup_args *sc;
278278
grpc_channel_element *elem;
@@ -293,7 +293,7 @@ static void send_shutdown(grpc_exec_ctx *exec_ctx, grpc_channel *channel,
293293

294294
static void channel_broadcaster_shutdown(grpc_exec_ctx *exec_ctx,
295295
channel_broadcaster *cb,
296-
int send_goaway,
296+
bool send_goaway,
297297
grpc_error *force_disconnect) {
298298
size_t i;
299299

@@ -1252,7 +1252,8 @@ void grpc_server_shutdown_and_notify(grpc_server *server,
12521252
l->destroy(&exec_ctx, server, l->arg, &l->destroy_done);
12531253
}
12541254

1255-
channel_broadcaster_shutdown(&exec_ctx, &broadcaster, 1, 0);
1255+
channel_broadcaster_shutdown(&exec_ctx, &broadcaster, true /* send_goaway */,
1256+
GRPC_ERROR_NONE);
12561257

12571258
done:
12581259
grpc_exec_ctx_finish(&exec_ctx);
@@ -1268,7 +1269,7 @@ void grpc_server_cancel_all_calls(grpc_server *server) {
12681269
channel_broadcaster_init(server, &broadcaster);
12691270
gpr_mu_unlock(&server->mu_global);
12701271

1271-
channel_broadcaster_shutdown(&exec_ctx, &broadcaster, 0,
1272+
channel_broadcaster_shutdown(&exec_ctx, &broadcaster, false /* send_goaway */,
12721273
GRPC_ERROR_CREATE("Cancelling all calls"));
12731274
grpc_exec_ctx_finish(&exec_ctx);
12741275
}

src/core/lib/transport/byte_stream.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,9 @@ struct grpc_byte_stream {
5959
* on_complete will not be called), 0 if the bytes will be available
6060
* asynchronously.
6161
*
62-
* on entry, *remaining can be set as a hint as to the maximum number
62+
* max_size_hint can be set as a hint as to the maximum number
6363
* of bytes that would be acceptable to read.
6464
*
65-
* fills *buffer, *length, *remaining with the bytes, length of bytes
66-
* and length of data remaining to be read before either returning 1
67-
* or calling on_complete.
68-
*
6965
* once a slice is returned into *slice, it is owned by the caller.
7066
*/
7167
int grpc_byte_stream_next(grpc_exec_ctx *exec_ctx,

src/core/lib/transport/metadata.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
#include <grpc/support/slice.h>
3838
#include <grpc/support/useful.h>
3939

40+
#ifdef __cplusplus
41+
extern "C" {
42+
#endif
43+
4044
/* This file provides a mechanism for tracking metadata through the grpc stack.
4145
It's not intended for consumption outside of the library.
4246
@@ -164,4 +168,8 @@ void grpc_mdctx_global_shutdown(void);
164168
extern gpr_slice (*grpc_chttp2_base64_encode_and_huffman_compress)(
165169
gpr_slice input);
166170

171+
#ifdef __cplusplus
172+
}
173+
#endif
174+
167175
#endif /* GRPC_CORE_LIB_TRANSPORT_METADATA_H */

src/core/lib/transport/metadata_batch.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
#include "src/core/lib/transport/metadata_batch.h"
3535

36+
#include <stdbool.h>
3637
#include <string.h>
3738

3839
#include <grpc/support/alloc.h>
@@ -187,7 +188,7 @@ void grpc_metadata_batch_clear(grpc_metadata_batch *batch) {
187188
grpc_metadata_batch_filter(batch, no_metadata_for_you, NULL);
188189
}
189190

190-
int grpc_metadata_batch_is_empty(grpc_metadata_batch *batch) {
191+
bool grpc_metadata_batch_is_empty(grpc_metadata_batch *batch) {
191192
return batch->list.head == NULL &&
192193
gpr_time_cmp(gpr_inf_future(batch->deadline.clock_type),
193194
batch->deadline) == 0;

0 commit comments

Comments
 (0)