From 384d8ffaaa973c85d1dc78e3e8e38c72715968e0 Mon Sep 17 00:00:00 2001
From: Muxi Yan <mxyan@google.com>
Date: Fri, 28 Apr 2017 12:15:14 -0700
Subject: [PATCH] Use mdelem's user data caching

---
 .../workaround_cronet_compression_filter.c           |  2 --
 src/core/ext/filters/workarounds/workaround_utils.c  | 12 +++++++++---
 2 files changed, 9 insertions(+), 5 deletions(-)

diff --git a/src/core/ext/filters/workarounds/workaround_cronet_compression_filter.c b/src/core/ext/filters/workarounds/workaround_cronet_compression_filter.c
index 04bd607fa6..57f0f0ae41 100644
--- a/src/core/ext/filters/workarounds/workaround_cronet_compression_filter.c
+++ b/src/core/ext/filters/workarounds/workaround_cronet_compression_filter.c
@@ -87,8 +87,6 @@ static void recv_initial_metadata_ready(grpc_exec_ctx* exec_ctx,
               ->workaround_active[GRPC_WORKAROUND_ID_CRONET_COMPRESSION]) {
         calld->workaround_active = true;
       }
-      // Remove with caching
-      gpr_free(user_agent_md);
     }
   }
 
diff --git a/src/core/ext/filters/workarounds/workaround_utils.c b/src/core/ext/filters/workarounds/workaround_utils.c
index 14ed84599c..fef81395b0 100644
--- a/src/core/ext/filters/workarounds/workaround_utils.c
+++ b/src/core/ext/filters/workarounds/workaround_utils.c
@@ -34,19 +34,25 @@
 #include <grpc/support/alloc.h>
 #include <grpc/support/log.h>
 
+static void destroy_user_agent_md(void *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 *user_agent_md;
-
-  // USE THE CACHE WHEN ABLE
+  grpc_user_agent_md *user_agent_md = (grpc_user_agent_md*)grpc_mdelem_get_user_data(md, destroy_user_agent_md);
 
+  if (NULL != user_agent_md) {
+    return user_agent_md;
+  }
   user_agent_md = gpr_malloc(sizeof(grpc_user_agent_md));
   for (int i = 0; i < GRPC_MAX_WORKAROUND_ID; i++) {
     if (user_agent_parsers[i]) {
       user_agent_md->workaround_active[i] = user_agent_parsers[i](md);
     }
   }
+  grpc_mdelem_set_user_data(md, destroy_user_agent_md, (void *)user_agent_md);
 
   return user_agent_md;
 }
-- 
GitLab