Skip to content
Snippets Groups Projects
Commit e634582e authored by Muxi Yan's avatar Muxi Yan
Browse files

Address comments

parent 05833744
No related branches found
No related tags found
No related merge requests found
...@@ -60,15 +60,10 @@ typedef struct channel_data { ...@@ -60,15 +60,10 @@ typedef struct channel_data {
// Find the user agent metadata element in the batch // Find the user agent metadata element in the batch
static bool get_user_agent_mdelem(const grpc_metadata_batch* batch, static bool get_user_agent_mdelem(const grpc_metadata_batch* batch,
grpc_mdelem* md) { grpc_mdelem* md) {
grpc_linked_mdelem* t = batch->list.head; if (batch->idx.named.user_agent != NULL) {
while (t != NULL) { *md = batch->idx.named.user_agent->md;
*md = t->md; return true;
if (grpc_slice_eq(GRPC_MDKEY(*md), GRPC_MDSTR_USER_AGENT)) {
return true;
}
t = t->next;
} }
return false; return false;
} }
...@@ -221,8 +216,7 @@ static bool register_workaround_cronet_compression( ...@@ -221,8 +216,7 @@ static bool register_workaround_cronet_compression(
if (a->value.integer == 0) { if (a->value.integer == 0) {
return true; return true;
} }
grpc_register_workaround(GRPC_WORKAROUND_ID_CRONET_COMPRESSION, grpc_enable_workaround(GRPC_WORKAROUND_ID_CRONET_COMPRESSION);
parse_user_agent);
return grpc_channel_stack_builder_prepend_filter( return grpc_channel_stack_builder_prepend_filter(
builder, &grpc_workaround_cronet_compression_filter, NULL, NULL); builder, &grpc_workaround_cronet_compression_filter, NULL, NULL);
} }
...@@ -231,6 +225,8 @@ void grpc_workaround_cronet_compression_filter_init(void) { ...@@ -231,6 +225,8 @@ void grpc_workaround_cronet_compression_filter_init(void) {
grpc_channel_init_register_stage( grpc_channel_init_register_stage(
GRPC_SERVER_CHANNEL, GRPC_WORKAROUND_PRIORITY_HIGH, GRPC_SERVER_CHANNEL, GRPC_WORKAROUND_PRIORITY_HIGH,
register_workaround_cronet_compression, NULL); register_workaround_cronet_compression, NULL);
grpc_register_workaround(GRPC_WORKAROUND_ID_CRONET_COMPRESSION,
parse_user_agent);
} }
void grpc_workaround_cronet_compression_filter_shutdown(void) {} void grpc_workaround_cronet_compression_filter_shutdown(void) {}
...@@ -34,12 +34,17 @@ ...@@ -34,12 +34,17 @@
#include <grpc/support/alloc.h> #include <grpc/support/alloc.h>
#include <grpc/support/log.h> #include <grpc/support/log.h>
typedef struct {
bool enabled;
user_agent_parser ua_parser;
} workaround_context;
static workaround_context workarounds[GRPC_MAX_WORKAROUND_ID];
static void destroy_user_agent_md(void *user_agent_md) { static void destroy_user_agent_md(void *user_agent_md) {
gpr_free(user_agent_md); gpr_free(user_agent_md);
} }
static user_agent_parser user_agent_parsers[GRPC_MAX_WORKAROUND_ID];
grpc_user_agent_md *grpc_parse_user_agent(grpc_mdelem md) { grpc_user_agent_md *grpc_parse_user_agent(grpc_mdelem md) {
grpc_user_agent_md *user_agent_md = grpc_user_agent_md *user_agent_md =
(grpc_user_agent_md *)grpc_mdelem_get_user_data(md, (grpc_user_agent_md *)grpc_mdelem_get_user_data(md,
...@@ -50,8 +55,8 @@ grpc_user_agent_md *grpc_parse_user_agent(grpc_mdelem md) { ...@@ -50,8 +55,8 @@ grpc_user_agent_md *grpc_parse_user_agent(grpc_mdelem md) {
} }
user_agent_md = gpr_malloc(sizeof(grpc_user_agent_md)); user_agent_md = gpr_malloc(sizeof(grpc_user_agent_md));
for (int i = 0; i < GRPC_MAX_WORKAROUND_ID; i++) { for (int i = 0; i < GRPC_MAX_WORKAROUND_ID; i++) {
if (user_agent_parsers[i]) { if (workarounds[i].enabled && workarounds[i].ua_parser) {
user_agent_md->workaround_active[i] = user_agent_parsers[i](md); user_agent_md->workaround_active[i] = workarounds[i].ua_parser(md);
} }
} }
grpc_mdelem_set_user_data(md, destroy_user_agent_md, (void *)user_agent_md); grpc_mdelem_set_user_data(md, destroy_user_agent_md, (void *)user_agent_md);
...@@ -61,5 +66,10 @@ grpc_user_agent_md *grpc_parse_user_agent(grpc_mdelem md) { ...@@ -61,5 +66,10 @@ grpc_user_agent_md *grpc_parse_user_agent(grpc_mdelem md) {
void grpc_register_workaround(uint32_t id, user_agent_parser parser) { void grpc_register_workaround(uint32_t id, user_agent_parser parser) {
GPR_ASSERT(id < GRPC_MAX_WORKAROUND_ID); GPR_ASSERT(id < GRPC_MAX_WORKAROUND_ID);
user_agent_parsers[id] = parser; workarounds[id].ua_parser = parser;
}
void grpc_enable_workaround(uint32_t id) {
GPR_ASSERT(id < GRPC_MAX_WORKAROUND_ID);
workarounds[id].enabled = true;
} }
...@@ -51,4 +51,6 @@ typedef bool (*user_agent_parser)(grpc_mdelem); ...@@ -51,4 +51,6 @@ typedef bool (*user_agent_parser)(grpc_mdelem);
void grpc_register_workaround(uint32_t id, user_agent_parser parser); void grpc_register_workaround(uint32_t id, user_agent_parser parser);
void grpc_enable_workaround(uint32_t id);
#endif #endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment