Improve performance.
This commit is contained in:
parent
1d4c7ec65c
commit
5b0f7d7d6e
2 changed files with 19 additions and 14 deletions
|
@ -1,15 +1,14 @@
|
||||||
|
local ngx = ngx
|
||||||
|
local tonumber = tonumber
|
||||||
|
local assert = assert
|
||||||
|
local string = string
|
||||||
|
local tostring = tostring
|
||||||
local socket = ngx.socket.tcp
|
local socket = ngx.socket.tcp
|
||||||
local cjson = require("cjson.safe")
|
local cjson = require("cjson.safe")
|
||||||
local assert = assert
|
|
||||||
local new_tab = require "table.new"
|
local new_tab = require "table.new"
|
||||||
local clear_tab = require "table.clear"
|
local clear_tab = require "table.clear"
|
||||||
local clone_tab = require "table.clone"
|
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
|
-- if an Nginx worker processes more than (MAX_BATCH_SIZE/FLUSH_INTERVAL) RPS
|
||||||
-- then it will start dropping metrics
|
-- then it will start dropping metrics
|
||||||
|
@ -17,6 +16,7 @@ local MAX_BATCH_SIZE = 10000
|
||||||
local FLUSH_INTERVAL = 1 -- second
|
local FLUSH_INTERVAL = 1 -- second
|
||||||
|
|
||||||
local metrics_batch = new_tab(MAX_BATCH_SIZE, 0)
|
local metrics_batch = new_tab(MAX_BATCH_SIZE, 0)
|
||||||
|
local metrics_count = 0
|
||||||
|
|
||||||
local _M = {}
|
local _M = {}
|
||||||
|
|
||||||
|
@ -53,12 +53,13 @@ local function flush(premature)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
if #metrics_batch == 0 then
|
if metrics_count == 0 then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local current_metrics_batch = clone_tab(metrics_batch)
|
local current_metrics_batch = clone_tab(metrics_batch)
|
||||||
clear_tab(metrics_batch)
|
clear_tab(metrics_batch)
|
||||||
|
metrics_count = 0
|
||||||
|
|
||||||
local payload, err = cjson.encode(current_metrics_batch)
|
local payload, err = cjson.encode(current_metrics_batch)
|
||||||
if not payload then
|
if not payload then
|
||||||
|
@ -77,13 +78,13 @@ function _M.init_worker()
|
||||||
end
|
end
|
||||||
|
|
||||||
function _M.call()
|
function _M.call()
|
||||||
local metrics_size = nkeys(metrics_batch)
|
if metrics_count >= MAX_BATCH_SIZE then
|
||||||
if metrics_size >= MAX_BATCH_SIZE then
|
|
||||||
ngx.log(ngx.WARN, "omitting metrics for the request, current batch is full")
|
ngx.log(ngx.WARN, "omitting metrics for the request, current batch is full")
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
metrics_batch[metrics_size + 1] = metrics()
|
metrics_count = metrics_count + 1
|
||||||
|
metrics_batch[metrics_count] = metrics()
|
||||||
end
|
end
|
||||||
|
|
||||||
setmetatable(_M, {__index = {
|
setmetatable(_M, {__index = {
|
||||||
|
|
|
@ -3,16 +3,14 @@ local ngx = ngx
|
||||||
local pairs = pairs
|
local pairs = pairs
|
||||||
local ipairs = ipairs
|
local ipairs = ipairs
|
||||||
local string_format = string.format
|
local string_format = string.format
|
||||||
local new_tab = require "table.new"
|
|
||||||
local ngx_log = ngx.log
|
local ngx_log = ngx.log
|
||||||
local INFO = ngx.INFO
|
local INFO = ngx.INFO
|
||||||
local ERR = ngx.ERR
|
local ERR = ngx.ERR
|
||||||
local pcall = pcall
|
local pcall = pcall
|
||||||
|
|
||||||
local _M = {}
|
local _M = {}
|
||||||
local MAX_NUMBER_OF_PLUGINS = 10000
|
local MAX_NUMBER_OF_PLUGINS = 20
|
||||||
-- TODO: is this good for a dictionary?
|
local plugins = {}
|
||||||
local plugins = new_tab(MAX_NUMBER_OF_PLUGINS, 0)
|
|
||||||
|
|
||||||
local function load_plugin(name)
|
local function load_plugin(name)
|
||||||
local path = string_format("plugins.%s.main", name)
|
local path = string_format("plugins.%s.main", name)
|
||||||
|
@ -27,8 +25,14 @@ local function load_plugin(name)
|
||||||
end
|
end
|
||||||
|
|
||||||
function _M.init(names)
|
function _M.init(names)
|
||||||
|
local count = 0
|
||||||
for _, name in ipairs(names) do
|
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)
|
load_plugin(name)
|
||||||
|
count = count + 1 -- ignore loading failure, just count the total
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue