Skip to content

Commit

Permalink
Merge pull request #5665 from agile6v/performance
Browse files Browse the repository at this point in the history
Fix: improve performance
  • Loading branch information
k8s-ci-robot committed Jun 11, 2020
2 parents c223b02 + 5b0f7d7 commit 589187c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
21 changes: 11 additions & 10 deletions rootfs/etc/nginx/lua/monitor.lua
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
local ngx = ngx
local tonumber = tonumber
local assert = assert
local string = string
local tostring = tostring
local socket = ngx.socket.tcp
local cjson = require("cjson.safe")
local assert = assert
local new_tab = require "table.new"
local clear_tab = require "table.clear"
local clone_tab = require "table.clone"
local nkeys = require "table.nkeys"

local ngx = ngx
local tonumber = tonumber
local string = string
local tostring = tostring

-- if an Nginx worker processes more than (MAX_BATCH_SIZE/FLUSH_INTERVAL) RPS
-- then it will start dropping metrics
local MAX_BATCH_SIZE = 10000
local FLUSH_INTERVAL = 1 -- second

local metrics_batch = new_tab(MAX_BATCH_SIZE, 0)
local metrics_count = 0

local _M = {}

Expand Down Expand Up @@ -53,12 +53,13 @@ local function flush(premature)
return
end

if #metrics_batch == 0 then
if metrics_count == 0 then
return
end

local current_metrics_batch = clone_tab(metrics_batch)
clear_tab(metrics_batch)
metrics_count = 0

local payload, err = cjson.encode(current_metrics_batch)
if not payload then
Expand All @@ -77,13 +78,13 @@ function _M.init_worker()
end

function _M.call()
local metrics_size = nkeys(metrics_batch)
if metrics_size >= MAX_BATCH_SIZE then
if metrics_count >= MAX_BATCH_SIZE then
ngx.log(ngx.WARN, "omitting metrics for the request, current batch is full")
return
end

metrics_batch[metrics_size + 1] = metrics()
metrics_count = metrics_count + 1
metrics_batch[metrics_count] = metrics()
end

setmetatable(_M, {__index = {
Expand Down
12 changes: 8 additions & 4 deletions rootfs/etc/nginx/lua/plugins.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ local ngx = ngx
local pairs = pairs
local ipairs = ipairs
local string_format = string.format
local new_tab = require "table.new"
local ngx_log = ngx.log
local INFO = ngx.INFO
local ERR = ngx.ERR
local pcall = pcall

local _M = {}
local MAX_NUMBER_OF_PLUGINS = 10000
-- TODO: is this good for a dictionary?
local plugins = new_tab(MAX_NUMBER_OF_PLUGINS, 0)
local MAX_NUMBER_OF_PLUGINS = 20
local plugins = {}

local function load_plugin(name)
local path = string_format("plugins.%s.main", name)
Expand All @@ -27,8 +25,14 @@ local function load_plugin(name)
end

function _M.init(names)
local count = 0
for _, name in ipairs(names) do
if count >= MAX_NUMBER_OF_PLUGINS then
ngx_log(ERR, "the total number of plugins exceed the maximum number: ", MAX_NUMBER_OF_PLUGINS)
break
end
load_plugin(name)
count = count + 1 -- ignore loading failure, just count the total
end
end

Expand Down

0 comments on commit 589187c

Please sign in to comment.