You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by to...@apache.org on 2021/05/12 01:46:43 UTC

[apisix] branch master updated: chore: avoid creating lrucache twice (#4220)

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

tokers pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new dd5f93b  chore: avoid creating lrucache twice (#4220)
dd5f93b is described below

commit dd5f93b70b850574282b8f0ad2ecfc4aa5f7dcfb
Author: 罗泽轩 <sp...@gmail.com>
AuthorDate: Wed May 12 09:46:34 2021 +0800

    chore: avoid creating lrucache twice (#4220)
    
    Also refactor part of the code.
    
    Signed-off-by: spacewander <sp...@gmail.com>
---
 apisix/plugins/ext-plugin/init.lua  | 41 +++++++++++++++++--------------------
 t/plugin/ext-plugin/http-req-call.t |  2 ++
 t/plugin/ext-plugin/sanity.t        | 15 ++++++++------
 3 files changed, 30 insertions(+), 28 deletions(-)

diff --git a/apisix/plugins/ext-plugin/init.lua b/apisix/plugins/ext-plugin/init.lua
index bbdaf37..f3315b3 100644
--- a/apisix/plugins/ext-plugin/init.lua
+++ b/apisix/plugins/ext-plugin/init.lua
@@ -575,26 +575,7 @@ local function spawn_proc(cmd)
 end
 
 
-local function setup_runner()
-    local local_conf = core.config.local_conf()
-    local cmd = core.table.try_read_attr(local_conf, "ext-plugin", "cmd")
-    if not cmd then
-        return
-    end
-
-    events_list = events.event_list(
-        "process_runner_exit_event",
-        "runner_exit"
-    )
-
-    -- flush cache when runner exited
-    events.register(create_lrucache, events_list._source, events_list.runner_exit)
-
-    -- note that the runner is run under the same user as the Nginx master
-    if process.type() ~= "privileged agent" then
-        return
-    end
-
+local function setup_runner(cmd)
     local proc = spawn_proc(cmd)
     ngx_timer_at(0, function(premature)
         if premature then
@@ -639,8 +620,24 @@ end
 
 
 function _M.init_worker()
-    create_lrucache()
-    setup_runner()
+    local local_conf = core.config.local_conf()
+    local cmd = core.table.try_read_attr(local_conf, "ext-plugin", "cmd")
+    if not cmd then
+        return
+    end
+
+    events_list = events.event_list(
+        "process_runner_exit_event",
+        "runner_exit"
+    )
+
+    -- flush cache when runner exited
+    events.register(create_lrucache, events_list._source, events_list.runner_exit)
+
+    -- note that the runner is run under the same user as the Nginx master
+    if process.type() == "privileged agent" then
+        setup_runner(cmd)
+    end
 end
 
 
diff --git a/t/plugin/ext-plugin/http-req-call.t b/t/plugin/ext-plugin/http-req-call.t
index 76d56b4..6e6b0e3 100644
--- a/t/plugin/ext-plugin/http-req-call.t
+++ b/t/plugin/ext-plugin/http-req-call.t
@@ -43,9 +43,11 @@ _EOC_
     }
 
     my $unix_socket_path = $ENV{"TEST_NGINX_HTML_DIR"} . "/nginx.sock";
+    my $cmd = $block->ext_plugin_cmd // "['sleep', '5s']";
     my $extra_yaml_config = <<_EOC_;
 ext-plugin:
     path_for_test: $unix_socket_path
+    cmd: $cmd
 _EOC_
 
     $block->set_value("extra_yaml_config", $extra_yaml_config);
diff --git a/t/plugin/ext-plugin/sanity.t b/t/plugin/ext-plugin/sanity.t
index f3646d4..deb3f9d 100644
--- a/t/plugin/ext-plugin/sanity.t
+++ b/t/plugin/ext-plugin/sanity.t
@@ -46,9 +46,11 @@ _EOC_
 
     my $unix_socket_path = $ENV{"TEST_NGINX_HTML_DIR"} . "/nginx.sock";
     my $orig_extra_yaml_config = $block->extra_yaml_config // "";
+    my $cmd = $block->ext_plugin_cmd // "['sleep', '5s']";
     my $extra_yaml_config = <<_EOC_;
 ext-plugin:
     path_for_test: $unix_socket_path
+    cmd: $cmd
 _EOC_
     $extra_yaml_config = $extra_yaml_config . $orig_extra_yaml_config;
 
@@ -114,6 +116,7 @@ hello world
 get conf token: 233
 --- no_error_log
 [error]
+flush conf token lrucache
 --- grep_error_log eval
 qr/(sending|receiving) rpc type: \d data length:/
 --- grep_error_log_out
@@ -179,8 +182,8 @@ failed to connect to the unix socket
 
 
 === TEST 6: spawn runner
---- extra_yaml_config
-    cmd: ["t/plugin/ext-plugin/runner.sh", "3600"]
+--- ext_plugin_cmd
+["t/plugin/ext-plugin/runner.sh", "3600"]
 --- config
     location /t {
         return 200;
@@ -195,8 +198,8 @@ EXPIRE 3600
 
 
 === TEST 7: respawn runner when it exited
---- extra_yaml_config
-    cmd: ["t/plugin/ext-plugin/runner.sh", "0.1"]
+--- ext_plugin_cmd
+["t/plugin/ext-plugin/runner.sh", "0.1"]
 --- config
     location /t {
         content_by_lua_block {
@@ -210,8 +213,8 @@ respawn runner with cmd: ["t\/plugin\/ext-plugin\/runner.sh","0.1"]
 
 
 === TEST 8: flush cache when runner exited
---- extra_yaml_config
-    cmd: ["t/plugin/ext-plugin/runner.sh", "0.4"]
+--- ext_plugin_cmd
+["t/plugin/ext-plugin/runner.sh", "0.4"]
 --- config
     location /t {
         content_by_lua_block {