Add grpc_channel_arg_get_integer() utility function.

pull/7846/head
Mark D. Roth 8 years ago
parent 54d78e2640
commit c3c6fafef8
  1. 18
      src/core/ext/client_config/subchannel.c
  2. 82
      src/core/ext/transport/chttp2/transport/chttp2_transport.c
  3. 18
      src/core/lib/channel/channel_args.c
  4. 8
      src/core/lib/channel/channel_args.h
  5. 28
      src/core/lib/channel/message_size_filter.c

@ -33,6 +33,7 @@
#include "src/core/ext/client_config/subchannel.h" #include "src/core/ext/client_config/subchannel.h"
#include <limits.h>
#include <string.h> #include <string.h>
#include <grpc/support/alloc.h> #include <grpc/support/alloc.h>
@ -347,21 +348,16 @@ grpc_subchannel *grpc_subchannel_create(grpc_exec_ctx *exec_ctx,
} }
if (0 == if (0 ==
strcmp(c->args->args[i].key, GRPC_ARG_MAX_RECONNECT_BACKOFF_MS)) { strcmp(c->args->args[i].key, GRPC_ARG_MAX_RECONNECT_BACKOFF_MS)) {
if (c->args->args[i].type == GRPC_ARG_INTEGER) { const grpc_integer_options options = {-1, 0, INT_MAX};
if (c->args->args[i].value.integer >= 0) { const int value = grpc_channel_arg_get_integer(&c->args->args[i],
options);
if (value >= 0) {
gpr_backoff_init( gpr_backoff_init(
&c->backoff_state, GRPC_SUBCHANNEL_RECONNECT_BACKOFF_MULTIPLIER, &c->backoff_state, GRPC_SUBCHANNEL_RECONNECT_BACKOFF_MULTIPLIER,
GRPC_SUBCHANNEL_RECONNECT_JITTER, GRPC_SUBCHANNEL_RECONNECT_JITTER,
GPR_MIN(c->args->args[i].value.integer, GPR_MIN(value,
GRPC_SUBCHANNEL_INITIAL_CONNECT_BACKOFF_SECONDS * 1000), GRPC_SUBCHANNEL_INITIAL_CONNECT_BACKOFF_SECONDS * 1000),
c->args->args[i].value.integer); value);
} else {
gpr_log(GPR_ERROR, GRPC_ARG_MAX_RECONNECT_BACKOFF_MS
" : must be non-negative");
}
} else {
gpr_log(GPR_ERROR,
GRPC_ARG_MAX_RECONNECT_BACKOFF_MS " : must be an integer");
} }
} }
} }

@ -33,6 +33,7 @@
#include "src/core/ext/transport/chttp2/transport/chttp2_transport.h" #include "src/core/ext/transport/chttp2/transport/chttp2_transport.h"
#include <limits.h>
#include <math.h> #include <math.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@ -46,6 +47,7 @@
#include "src/core/ext/transport/chttp2/transport/http2_errors.h" #include "src/core/ext/transport/chttp2/transport/http2_errors.h"
#include "src/core/ext/transport/chttp2/transport/internal.h" #include "src/core/ext/transport/chttp2/transport/internal.h"
#include "src/core/ext/transport/chttp2/transport/status_conversion.h" #include "src/core/ext/transport/chttp2/transport/status_conversion.h"
#include "src/core/lib/channel/channel_args.h"
#include "src/core/lib/http/parser.h" #include "src/core/lib/http/parser.h"
#include "src/core/lib/iomgr/workqueue.h" #include "src/core/lib/iomgr/workqueue.h"
#include "src/core/lib/profiling/timers.h" #include "src/core/lib/profiling/timers.h"
@ -337,76 +339,66 @@ static void init_transport(grpc_exec_ctx *exec_ctx, grpc_chttp2_transport *t,
if (is_client) { if (is_client) {
gpr_log(GPR_ERROR, "%s: is ignored on the client", gpr_log(GPR_ERROR, "%s: is ignored on the client",
GRPC_ARG_MAX_CONCURRENT_STREAMS); GRPC_ARG_MAX_CONCURRENT_STREAMS);
} else if (channel_args->args[i].type != GRPC_ARG_INTEGER) {
gpr_log(GPR_ERROR, "%s: must be an integer",
GRPC_ARG_MAX_CONCURRENT_STREAMS);
} else { } else {
push_setting(exec_ctx, t, GRPC_CHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, const grpc_integer_options options = {-1, 0, INT_MAX};
(uint32_t)channel_args->args[i].value.integer); const int value = grpc_channel_arg_get_integer(&channel_args->args[i],
options);
if (value >= 0) {
push_setting(exec_ctx, t,
GRPC_CHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS,
(uint32_t)value);
}
} }
} else if (0 == strcmp(channel_args->args[i].key, } else if (0 == strcmp(channel_args->args[i].key,
GRPC_ARG_HTTP2_INITIAL_SEQUENCE_NUMBER)) { GRPC_ARG_HTTP2_INITIAL_SEQUENCE_NUMBER)) {
if (channel_args->args[i].type != GRPC_ARG_INTEGER) { const grpc_integer_options options = {-1, 0, INT_MAX};
gpr_log(GPR_ERROR, "%s: must be an integer", const int value = grpc_channel_arg_get_integer(&channel_args->args[i],
GRPC_ARG_HTTP2_INITIAL_SEQUENCE_NUMBER); options);
} else if ((t->global.next_stream_id & 1) != if (value >= 0) {
(channel_args->args[i].value.integer & 1)) { if ((t->global.next_stream_id & 1) != (value & 1)) {
gpr_log(GPR_ERROR, "%s: low bit must be %d on %s", gpr_log(GPR_ERROR, "%s: low bit must be %d on %s",
GRPC_ARG_HTTP2_INITIAL_SEQUENCE_NUMBER, GRPC_ARG_HTTP2_INITIAL_SEQUENCE_NUMBER,
t->global.next_stream_id & 1, t->global.next_stream_id & 1,
is_client ? "client" : "server"); is_client ? "client" : "server");
} else { } else {
t->global.next_stream_id = t->global.next_stream_id = (uint32_t)value;
(uint32_t)channel_args->args[i].value.integer; }
} }
} else if (0 == strcmp(channel_args->args[i].key, } else if (0 == strcmp(channel_args->args[i].key,
GRPC_ARG_HTTP2_STREAM_LOOKAHEAD_BYTES)) { GRPC_ARG_HTTP2_STREAM_LOOKAHEAD_BYTES)) {
if (channel_args->args[i].type != GRPC_ARG_INTEGER) { const grpc_integer_options options = {-1, 5, INT_MAX};
gpr_log(GPR_ERROR, "%s: must be an integer", const int value = grpc_channel_arg_get_integer(&channel_args->args[i],
GRPC_ARG_HTTP2_STREAM_LOOKAHEAD_BYTES); options);
} else if (channel_args->args[i].value.integer <= 5) { if (value >= 0) {
gpr_log(GPR_ERROR, "%s: must be at least 5", t->global.stream_lookahead = (uint32_t)value;
GRPC_ARG_HTTP2_STREAM_LOOKAHEAD_BYTES);
} else {
t->global.stream_lookahead =
(uint32_t)channel_args->args[i].value.integer;
} }
} else if (0 == strcmp(channel_args->args[i].key, } else if (0 == strcmp(channel_args->args[i].key,
GRPC_ARG_HTTP2_HPACK_TABLE_SIZE_DECODER)) { GRPC_ARG_HTTP2_HPACK_TABLE_SIZE_DECODER)) {
if (channel_args->args[i].type != GRPC_ARG_INTEGER) { const grpc_integer_options options = {-1, 0, INT_MAX};
gpr_log(GPR_ERROR, "%s: must be an integer", const int value = grpc_channel_arg_get_integer(&channel_args->args[i],
GRPC_ARG_HTTP2_HPACK_TABLE_SIZE_DECODER); options);
} else if (channel_args->args[i].value.integer < 0) { if (value >= 0) {
gpr_log(GPR_ERROR, "%s: must be non-negative",
GRPC_ARG_HTTP2_HPACK_TABLE_SIZE_DECODER);
} else {
push_setting(exec_ctx, t, GRPC_CHTTP2_SETTINGS_HEADER_TABLE_SIZE, push_setting(exec_ctx, t, GRPC_CHTTP2_SETTINGS_HEADER_TABLE_SIZE,
(uint32_t)channel_args->args[i].value.integer); (uint32_t)value);
} }
} else if (0 == strcmp(channel_args->args[i].key, } else if (0 == strcmp(channel_args->args[i].key,
GRPC_ARG_HTTP2_HPACK_TABLE_SIZE_ENCODER)) { GRPC_ARG_HTTP2_HPACK_TABLE_SIZE_ENCODER)) {
if (channel_args->args[i].type != GRPC_ARG_INTEGER) { const grpc_integer_options options = {-1, 0, INT_MAX};
gpr_log(GPR_ERROR, "%s: must be an integer", const int value = grpc_channel_arg_get_integer(&channel_args->args[i],
GRPC_ARG_HTTP2_HPACK_TABLE_SIZE_ENCODER); options);
} else if (channel_args->args[i].value.integer < 0) { if (value >= 0) {
gpr_log(GPR_ERROR, "%s: must be non-negative",
GRPC_ARG_HTTP2_HPACK_TABLE_SIZE_ENCODER);
} else {
grpc_chttp2_hpack_compressor_set_max_usable_size( grpc_chttp2_hpack_compressor_set_max_usable_size(
&t->writing.hpack_compressor, &t->writing.hpack_compressor,
(uint32_t)channel_args->args[i].value.integer); (uint32_t)value);
} }
} else if (0 == strcmp(channel_args->args[i].key, } else if (0 == strcmp(channel_args->args[i].key,
GRPC_ARG_MAX_METADATA_SIZE)) { GRPC_ARG_MAX_METADATA_SIZE)) {
if (channel_args->args[i].type != GRPC_ARG_INTEGER) { const grpc_integer_options options = {-1, 0, INT_MAX};
gpr_log(GPR_ERROR, "%s: must be an integer", const int value = grpc_channel_arg_get_integer(&channel_args->args[i],
GRPC_ARG_MAX_METADATA_SIZE); options);
} else if (channel_args->args[i].value.integer < 0) { if (value >= 0) {
gpr_log(GPR_ERROR, "%s: must be non-negative",
GRPC_ARG_MAX_METADATA_SIZE);
} else {
push_setting(exec_ctx, t, GRPC_CHTTP2_SETTINGS_MAX_HEADER_LIST_SIZE, push_setting(exec_ctx, t, GRPC_CHTTP2_SETTINGS_MAX_HEADER_LIST_SIZE,
(uint32_t)channel_args->args[i].value.integer); (uint32_t)value);
} }
} }
} }

@ -271,3 +271,21 @@ int grpc_channel_args_compare(const grpc_channel_args *a,
} }
return 0; return 0;
} }
int grpc_channel_arg_get_integer(grpc_arg *arg, grpc_integer_options options) {
if (arg->type != GRPC_ARG_INTEGER) {
gpr_log(GPR_ERROR, "%s ignored: it must be an integer", arg->key);
return options.default_value;
}
if (arg->value.integer < options.min_value) {
gpr_log(GPR_ERROR, "%s ignored: it must be >= %d", arg->key,
options.min_value);
return options.default_value;
}
if (arg->value.integer > options.max_value) {
gpr_log(GPR_ERROR, "%s ignored: it must be <= %d", arg->key,
options.max_value);
return options.default_value;
}
return arg->value.integer;
}

@ -89,4 +89,12 @@ uint32_t grpc_channel_args_compression_algorithm_get_states(
int grpc_channel_args_compare(const grpc_channel_args *a, int grpc_channel_args_compare(const grpc_channel_args *a,
const grpc_channel_args *b); const grpc_channel_args *b);
/** Returns the value of \a arg, subject to the contraints in \a options. */
typedef struct grpc_integer_options {
int default_value; // Return this if value is outside of expected bounds.
int min_value;
int max_value;
} grpc_integer_options;
int grpc_channel_arg_get_integer(grpc_arg *arg, grpc_integer_options options);
#endif /* GRPC_CORE_LIB_CHANNEL_CHANNEL_ARGS_H */ #endif /* GRPC_CORE_LIB_CHANNEL_CHANNEL_ARGS_H */

@ -31,12 +31,15 @@
#include "src/core/lib/channel/message_size_filter.h" #include "src/core/lib/channel/message_size_filter.h"
#include <limits.h>
#include <string.h> #include <string.h>
#include <grpc/support/alloc.h> #include <grpc/support/alloc.h>
#include <grpc/support/log.h> #include <grpc/support/log.h>
#include <grpc/support/string_util.h> #include <grpc/support/string_util.h>
#include "src/core/lib/channel/channel_args.h"
// The protobuf library will (by default) start warning at 100 megs. // The protobuf library will (by default) start warning at 100 megs.
#define DEFAULT_MAX_MESSAGE_LENGTH (4 * 1024 * 1024) #define DEFAULT_MAX_MESSAGE_LENGTH (4 * 1024 * 1024)
@ -129,32 +132,17 @@ static void init_channel_elem(grpc_exec_ctx* exec_ctx,
memset(chand, 0, sizeof(*chand)); memset(chand, 0, sizeof(*chand));
chand->max_send_size = DEFAULT_MAX_MESSAGE_LENGTH; chand->max_send_size = DEFAULT_MAX_MESSAGE_LENGTH;
chand->max_recv_size = DEFAULT_MAX_MESSAGE_LENGTH; chand->max_recv_size = DEFAULT_MAX_MESSAGE_LENGTH;
const grpc_integer_options options = {DEFAULT_MAX_MESSAGE_LENGTH, 0, INT_MAX};
for (size_t i = 0; i < args->channel_args->num_args; ++i) { for (size_t i = 0; i < args->channel_args->num_args; ++i) {
if (strcmp(args->channel_args->args[i].key, if (strcmp(args->channel_args->args[i].key,
GRPC_ARG_MAX_SEND_MESSAGE_LENGTH) == 0) { GRPC_ARG_MAX_SEND_MESSAGE_LENGTH) == 0) {
if (args->channel_args->args[i].type != GRPC_ARG_INTEGER) { chand->max_send_size = (size_t)grpc_channel_arg_get_integer(
gpr_log(GPR_ERROR, "%s ignored: it must be an integer", &args->channel_args->args[i], options);
GRPC_ARG_MAX_SEND_MESSAGE_LENGTH);
} else if (args->channel_args->args[i].value.integer < 0) {
gpr_log(GPR_ERROR, "%s ignored: it must be >= 0",
GRPC_ARG_MAX_SEND_MESSAGE_LENGTH);
} else {
chand->max_send_size =
(size_t)args->channel_args->args[i].value.integer;
}
} }
if (strcmp(args->channel_args->args[i].key, if (strcmp(args->channel_args->args[i].key,
GRPC_ARG_MAX_RECEIVE_MESSAGE_LENGTH) == 0) { GRPC_ARG_MAX_RECEIVE_MESSAGE_LENGTH) == 0) {
if (args->channel_args->args[i].type != GRPC_ARG_INTEGER) { chand->max_recv_size = (size_t)grpc_channel_arg_get_integer(
gpr_log(GPR_ERROR, "%s ignored: it must be an integer", &args->channel_args->args[i], options);
GRPC_ARG_MAX_RECEIVE_MESSAGE_LENGTH);
} else if (args->channel_args->args[i].value.integer < 0) {
gpr_log(GPR_ERROR, "%s ignored: it must be >= 0",
GRPC_ARG_MAX_RECEIVE_MESSAGE_LENGTH);
} else {
chand->max_recv_size =
(size_t)args->channel_args->args[i].value.integer;
}
} }
} }
} }

Loading…
Cancel
Save