From 002daa54b0d115adf73e546ebc674b1034abafef Mon Sep 17 00:00:00 2001 From: Robert Paprocki Date: Mon, 20 Mar 2017 09:52:25 -0700 Subject: [PATCH] optimization: dont calculate msg length more than necessary --- lib/resty/logger/socket.lua | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/resty/logger/socket.lua b/lib/resty/logger/socket.lua index c5b897e..0ef8bef 100644 --- a/lib/resty/logger/socket.lua +++ b/lib/resty/logger/socket.lua @@ -361,11 +361,11 @@ local function _flush_buffer() end end -local function _write_buffer(msg) +local function _write_buffer(msg, len) log_buffer_index = log_buffer_index + 1 log_buffer_data[log_buffer_index] = msg - buffer_size = buffer_size + #msg + buffer_size = buffer_size + len return buffer_size @@ -505,28 +505,28 @@ function _M.log(msg) msg = tostring(msg) end + local msg_len = #msg + if (debug) then ngx.update_time() - ngx_log(DEBUG, ngx.now(), ":log message length: " .. #msg) + ngx_log(DEBUG, ngx.now(), ":log message length: " .. msg_len) end - local msg_len = #msg - -- response of "_flush_buffer" is not checked, because it writes -- error buffer if (is_exiting()) then exiting = true - _write_buffer(msg) + _write_buffer(msg, msg_len) _flush_buffer() if (debug) then ngx_log(DEBUG, "Nginx worker is exiting") end bytes = 0 elseif (msg_len + buffer_size < flush_limit) then - _write_buffer(msg) + _write_buffer(msg, msg_len) bytes = msg_len elseif (msg_len + buffer_size <= drop_limit) then - _write_buffer(msg) + _write_buffer(msg, msg_len) _flush_buffer() bytes = msg_len else