You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by sp...@apache.org on 2022/03/01 03:30:57 UTC

[apisix] branch master updated: chore: reduce shdict if we disable the plugin (#6463)

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

spacewander 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 48398d3  chore: reduce shdict if we disable the plugin (#6463)
48398d3 is described below

commit 48398d3f1501fe425b08673609605ea9d8039f54
Author: 罗泽轩 <sp...@gmail.com>
AuthorDate: Tue Mar 1 11:30:49 2022 +0800

    chore: reduce shdict if we disable the plugin (#6463)
---
 apisix/cli/ngx_tpl.lua      | 42 +++++++++++++++++++++++++++++++++++-------
 apisix/cli/ops.lua          |  8 +++++++-
 apisix/plugin.lua           |  6 +++---
 t/cli/test_http_config.sh   | 26 ++++++++++++++++++++++++++
 t/cli/test_main.sh          |  5 -----
 t/cli/test_stream_config.sh | 34 ++++++++++++++++++++++++++++++++++
 6 files changed, 105 insertions(+), 16 deletions(-)

diff --git a/apisix/cli/ngx_tpl.lua b/apisix/cli/ngx_tpl.lua
index 1064253..9ddcaaa 100644
--- a/apisix/cli/ngx_tpl.lua
+++ b/apisix/cli/ngx_tpl.lua
@@ -74,9 +74,12 @@ stream {
     {% end %}
 
     lua_shared_dict lrucache-lock-stream {* stream.lua_shared_dict["lrucache-lock-stream"] *};
-    lua_shared_dict plugin-limit-conn-stream {* stream.lua_shared_dict["plugin-limit-conn-stream"] *};
     lua_shared_dict etcd-cluster-health-check-stream {* stream.lua_shared_dict["etcd-cluster-health-check-stream"] *};
 
+    {% if enabled_stream_plugins["limit-conn"] then %}
+    lua_shared_dict plugin-limit-conn-stream {* stream.lua_shared_dict["plugin-limit-conn-stream"] *};
+    {% end %}
+
     resolver {% for _, dns_addr in ipairs(dns_resolver or {}) do %} {*dns_addr*} {% end %} {% if dns_resolver_valid then %} valid={*dns_resolver_valid*}{% end %} ipv6={% if enable_ipv6 then %}on{% else %}off{% end %};
     resolver_timeout {*resolver_timeout*};
 
@@ -179,33 +182,58 @@ http {
     {% end %}
 
     lua_shared_dict internal-status {* http.lua_shared_dict["internal-status"] *};
-    lua_shared_dict plugin-limit-req {* http.lua_shared_dict["plugin-limit-req"] *};
-    lua_shared_dict plugin-limit-count {* http.lua_shared_dict["plugin-limit-count"] *};
-    lua_shared_dict prometheus-metrics {* http.lua_shared_dict["prometheus-metrics"] *};
-    lua_shared_dict plugin-limit-conn {* http.lua_shared_dict["plugin-limit-conn"] *};
     lua_shared_dict upstream-healthcheck {* http.lua_shared_dict["upstream-healthcheck"] *};
     lua_shared_dict worker-events {* http.lua_shared_dict["worker-events"] *};
     lua_shared_dict lrucache-lock {* http.lua_shared_dict["lrucache-lock"] *};
     lua_shared_dict balancer-ewma {* http.lua_shared_dict["balancer-ewma"] *};
     lua_shared_dict balancer-ewma-locks {* http.lua_shared_dict["balancer-ewma-locks"] *};
     lua_shared_dict balancer-ewma-last-touched-at {* http.lua_shared_dict["balancer-ewma-last-touched-at"] *};
+    lua_shared_dict etcd-cluster-health-check {* http.lua_shared_dict["etcd-cluster-health-check"] *}; # etcd health check
+
+    {% if enabled_plugins["limit-conn"] then %}
+    lua_shared_dict plugin-limit-conn {* http.lua_shared_dict["plugin-limit-conn"] *};
+    {% end %}
+
+    {% if enabled_plugins["limit-req"] then %}
+    lua_shared_dict plugin-limit-req {* http.lua_shared_dict["plugin-limit-req"] *};
+    {% end %}
+
+    {% if enabled_plugins["limit-count"] then %}
+    lua_shared_dict plugin-limit-count {* http.lua_shared_dict["plugin-limit-count"] *};
     lua_shared_dict plugin-limit-count-redis-cluster-slot-lock {* http.lua_shared_dict["plugin-limit-count-redis-cluster-slot-lock"] *};
+    {% end %}
+
+    {% if enabled_plugins["prometheus"] then %}
+    lua_shared_dict prometheus-metrics {* http.lua_shared_dict["prometheus-metrics"] *};
+    {% end %}
+
+    {% if enabled_plugins["skywalking"] then %}
     lua_shared_dict tracing_buffer {* http.lua_shared_dict.tracing_buffer *}; # plugin: skywalking
+    {% end %}
+
+    {% if enabled_plugins["api-breaker"] then %}
     lua_shared_dict plugin-api-breaker {* http.lua_shared_dict["plugin-api-breaker"] *};
-    lua_shared_dict etcd-cluster-health-check {* http.lua_shared_dict["etcd-cluster-health-check"] *}; # etcd health check
+    {% end %}
 
+    {% if enabled_plugins["openid-connect"] or enabled_plugins["authz-keycloak"] then %}
     # for openid-connect and authz-keycloak plugin
     lua_shared_dict discovery {* http.lua_shared_dict["discovery"] *}; # cache for discovery metadata documents
+    {% end %}
 
+    {% if enabled_plugins["openid-connect"] then %}
     # for openid-connect plugin
     lua_shared_dict jwks {* http.lua_shared_dict["jwks"] *}; # cache for JWKs
     lua_shared_dict introspection {* http.lua_shared_dict["introspection"] *}; # cache for JWT verification results
+    {% end %}
 
+    {% if enabled_plugins["authz-keycloak"] then %}
     # for authz-keycloak
     lua_shared_dict access-tokens {* http.lua_shared_dict["access-tokens"] *}; # cache for service account access tokens
+    {% end %}
 
-    # for ext-plugin
+    {% if enabled_plugins["ext-plugin-pre-req"] or enabled_plugins["ext-plugin-post-req"] then %}
     lua_shared_dict ext-plugin {* http.lua_shared_dict["ext-plugin"] *}; # cache for ext-plugin
+    {% end %}
 
     # for custom shared dict
     {% if http.custom_lua_shared_dict then %}
diff --git a/apisix/cli/ops.lua b/apisix/cli/ops.lua
index b9cc58a..219afc4 100644
--- a/apisix/cli/ops.lua
+++ b/apisix/cli/ops.lua
@@ -252,10 +252,15 @@ Please modify "admin_key" in conf/config.yaml .
     end
 
     local enabled_plugins = {}
-    for i, name in ipairs(yaml_conf.plugins) do
+    for i, name in ipairs(yaml_conf.plugins or {}) do
         enabled_plugins[name] = true
     end
 
+    local enabled_stream_plugins = {}
+    for i, name in ipairs(yaml_conf.stream_plugins or {}) do
+        enabled_stream_plugins[name] = true
+    end
+
     if enabled_plugins["proxy-cache"] and not yaml_conf.apisix.proxy_cache then
         util.die("missing apisix.proxy_cache for plugin proxy-cache\n")
     end
@@ -519,6 +524,7 @@ Please modify "admin_key" in conf/config.yaml .
         use_apisix_openresty = use_apisix_openresty,
         error_log = {level = "warn"},
         enabled_plugins = enabled_plugins,
+        enabled_stream_plugins = enabled_stream_plugins,
         dubbo_upstream_multiplex_count = dubbo_upstream_multiplex_count,
         tcp_enable_ssl = tcp_enable_ssl,
         admin_server_addr = admin_server_addr,
diff --git a/apisix/plugin.lua b/apisix/plugin.lua
index 729884f..0d9b707 100644
--- a/apisix/plugin.lua
+++ b/apisix/plugin.lua
@@ -551,13 +551,13 @@ end
 
 
 function _M.init_worker()
+    _M.load()
+
     -- some plugins need to be initialized in init* phases
-    if ngx.config.subsystem == "http" then
+    if ngx.config.subsystem == "http" and local_plugins_hash["prometheus"] then
         require("apisix.plugins.prometheus.exporter").init()
     end
 
-    _M.load()
-
     if local_conf and not local_conf.apisix.enable_admin then
         init_plugins_syncer()
     end
diff --git a/t/cli/test_http_config.sh b/t/cli/test_http_config.sh
index 6e87741..20837f6 100755
--- a/t/cli/test_http_config.sh
+++ b/t/cli/test_http_config.sh
@@ -54,3 +54,29 @@ if ! grep "lua_shared_dict my_dict 1m;" conf/nginx.conf > /dev/null; then
 fi
 
 echo "passed: define custom shdict in the old way"
+
+echo "
+plugins:
+    - ip-restriction
+" > conf/config.yaml
+
+make init
+
+if grep "plugin-limit-conn" conf/nginx.conf > /dev/null; then
+    echo "failed: enable shdict on demand"
+    exit 1
+fi
+
+echo "
+plugins:
+    - limit-conn
+" > conf/config.yaml
+
+make init
+
+if ! grep "plugin-limit-conn" conf/nginx.conf > /dev/null; then
+    echo "failed: enable shdict on demand"
+    exit 1
+fi
+
+echo "passed: enable shdict on demand"
diff --git a/t/cli/test_main.sh b/t/cli/test_main.sh
index f9c6a38..88d01cc 100755
--- a/t/cli/test_main.sh
+++ b/t/cli/test_main.sh
@@ -932,11 +932,6 @@ if ! grep "plugin-limit-count-redis-cluster-slot-lock 2m;" conf/nginx.conf > /de
     exit 1
 fi
 
-if ! grep "tracing_buffer 20m;" conf/nginx.conf > /dev/null; then
-    echo "failed: 'tracing_buffer 20m;' not in nginx.conf"
-    exit 1
-fi
-
 if ! grep "plugin-api-breaker 20m;" conf/nginx.conf > /dev/null; then
     echo "failed: 'plugin-api-breaker 20m;' not in nginx.conf"
     exit 1
diff --git a/t/cli/test_stream_config.sh b/t/cli/test_stream_config.sh
index 71e2f33..5a15ae1 100755
--- a/t/cli/test_stream_config.sh
+++ b/t/cli/test_stream_config.sh
@@ -89,3 +89,37 @@ if ! grep "t/certs/mtls_ca.crt;" conf/nginx.conf > /dev/null; then
 fi
 
 echo "passed: set trust certificate"
+
+echo "
+apisix:
+    stream_proxy:
+        tcp:
+            - addr: 9100
+stream_plugins:
+    - ip-restriction
+" > conf/config.yaml
+
+make init
+
+if grep "plugin-limit-conn-stream" conf/nginx.conf > /dev/null; then
+    echo "failed: enable shdict on demand"
+    exit 1
+fi
+
+echo "
+apisix:
+    stream_proxy:
+        tcp:
+            - addr: 9100
+stream_plugins:
+    - limit-conn
+" > conf/config.yaml
+
+make init
+
+if ! grep "plugin-limit-conn-stream" conf/nginx.conf > /dev/null; then
+    echo "failed: enable shdict on demand"
+    exit 1
+fi
+
+echo "passed: enable shdict on demand"