diff options
author | David Garcia Quintas <dgq@google.com> | 2015-08-10 13:39:52 -0700 |
---|---|---|
committer | David Garcia Quintas <dgq@google.com> | 2015-08-10 15:08:41 -0700 |
commit | beac88ca56f4710e86668f2cbbd80e02e0607f9c (patch) | |
tree | 84ee80880d363b655b97eb964ef3adfe0912c5be /src/core | |
parent | 47245f509cd9d8ec807b6bdeb6958243d9676fa5 (diff) |
Server: added the ability to disable compression algorithm
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/channel/channel_args.c | 27 | ||||
-rw-r--r-- | src/core/channel/channel_args.h | 15 | ||||
-rw-r--r-- | src/core/channel/compress_filter.c | 28 | ||||
-rw-r--r-- | src/core/compression/algorithm.c | 23 |
4 files changed, 73 insertions, 20 deletions
diff --git a/src/core/channel/channel_args.c b/src/core/channel/channel_args.c index 10199f7719..7d97b79553 100644 --- a/src/core/channel/channel_args.c +++ b/src/core/channel/channel_args.c @@ -148,16 +148,19 @@ grpc_channel_args *grpc_channel_args_set_compression_algorithm( return grpc_channel_args_copy_and_add(a, &tmp, 1); } +/** Returns the compression algorithm's enabled states bitset from \a a. If not + * found, return a biset will all algorithms enabled */ static gpr_uint32 find_compression_algorithm_states_bitset( const grpc_channel_args *a) { - size_t i; - gpr_uint32 states_bitset = 0; - if (a == NULL) return 0; - for (i = 0; i < a->num_args; ++i) { - if (a->args[i].type == GRPC_ARG_INTEGER && - !strcmp(GRPC_COMPRESSION_ALGORITHM_STATE_ARG, a->args[i].key)) { - states_bitset = a->args[i].value.integer; - break; + gpr_uint32 states_bitset = (1u << GRPC_COMPRESS_ALGORITHMS_COUNT) - 1; + if (a != NULL) { + size_t i; + for (i = 0; i < a->num_args; ++i) { + if (a->args[i].type == GRPC_ARG_INTEGER && + !strcmp(GRPC_COMPRESSION_ALGORITHM_STATE_ARG, a->args[i].key)) { + states_bitset = a->args[i].value.integer; + break; + } } } return states_bitset; @@ -182,9 +185,7 @@ grpc_channel_args *grpc_channel_args_compression_algorithm_set_state( return grpc_channel_args_copy_and_add(a, &tmp, 1); } -int grpc_channel_args_compression_algorithm_get_state( - grpc_channel_args *a, - grpc_compression_algorithm algorithm) { - const gpr_uint32 states_bitset = find_compression_algorithm_states_bitset(a); - return GPR_BITGET(states_bitset, algorithm); +int grpc_channel_args_compression_algorithm_get_states( + const grpc_channel_args *a) { + return find_compression_algorithm_states_bitset(a); } diff --git a/src/core/channel/channel_args.h b/src/core/channel/channel_args.h index f1a75117af..e557f9a9d9 100644 --- a/src/core/channel/channel_args.h +++ b/src/core/channel/channel_args.h @@ -68,17 +68,20 @@ grpc_channel_args *grpc_channel_args_set_compression_algorithm( grpc_channel_args *a, grpc_compression_algorithm algorithm); /** Sets the support for the given compression algorithm. By default, all - * compression algorithms are enabled. Disabling an algorithm set by - * grpc_channel_args_set_compression_algorithm disables compression altogether + * compression algorithms are enabled. It's an error to disable an algorithm set + * by grpc_channel_args_set_compression_algorithm. * */ grpc_channel_args *grpc_channel_args_compression_algorithm_set_state( grpc_channel_args *a, grpc_compression_algorithm algorithm, int enabled); -/** Returns the state (true for enabled, false for disabled) for \a algorithm */ -int grpc_channel_args_compression_algorithm_get_state( - grpc_channel_args *a, - grpc_compression_algorithm algorithm); +/** Returns the bitset representing the support state (true for enabled, false + * for disabled) for compression algorithms. + * + * The i-th bit of the returned bitset corresponds to the i-th entry in the + * grpc_compression_algorithm enum. */ +int grpc_channel_args_compression_algorithm_get_states( + const grpc_channel_args *a); #endif /* GRPC_INTERNAL_CORE_CHANNEL_CHANNEL_ARGS_H */ diff --git a/src/core/channel/compress_filter.c b/src/core/channel/compress_filter.c index 2fd4c8cae6..065fe258dc 100644 --- a/src/core/channel/compress_filter.c +++ b/src/core/channel/compress_filter.c @@ -70,6 +70,8 @@ typedef struct channel_data { grpc_mdelem *mdelem_accept_encoding; /** The default, channel-level, compression algorithm */ grpc_compression_algorithm default_compression_algorithm; + /** Compression options for the channel */ + grpc_compression_options compression_options; } channel_data; /** Compress \a slices in place using \a algorithm. Returns 1 if compression did @@ -102,7 +104,17 @@ static grpc_mdelem* compression_md_filter(void *user_data, grpc_mdelem *md) { const char *md_c_str = grpc_mdstr_as_c_string(md->value); if (!grpc_compression_algorithm_parse(md_c_str, strlen(md_c_str), &calld->compression_algorithm)) { - gpr_log(GPR_ERROR, "Invalid compression algorithm: '%s'. Ignoring.", + gpr_log(GPR_ERROR, + "Invalid compression algorithm: '%s' (unknown). Ignoring.", + md_c_str); + calld->compression_algorithm = GRPC_COMPRESS_NONE; + } + if (grpc_compression_options_is_algorithm_enabled( + &channeld->compression_options, calld->compression_algorithm) == 0) + { + gpr_log(GPR_ERROR, + "Invalid compression algorithm: '%s' (previously disabled). " + "Ignoring.", md_c_str); calld->compression_algorithm = GRPC_COMPRESS_NONE; } @@ -297,8 +309,17 @@ static void init_channel_elem(grpc_channel_element *elem, grpc_channel *master, char *accept_encoding_str; size_t accept_encoding_str_len; + grpc_compression_options_init(&channeld->compression_options); + channeld->compression_options.enabled_algorithms_bitset = + grpc_channel_args_compression_algorithm_get_states(args); + channeld->default_compression_algorithm = grpc_channel_args_get_compression_algorithm(args); + /* Make sure the default isn't disabled. */ + GPR_ASSERT(grpc_compression_options_is_algorithm_enabled( + &channeld->compression_options, channeld->default_compression_algorithm)); + channeld->compression_options.default_compression_algorithm = + channeld->default_compression_algorithm; channeld->mdstr_request_compression_algorithm_key = grpc_mdstr_from_string(mdctx, GRPC_COMPRESS_REQUEST_ALGORITHM_KEY, 0); @@ -311,6 +332,11 @@ static void init_channel_elem(grpc_channel_element *elem, grpc_channel *master, for (algo_idx = 0; algo_idx < GRPC_COMPRESS_ALGORITHMS_COUNT; ++algo_idx) { char *algorithm_name; + /* skip disabled algorithms */ + if (grpc_compression_options_is_algorithm_enabled( + &channeld->compression_options, algo_idx) == 0) { + continue; + } GPR_ASSERT(grpc_compression_algorithm_name(algo_idx, &algorithm_name) != 0); channeld->mdelem_compression_algorithms[algo_idx] = grpc_mdelem_from_metadata_strings( diff --git a/src/core/compression/algorithm.c b/src/core/compression/algorithm.c index dbf4721d13..6514fcd26f 100644 --- a/src/core/compression/algorithm.c +++ b/src/core/compression/algorithm.c @@ -33,7 +33,9 @@ #include <stdlib.h> #include <string.h> + #include <grpc/compression.h> +#include <grpc/support/useful.h> int grpc_compression_algorithm_parse(const char* name, size_t name_length, grpc_compression_algorithm *algorithm) { @@ -102,3 +104,24 @@ grpc_compression_level grpc_compression_level_for_algorithm( } abort(); } + +void grpc_compression_options_init(grpc_compression_options *opts) { + opts->enabled_algorithms_bitset = (1u << GRPC_COMPRESS_ALGORITHMS_COUNT)-1; + opts->default_compression_algorithm = GRPC_COMPRESS_NONE; +} + +void grpc_compression_options_enable_algorithm( + grpc_compression_options *opts, grpc_compression_algorithm algorithm) { + GPR_BITSET(&opts->enabled_algorithms_bitset, algorithm); +} + +void grpc_compression_options_disable_algorithm( + grpc_compression_options *opts, grpc_compression_algorithm algorithm) { + GPR_BITCLEAR(&opts->enabled_algorithms_bitset, algorithm); +} + +int grpc_compression_options_is_algorithm_enabled( + const grpc_compression_options *opts, + grpc_compression_algorithm algorithm) { + return GPR_BITGET(opts->enabled_algorithms_bitset, algorithm); +} |