Merge pull request #20172 from arjunroy/validate_init_md

Better codegen for validate_filtered_metadata.
pull/20209/head
Arjun Roy 5 years ago committed by GitHub
commit e13a652e3e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      src/core/lib/compression/compression.cc
  2. 8
      src/core/lib/compression/compression_internal.h
  3. 127
      src/core/lib/surface/call.cc

@ -127,7 +127,8 @@ void grpc_compression_options_disable_algorithm(
int grpc_compression_options_is_algorithm_enabled( int grpc_compression_options_is_algorithm_enabled(
const grpc_compression_options* opts, const grpc_compression_options* opts,
grpc_compression_algorithm algorithm) { grpc_compression_algorithm algorithm) {
return GPR_BITGET(opts->enabled_algorithms_bitset, algorithm); return grpc_compression_options_is_algorithm_enabled_internal(opts,
algorithm);
} }
grpc_slice grpc_compression_algorithm_slice( grpc_slice grpc_compression_algorithm_slice(

@ -23,6 +23,8 @@
#include <grpc/impl/codegen/compression_types.h> #include <grpc/impl/codegen/compression_types.h>
#include "src/core/lib/gpr/useful.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
@ -85,4 +87,10 @@ int grpc_stream_compression_algorithm_parse(
} }
#endif #endif
inline int grpc_compression_options_is_algorithm_enabled_internal(
const grpc_compression_options* opts,
grpc_compression_algorithm algorithm) {
return GPR_BITGET(opts->enabled_algorithms_bitset, algorithm);
}
#endif /* GRPC_CORE_LIB_COMPRESSION_COMPRESSION_INTERNAL_H */ #endif /* GRPC_CORE_LIB_COMPRESSION_COMPRESSION_INTERNAL_H */

@ -1368,64 +1368,95 @@ static void receiving_stream_ready_in_call_combiner(void* bctlp,
receiving_stream_ready(bctlp, error); receiving_stream_ready(bctlp, error);
} }
static void GPR_ATTRIBUTE_NOINLINE
handle_both_stream_and_msg_compression_set(grpc_call* call) {
char* error_msg = nullptr;
gpr_asprintf(&error_msg,
"Incoming stream has both stream compression (%d) and message "
"compression (%d).",
call->incoming_stream_compression_algorithm,
call->incoming_message_compression_algorithm);
gpr_log(GPR_ERROR, "%s", error_msg);
cancel_with_status(call, GRPC_STATUS_INTERNAL, error_msg);
gpr_free(error_msg);
}
static void GPR_ATTRIBUTE_NOINLINE
handle_error_parsing_compression_algorithm(grpc_call* call) {
char* error_msg = nullptr;
gpr_asprintf(&error_msg,
"Error in incoming message compression (%d) or stream "
"compression (%d).",
call->incoming_stream_compression_algorithm,
call->incoming_message_compression_algorithm);
cancel_with_status(call, GRPC_STATUS_INTERNAL, error_msg);
gpr_free(error_msg);
}
static void GPR_ATTRIBUTE_NOINLINE handle_invalid_compression(
grpc_call* call, grpc_compression_algorithm compression_algorithm) {
char* error_msg = nullptr;
gpr_asprintf(&error_msg, "Invalid compression algorithm value '%d'.",
compression_algorithm);
gpr_log(GPR_ERROR, "%s", error_msg);
cancel_with_status(call, GRPC_STATUS_UNIMPLEMENTED, error_msg);
gpr_free(error_msg);
}
static void GPR_ATTRIBUTE_NOINLINE handle_compression_algorithm_disabled(
grpc_call* call, grpc_compression_algorithm compression_algorithm) {
char* error_msg = nullptr;
const char* algo_name = nullptr;
grpc_compression_algorithm_name(compression_algorithm, &algo_name);
gpr_asprintf(&error_msg, "Compression algorithm '%s' is disabled.",
algo_name);
gpr_log(GPR_ERROR, "%s", error_msg);
cancel_with_status(call, GRPC_STATUS_UNIMPLEMENTED, error_msg);
gpr_free(error_msg);
}
static void GPR_ATTRIBUTE_NOINLINE handle_compression_algorithm_not_accepted(
grpc_call* call, grpc_compression_algorithm compression_algorithm) {
const char* algo_name = nullptr;
grpc_compression_algorithm_name(compression_algorithm, &algo_name);
gpr_log(GPR_ERROR,
"Compression algorithm ('%s') not present in the bitset of "
"accepted encodings ('0x%x')",
algo_name, call->encodings_accepted_by_peer);
}
static void validate_filtered_metadata(batch_control* bctl) { static void validate_filtered_metadata(batch_control* bctl) {
grpc_compression_algorithm compression_algorithm; grpc_compression_algorithm compression_algorithm;
grpc_call* call = bctl->call; grpc_call* call = bctl->call;
if (call->incoming_stream_compression_algorithm != if (GPR_UNLIKELY(call->incoming_stream_compression_algorithm !=
GRPC_STREAM_COMPRESS_NONE && GRPC_STREAM_COMPRESS_NONE &&
call->incoming_message_compression_algorithm != call->incoming_message_compression_algorithm !=
GRPC_MESSAGE_COMPRESS_NONE) { GRPC_MESSAGE_COMPRESS_NONE)) {
char* error_msg = nullptr; handle_both_stream_and_msg_compression_set(call);
gpr_asprintf(&error_msg,
"Incoming stream has both stream compression (%d) and message "
"compression (%d).",
call->incoming_stream_compression_algorithm,
call->incoming_message_compression_algorithm);
gpr_log(GPR_ERROR, "%s", error_msg);
cancel_with_status(call, GRPC_STATUS_INTERNAL, error_msg);
gpr_free(error_msg);
} else if ( } else if (
grpc_compression_algorithm_from_message_stream_compression_algorithm( GPR_UNLIKELY(
&compression_algorithm, call->incoming_message_compression_algorithm, grpc_compression_algorithm_from_message_stream_compression_algorithm(
call->incoming_stream_compression_algorithm) == 0) { &compression_algorithm,
char* error_msg = nullptr; call->incoming_message_compression_algorithm,
gpr_asprintf(&error_msg, call->incoming_stream_compression_algorithm) == 0)) {
"Error in incoming message compression (%d) or stream " handle_error_parsing_compression_algorithm(call);
"compression (%d).",
call->incoming_stream_compression_algorithm,
call->incoming_message_compression_algorithm);
cancel_with_status(call, GRPC_STATUS_INTERNAL, error_msg);
gpr_free(error_msg);
} else { } else {
char* error_msg = nullptr;
const grpc_compression_options compression_options = const grpc_compression_options compression_options =
grpc_channel_compression_options(call->channel); grpc_channel_compression_options(call->channel);
if (compression_algorithm >= GRPC_COMPRESS_ALGORITHMS_COUNT) { if (GPR_UNLIKELY(compression_algorithm >= GRPC_COMPRESS_ALGORITHMS_COUNT)) {
gpr_asprintf(&error_msg, "Invalid compression algorithm value '%d'.", handle_invalid_compression(call, compression_algorithm);
compression_algorithm); } else if (GPR_UNLIKELY(
gpr_log(GPR_ERROR, "%s", error_msg); grpc_compression_options_is_algorithm_enabled_internal(
cancel_with_status(call, GRPC_STATUS_UNIMPLEMENTED, error_msg); &compression_options, compression_algorithm) == 0)) {
} else if (grpc_compression_options_is_algorithm_enabled(
&compression_options, compression_algorithm) == 0) {
/* check if algorithm is supported by current channel config */ /* check if algorithm is supported by current channel config */
const char* algo_name = nullptr; handle_compression_algorithm_disabled(call, compression_algorithm);
grpc_compression_algorithm_name(compression_algorithm, &algo_name);
gpr_asprintf(&error_msg, "Compression algorithm '%s' is disabled.",
algo_name);
gpr_log(GPR_ERROR, "%s", error_msg);
cancel_with_status(call, GRPC_STATUS_UNIMPLEMENTED, error_msg);
} }
gpr_free(error_msg); /* GRPC_COMPRESS_NONE is always set. */
GPR_DEBUG_ASSERT(call->encodings_accepted_by_peer != 0);
GPR_ASSERT(call->encodings_accepted_by_peer != 0); if (GPR_UNLIKELY(!GPR_BITGET(call->encodings_accepted_by_peer,
if (!GPR_BITGET(call->encodings_accepted_by_peer, compression_algorithm)) { compression_algorithm))) {
if (GRPC_TRACE_FLAG_ENABLED(grpc_compression_trace)) { if (GRPC_TRACE_FLAG_ENABLED(grpc_compression_trace)) {
const char* algo_name = nullptr; handle_compression_algorithm_not_accepted(call, compression_algorithm);
grpc_compression_algorithm_name(compression_algorithm, &algo_name);
gpr_log(GPR_ERROR,
"Compression algorithm ('%s') not present in the bitset of "
"accepted encodings ('0x%x')",
algo_name, call->encodings_accepted_by_peer);
} }
} }
} }

Loading…
Cancel
Save