You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by zw...@apache.org on 2021/02/22 16:55:22 UTC

[trafficserver] 02/02: Select lua context per thread (#7465)

This is an automated email from the ASF dual-hosted git repository.

zwoop pushed a commit to branch 9.1.x
in repository https://gitbox.apache.org/repos/asf/trafficserver.git

commit 9b7ea37bc18f01bfeabc86ff5bf1cf8b52c655fc
Author: Kit Chan <ki...@apache.org>
AuthorDate: Tue Feb 16 14:37:35 2021 -0800

    Select lua context per thread (#7465)
    
    * Select lua context per thread
    
    * fix typo
    
    * Fix typo
    
    (cherry picked from commit cd33010ff3926d92f00c5f44606acf10062ce4e1)
---
 plugins/lua/ts_lua.c | 27 +++++++++++++++++++++------
 1 file changed, 21 insertions(+), 6 deletions(-)

diff --git a/plugins/lua/ts_lua.c b/plugins/lua/ts_lua.c
index 0e7468f..e39a290 100644
--- a/plugins/lua/ts_lua.c
+++ b/plugins/lua/ts_lua.c
@@ -21,6 +21,7 @@
 #include <pthread.h>
 #include <getopt.h>
 #include <inttypes.h>
+#include <pthread.h>
 
 #include "ts_lua_util.h"
 
@@ -40,6 +41,9 @@ static uint64_t ts_lua_g_http_next_id = 0;
 static ts_lua_main_ctx *ts_lua_main_ctx_array   = NULL;
 static ts_lua_main_ctx *ts_lua_g_main_ctx_array = NULL;
 
+static pthread_key_t lua_g_state_key;
+static pthread_key_t lua_state_key;
+
 // records.config entry injected by plugin
 static char const *const ts_lua_mgmt_state_str   = "proxy.config.plugin.lua.max_states";
 static char const *const ts_lua_mgmt_state_regex = "^[1-9][0-9]*$";
@@ -307,6 +311,8 @@ TSRemapInit(TSRemapInterface *api_info, char *errbuf, int errbuf_size)
   if (NULL == ts_lua_main_ctx_array) {
     ts_lua_main_ctx_array = create_lua_vms();
     if (NULL != ts_lua_main_ctx_array) {
+      pthread_key_create(&lua_state_key, NULL);
+
       TSCont const lcontp = TSContCreate(lifecycleHandler, TSMutexCreate());
       TSContDataSet(lcontp, ts_lua_main_ctx_array);
       TSLifecycleHookAdd(TS_LIFECYCLE_MSG_HOOK, lcontp);
@@ -475,9 +481,13 @@ ts_lua_remap_plugin_init(void *ih, TSHttpTxn rh, TSRemapRequestInfo *rri)
 
   int remap     = (rri == NULL ? 0 : 1);
   instance_conf = (ts_lua_instance_conf *)ih;
-  req_id        = __sync_fetch_and_add(&ts_lua_http_next_id, 1);
 
-  main_ctx = &ts_lua_main_ctx_array[req_id % instance_conf->states];
+  main_ctx = pthread_getspecific(lua_state_key);
+  if (main_ctx == NULL) {
+    req_id   = __sync_fetch_and_add(&ts_lua_http_next_id, 1);
+    main_ctx = &ts_lua_main_ctx_array[req_id % instance_conf->states];
+    pthread_setspecific(lua_state_key, main_ctx);
+  }
 
   TSMutexLock(main_ctx->mutexp);
 
@@ -577,11 +587,14 @@ globalHookHandler(TSCont contp, TSEvent event ATS_UNUSED, void *edata)
 
   ts_lua_instance_conf *conf = (ts_lua_instance_conf *)TSContDataGet(contp);
 
-  req_id = __sync_fetch_and_add(&ts_lua_g_http_next_id, 1);
-
-  main_ctx = &ts_lua_g_main_ctx_array[req_id % conf->states];
+  main_ctx = pthread_getspecific(lua_g_state_key);
+  if (main_ctx == NULL) {
+    req_id = __sync_fetch_and_add(&ts_lua_g_http_next_id, 1);
+    TSDebug(TS_LUA_DEBUG_TAG, "[%s] req_id: %" PRId64, __FUNCTION__, req_id);
+    main_ctx = &ts_lua_g_main_ctx_array[req_id % conf->states];
+    pthread_setspecific(lua_g_state_key, main_ctx);
+  }
 
-  TSDebug(TS_LUA_DEBUG_TAG, "[%s] req_id: %" PRId64, __FUNCTION__, req_id);
   TSMutexLock(main_ctx->mutexp);
 
   http_ctx           = ts_lua_create_http_ctx(main_ctx, conf);
@@ -740,6 +753,8 @@ TSPluginInit(int argc, const char *argv[])
   if (NULL == ts_lua_g_main_ctx_array) {
     ts_lua_g_main_ctx_array = create_lua_vms();
     if (NULL != ts_lua_g_main_ctx_array) {
+      pthread_key_create(&lua_g_state_key, NULL);
+
       TSCont const contp = TSContCreate(lifecycleHandler, TSMutexCreate());
       TSContDataSet(contp, ts_lua_g_main_ctx_array);
       TSLifecycleHookAdd(TS_LIFECYCLE_MSG_HOOK, contp);