Skip to content

Commit

Permalink
Merge pull request #58 from TencentBlueKing/master
Browse files Browse the repository at this point in the history
backport 20231018
  • Loading branch information
wklken authored Oct 18, 2023
2 parents 24ef5da + a80bfe9 commit c8f4dda
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ RUN curl https://raw.githubusercontent.com/apache/apisix/${APISIX_VERSION}/utils
RUN luarocks install multipart --tree=/usr/local/apisix/deps && \
rm -rf /root/.cache/luarocks/ || echo "no /root/.cache/luarocks to clean"

ADD ./build/config-watcher ./src/build/bin/apisix-start.sh ./src/build/bin/config-watcher-start.sh /data/bkgateway/bin/
ADD ./build/config-watcher ./src/build/bin/apisix-start.sh ./src/build/bin/config-watcher-start.sh ./src/build/bin/sentrylogs-daemonize.sh /data/bkgateway/bin/
ADD ./src/apisix/plugins/ /usr/local/apisix/apisix/plugins/
# FIXME: remove the patch if upgrade to >=3.4.x, while the patch is only for 3.2.x ---
ADD ./src/build/patches /usr/local/apisix/patches
Expand Down
3 changes: 3 additions & 0 deletions src/build/bin/apisix-start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ if [ x"${BK_APIGW_NGINX_ERROR_LOG_SENTRY_DSN}" != x"" ]
then
echo "start sentrylogs to ship nginx error log to sentry"
SENTRY_DSN="${BK_APIGW_NGINX_ERROR_LOG_SENTRY_DSN}" NGINX_ERROR_PATH="/usr/local/apisix/logs/error.log" sentrylogs --daemonize

# start a daemon to check sentrylogs, the sentrylogs will only process the `tail` records, so it's safe to restart it
sh /data/bkgateway/bin/sentrylogs-daemonize.sh &
fi

echo "start config-watcher for ${apisixDebugConfigPath}(note: will wait until the container quit)"
Expand Down
17 changes: 17 additions & 0 deletions src/build/bin/sentrylogs-daemonize.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

while true
do
# Check if ${BK_APIGW_NGINX_ERROR_LOG_SENTRY_DSN} is not empty
if [[ -n "${BK_APIGW_NGINX_ERROR_LOG_SENTRY_DSN}" ]]; then
# Check if sentrylogs process is running
if pgrep -x "sentrylogs" > /dev/null
then
echo "sentrylogs is running"
else
echo "sentrylogs is not running, restarting..."
SENTRY_DSN="${BK_APIGW_NGINX_ERROR_LOG_SENTRY_DSN}" NGINX_ERROR_PATH="/usr/local/apisix/logs/error.log" sentrylogs --daemonize
fi
fi
sleep 60
done
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
diff --git a/apisix/http/router/radixtree_uri_with_parameter.lua b/apisix/http/router/radixtree_uri_with_parameter.lua
index 4bf7f3eb..cd9da089 100644
--- a/apisix/http/router/radixtree_uri_with_parameter.lua
+++ b/apisix/http/router/radixtree_uri_with_parameter.lua
@@ -18,8 +18,12 @@ local require = require
local core = require("apisix.core")
local base_router = require("apisix.http.route")
local get_services = require("apisix.http.service").services
+local ngx_time = ngx.time
+local random = math.random
local cached_router_version
local cached_service_version
+local cached_timestamp = 0
+local random_interval = 0


local _M = {}
@@ -34,10 +38,22 @@ function _M.match(api_ctx)
if not cached_router_version or cached_router_version ~= user_routes.conf_version
or not cached_service_version or cached_service_version ~= service_version
then
+ if ngx_time() - cached_timestamp >= random_interval
+ then
+
uri_router = base_router.create_radixtree_uri_router(user_routes.values,
uri_routes, true)
cached_router_version = user_routes.conf_version
cached_service_version = service_version
+
+ cached_timestamp = ngx_time()
+ -- rebuild the tree every {random_interval} seconds, at least 5 seconds
+ -- we do this to avoid:
+ -- 1. all the workers rebuild the tree at the same time
+ -- 2. the workers keep rebuilding if the `/routes` is updating frequently
+ random_interval = random(5, 15)
+ core.log.info("rebuild the tree")
+ end
end

if not uri_router then

0 comments on commit c8f4dda

Please sign in to comment.