Cleanup filter selection

reviewable/pr10449/r6
Craig Tiller 8 years ago
parent e376970691
commit 41f2ed68c4
  1. 10
      src/core/ext/filters/deadline/deadline_filter.c
  2. 9
      src/core/ext/filters/http/http_filters_plugin.c
  3. 10
      src/core/ext/filters/load_reporting/load_reporting.c
  4. 28
      src/core/ext/filters/max_age/max_age_filter.c
  5. 49
      src/core/ext/filters/message_size/message_size_filter.c
  6. 28
      src/core/lib/channel/channel_args.c
  7. 5
      src/core/lib/channel/channel_args.h

@ -346,13 +346,9 @@ const grpc_channel_filter grpc_server_deadline_filter = {
}; };
bool grpc_deadline_checking_enabled(const grpc_channel_args* channel_args) { bool grpc_deadline_checking_enabled(const grpc_channel_args* channel_args) {
bool enable = !grpc_channel_args_want_minimal_stack(channel_args); return grpc_channel_arg_get_bool(
const grpc_arg* a = grpc_channel_args_find(channel_args, GRPC_ARG_ENABLE_DEADLINE_CHECKS),
grpc_channel_args_find(channel_args, GRPC_ARG_ENABLE_DEADLINE_CHECKS); !grpc_channel_args_want_minimal_stack(channel_args));
if (a != NULL && a->type == GRPC_ARG_INTEGER && a->value.integer != 0) {
enable = true;
}
return enable;
} }
static bool maybe_add_deadline_filter(grpc_exec_ctx* exec_ctx, static bool maybe_add_deadline_filter(grpc_exec_ctx* exec_ctx,

@ -61,12 +61,9 @@ static bool maybe_add_optional_filter(grpc_exec_ctx *exec_ctx,
optional_filter *filtarg = arg; optional_filter *filtarg = arg;
const grpc_channel_args *channel_args = const grpc_channel_args *channel_args =
grpc_channel_stack_builder_get_channel_arguments(builder); grpc_channel_stack_builder_get_channel_arguments(builder);
bool enable = !grpc_channel_args_want_minimal_stack(channel_args); bool enable = grpc_channel_arg_get_bool(
const grpc_arg *ctlarg = grpc_channel_args_find(channel_args, filtarg->control_channel_arg),
grpc_channel_args_find(channel_args, filtarg->control_channel_arg); !grpc_channel_args_want_minimal_stack(channel_args));
if (ctlarg != NULL) {
enable = !(ctlarg->type == GRPC_ARG_INTEGER && ctlarg->value.integer == 0);
}
return enable ? grpc_channel_stack_builder_prepend_filter( return enable ? grpc_channel_stack_builder_prepend_filter(
builder, filtarg->filter, NULL, NULL) builder, filtarg->filter, NULL, NULL)
: true; : true;

@ -63,14 +63,8 @@ void grpc_call_set_load_reporting_cost_context(
} }
static bool is_load_reporting_enabled(const grpc_channel_args *a) { static bool is_load_reporting_enabled(const grpc_channel_args *a) {
if (a == NULL) return false; return grpc_channel_arg_get_bool(
for (size_t i = 0; i < a->num_args; i++) { grpc_channel_args_find(a, GRPC_ARG_ENABLE_LOAD_REPORTING), false);
if (0 == strcmp(a->args[i].key, GRPC_ARG_ENABLE_LOAD_REPORTING)) {
return a->args[i].type == GRPC_ARG_INTEGER &&
a->args[i].value.integer != 0;
}
}
return false;
} }
static bool maybe_add_load_reporting_filter(grpc_exec_ctx *exec_ctx, static bool maybe_add_load_reporting_filter(grpc_exec_ctx *exec_ctx,

@ -48,6 +48,11 @@
#define DEFAULT_MAX_CONNECTION_IDLE_MS INT_MAX #define DEFAULT_MAX_CONNECTION_IDLE_MS INT_MAX
#define MAX_CONNECTION_AGE_JITTER 0.1 #define MAX_CONNECTION_AGE_JITTER 0.1
#define MAX_CONNECTION_AGE_INTEGER_OPTIONS \
(grpc_integer_options) { DEFAULT_MAX_CONNECTION_AGE_MS, 1, INT_MAX }
#define MAX_CONNECTION_IDLE_INTEGER_OPTIONS \
(grpc_integer_options) { DEFAULT_MAX_CONNECTION_IDLE_MS, 1, INT_MAX }
typedef struct channel_data { typedef struct channel_data {
/* We take a reference to the channel stack for the timer callback */ /* We take a reference to the channel stack for the timer callback */
grpc_channel_stack* channel_stack; grpc_channel_stack* channel_stack;
@ -315,8 +320,7 @@ static grpc_error* init_channel_elem(grpc_exec_ctx* exec_ctx,
if (0 == strcmp(args->channel_args->args[i].key, if (0 == strcmp(args->channel_args->args[i].key,
GRPC_ARG_MAX_CONNECTION_AGE_MS)) { GRPC_ARG_MAX_CONNECTION_AGE_MS)) {
const int value = grpc_channel_arg_get_integer( const int value = grpc_channel_arg_get_integer(
&args->channel_args->args[i], &args->channel_args->args[i], MAX_CONNECTION_AGE_INTEGER_OPTIONS);
(grpc_integer_options){DEFAULT_MAX_CONNECTION_AGE_MS, 1, INT_MAX});
chand->max_connection_age = chand->max_connection_age =
value == INT_MAX value == INT_MAX
? gpr_inf_future(GPR_TIMESPAN) ? gpr_inf_future(GPR_TIMESPAN)
@ -334,8 +338,7 @@ static grpc_error* init_channel_elem(grpc_exec_ctx* exec_ctx,
} else if (0 == strcmp(args->channel_args->args[i].key, } else if (0 == strcmp(args->channel_args->args[i].key,
GRPC_ARG_MAX_CONNECTION_IDLE_MS)) { GRPC_ARG_MAX_CONNECTION_IDLE_MS)) {
const int value = grpc_channel_arg_get_integer( const int value = grpc_channel_arg_get_integer(
&args->channel_args->args[i], &args->channel_args->args[i], MAX_CONNECTION_IDLE_INTEGER_OPTIONS);
(grpc_integer_options){DEFAULT_MAX_CONNECTION_IDLE_MS, 1, INT_MAX});
chand->max_connection_idle = chand->max_connection_idle =
value == INT_MAX ? gpr_inf_future(GPR_TIMESPAN) value == INT_MAX ? gpr_inf_future(GPR_TIMESPAN)
: gpr_time_from_millis(value, GPR_TIMESPAN); : gpr_time_from_millis(value, GPR_TIMESPAN);
@ -412,16 +415,13 @@ static bool maybe_add_max_age_filter(grpc_exec_ctx* exec_ctx,
void* arg) { void* arg) {
const grpc_channel_args* channel_args = const grpc_channel_args* channel_args =
grpc_channel_stack_builder_get_channel_arguments(builder); grpc_channel_stack_builder_get_channel_arguments(builder);
const grpc_arg* a = bool enable =
grpc_channel_args_find(channel_args, GRPC_ARG_MAX_CONNECTION_AGE_MS); grpc_channel_arg_get_integer(
bool enable = false; grpc_channel_args_find(channel_args, GRPC_ARG_MAX_CONNECTION_AGE_MS),
if (a != NULL && a->type == GRPC_ARG_INTEGER && a->value.integer != INT_MAX) { MAX_CONNECTION_AGE_INTEGER_OPTIONS) != INT_MAX &&
enable = true; grpc_channel_arg_get_integer(
} grpc_channel_args_find(channel_args, GRPC_ARG_MAX_CONNECTION_IDLE_MS),
a = grpc_channel_args_find(channel_args, GRPC_ARG_MAX_CONNECTION_IDLE_MS); MAX_CONNECTION_IDLE_INTEGER_OPTIONS) != INT_MAX;
if (a != NULL && a->type == GRPC_ARG_INTEGER && a->value.integer != INT_MAX) {
enable = true;
}
if (enable) { if (enable) {
return grpc_channel_stack_builder_prepend_filter( return grpc_channel_stack_builder_prepend_filter(
builder, &grpc_max_age_filter, NULL, NULL); builder, &grpc_max_age_filter, NULL, NULL);

@ -91,8 +91,7 @@ static void* message_size_limits_create_from_json(const grpc_json* json) {
} }
typedef struct call_data { typedef struct call_data {
int max_send_size; message_size_limits limits;
int max_recv_size;
// Receive closures are chained: we inject this closure as the // Receive closures are chained: we inject this closure as the
// recv_message_ready up-call on transport_stream_op, and remember to // recv_message_ready up-call on transport_stream_op, and remember to
// call our next_recv_message_ready member after handling it. // call our next_recv_message_ready member after handling it.
@ -104,8 +103,7 @@ typedef struct call_data {
} call_data; } call_data;
typedef struct channel_data { typedef struct channel_data {
int max_send_size; message_size_limits limits;
int max_recv_size;
// Maps path names to message_size_limits structs. // Maps path names to message_size_limits structs.
grpc_slice_hash_table* method_limit_table; grpc_slice_hash_table* method_limit_table;
} channel_data; } channel_data;
@ -116,12 +114,12 @@ static void recv_message_ready(grpc_exec_ctx* exec_ctx, void* user_data,
grpc_error* error) { grpc_error* error) {
grpc_call_element* elem = user_data; grpc_call_element* elem = user_data;
call_data* calld = elem->call_data; call_data* calld = elem->call_data;
if (*calld->recv_message != NULL && calld->max_recv_size >= 0 && if (*calld->recv_message != NULL && calld->limits.max_recv_size >= 0 &&
(*calld->recv_message)->length > (size_t)calld->max_recv_size) { (*calld->recv_message)->length > (size_t)calld->limits.max_recv_size) {
char* message_string; char* message_string;
gpr_asprintf(&message_string, gpr_asprintf(&message_string,
"Received message larger than max (%u vs. %d)", "Received message larger than max (%u vs. %d)",
(*calld->recv_message)->length, calld->max_recv_size); (*calld->recv_message)->length, calld->limits.max_recv_size);
grpc_error* new_error = grpc_error_set_int( grpc_error* new_error = grpc_error_set_int(
GRPC_ERROR_CREATE_FROM_COPIED_STRING(message_string), GRPC_ERROR_CREATE_FROM_COPIED_STRING(message_string),
GRPC_ERROR_INT_GRPC_STATUS, GRPC_STATUS_INVALID_ARGUMENT); GRPC_ERROR_INT_GRPC_STATUS, GRPC_STATUS_INVALID_ARGUMENT);
@ -143,13 +141,13 @@ static void start_transport_stream_op_batch(
grpc_transport_stream_op_batch* op) { grpc_transport_stream_op_batch* op) {
call_data* calld = elem->call_data; call_data* calld = elem->call_data;
// Check max send message size. // Check max send message size.
if (op->send_message && calld->max_send_size >= 0 && if (op->send_message && calld->limits.max_send_size >= 0 &&
op->payload->send_message.send_message->length > op->payload->send_message.send_message->length >
(size_t)calld->max_send_size) { (size_t)calld->limits.max_send_size) {
char* message_string; char* message_string;
gpr_asprintf(&message_string, "Sent message larger than max (%u vs. %d)", gpr_asprintf(&message_string, "Sent message larger than max (%u vs. %d)",
op->payload->send_message.send_message->length, op->payload->send_message.send_message->length,
calld->max_send_size); calld->limits.max_send_size);
grpc_transport_stream_op_batch_finish_with_failure( grpc_transport_stream_op_batch_finish_with_failure(
exec_ctx, op, exec_ctx, op,
grpc_error_set_int(GRPC_ERROR_CREATE_FROM_COPIED_STRING(message_string), grpc_error_set_int(GRPC_ERROR_CREATE_FROM_COPIED_STRING(message_string),
@ -182,21 +180,20 @@ static grpc_error* init_call_elem(grpc_exec_ctx* exec_ctx,
// Note: Per-method config is only available on the client, so we // Note: Per-method config is only available on the client, so we
// apply the max request size to the send limit and the max response // apply the max request size to the send limit and the max response
// size to the receive limit. // size to the receive limit.
calld->max_send_size = chand->max_send_size; calld->limits = chand->limits;
calld->max_recv_size = chand->max_recv_size;
if (chand->method_limit_table != NULL) { if (chand->method_limit_table != NULL) {
message_size_limits* limits = grpc_method_config_table_get( message_size_limits* limits = grpc_method_config_table_get(
exec_ctx, chand->method_limit_table, args->path); exec_ctx, chand->method_limit_table, args->path);
if (limits != NULL) { if (limits != NULL) {
if (limits->max_send_size >= 0 && if (limits->max_send_size >= 0 &&
(limits->max_send_size < calld->max_send_size || (limits->max_send_size < calld->limits.max_send_size ||
calld->max_send_size < 0)) { calld->limits.max_send_size < 0)) {
calld->max_send_size = limits->max_send_size; calld->limits.max_send_size = limits->max_send_size;
} }
if (limits->max_recv_size >= 0 && if (limits->max_recv_size >= 0 &&
(limits->max_recv_size < calld->max_recv_size || (limits->max_recv_size < calld->limits.max_recv_size ||
calld->max_recv_size < 0)) { calld->limits.max_recv_size < 0)) {
calld->max_recv_size = limits->max_recv_size; calld->limits.max_recv_size = limits->max_recv_size;
} }
} }
} }
@ -216,13 +213,9 @@ static int default_size(const grpc_channel_args* args,
return without_minimal_stack; return without_minimal_stack;
} }
typedef struct { message_size_limits get_message_size_limits(
int max_recv_size; const grpc_channel_args* channel_args) {
int max_send_size; message_size_limits lim;
} channel_limits;
channel_limits get_channel_limits(const grpc_channel_args* channel_args) {
channel_limits lim;
lim.max_send_size = lim.max_send_size =
default_size(channel_args, GRPC_DEFAULT_MAX_SEND_MESSAGE_LENGTH); default_size(channel_args, GRPC_DEFAULT_MAX_SEND_MESSAGE_LENGTH);
lim.max_recv_size = lim.max_recv_size =
@ -254,9 +247,7 @@ static grpc_error* init_channel_elem(grpc_exec_ctx* exec_ctx,
grpc_channel_element_args* args) { grpc_channel_element_args* args) {
GPR_ASSERT(!args->is_last); GPR_ASSERT(!args->is_last);
channel_data* chand = elem->channel_data; channel_data* chand = elem->channel_data;
channel_limits lim = get_channel_limits(args->channel_args); chand->limits = get_message_size_limits(args->channel_args);
chand->max_send_size = lim.max_send_size;
chand->max_recv_size = lim.max_recv_size;
// Get method config table from channel args. // Get method config table from channel args.
const grpc_arg* channel_arg = const grpc_arg* channel_arg =
grpc_channel_args_find(args->channel_args, GRPC_ARG_SERVICE_CONFIG); grpc_channel_args_find(args->channel_args, GRPC_ARG_SERVICE_CONFIG);
@ -302,7 +293,7 @@ static bool maybe_add_message_size_filter(grpc_exec_ctx* exec_ctx,
const grpc_channel_args* channel_args = const grpc_channel_args* channel_args =
grpc_channel_stack_builder_get_channel_arguments(builder); grpc_channel_stack_builder_get_channel_arguments(builder);
bool enable = false; bool enable = false;
channel_limits lim = get_channel_limits(channel_args); message_size_limits lim = get_message_size_limits(channel_args);
if (lim.max_send_size != INT_MAX || lim.max_recv_size != INT_MAX) { if (lim.max_send_size != INT_MAX || lim.max_recv_size != INT_MAX) {
enable = true; enable = true;
} }

@ -329,7 +329,9 @@ const grpc_arg *grpc_channel_args_find(const grpc_channel_args *args,
return NULL; return NULL;
} }
int grpc_channel_arg_get_integer(grpc_arg *arg, grpc_integer_options options) { int grpc_channel_arg_get_integer(const grpc_arg *arg,
grpc_integer_options options) {
if (arg == NULL) return options.default_value;
if (arg->type != GRPC_ARG_INTEGER) { if (arg->type != GRPC_ARG_INTEGER) {
gpr_log(GPR_ERROR, "%s ignored: it must be an integer", arg->key); gpr_log(GPR_ERROR, "%s ignored: it must be an integer", arg->key);
return options.default_value; return options.default_value;
@ -347,9 +349,25 @@ int grpc_channel_arg_get_integer(grpc_arg *arg, grpc_integer_options options) {
return arg->value.integer; return arg->value.integer;
} }
bool grpc_channel_arg_get_bool(const grpc_arg *arg, bool default_value) {
if (arg == NULL) return default_value;
if (arg->type != GRPC_ARG_INTEGER) {
gpr_log(GPR_ERROR, "%s ignored: it must be an integer", arg->key);
return default_value;
}
switch (arg->value.integer) {
case 0:
return false;
case 1:
return true;
default:
gpr_log(GPR_ERROR, "%s treated as bool but set to %d (assuming true)",
arg->key, arg->value.integer);
return true;
}
}
bool grpc_channel_args_want_minimal_stack(const grpc_channel_args *args) { bool grpc_channel_args_want_minimal_stack(const grpc_channel_args *args) {
const grpc_arg *arg = grpc_channel_args_find(args, GRPC_ARG_MINIMAL_STACK); return grpc_channel_arg_get_bool(
if (arg == NULL) return false; grpc_channel_args_find(args, GRPC_ARG_MINIMAL_STACK), false);
if (arg->type == GRPC_ARG_INTEGER && arg->value.integer == 0) return false;
return true;
} }

@ -121,6 +121,9 @@ typedef struct grpc_integer_options {
int max_value; int max_value;
} grpc_integer_options; } grpc_integer_options;
/** Returns the value of \a arg, subject to the contraints in \a options. */ /** Returns the value of \a arg, subject to the contraints in \a options. */
int grpc_channel_arg_get_integer(grpc_arg *arg, grpc_integer_options options); int grpc_channel_arg_get_integer(const grpc_arg *arg,
grpc_integer_options options);
bool grpc_channel_arg_get_bool(const grpc_arg *arg, bool default_value);
#endif /* GRPC_CORE_LIB_CHANNEL_CHANNEL_ARGS_H */ #endif /* GRPC_CORE_LIB_CHANNEL_CHANNEL_ARGS_H */

Loading…
Cancel
Save