Skip to content

Commit 589b3f8

Browse files
committed
Implement periodic flushing
1 parent 64ca3d3 commit 589b3f8

File tree

3 files changed

+93
-1
lines changed

3 files changed

+93
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ Available user configurations are listed as follows:
127127

128128
Keepalive pool size used by sock:keepalive. Default to 10.
129129

130+
* `periodic_flush`
131+
132+
Periodic flush interval (in seconds). Set to `nil` to turn off this feature.
133+
130134
[Back to TOC](#table-of-contents)
131135

132136
initted

lib/resty/logger/socket.lua

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ local port
5252
local path
5353
local max_buffer_reuse = 10000 -- reuse buffer for at most 10000
5454
-- times
55+
local periodic_flush = nil
56+
local need_periodic_flush = nil
5557

5658
-- internal variables
5759
local buffer_size = 0
@@ -77,7 +79,6 @@ local logger_initted
7779
local counter = 0
7880

7981

80-
8182
local function _write_error(msg)
8283
last_error = msg
8384
end
@@ -279,6 +280,10 @@ local function _flush()
279280
.. err
280281
_write_error(err_msg)
281282
return nil, err_msg
283+
else
284+
if debug then
285+
ngx_log(DEBUG, "send " .. bytes .. " bytes")
286+
end
282287
end
283288

284289
buffer_size = buffer_size - #send_buffer
@@ -287,8 +292,29 @@ local function _flush()
287292
return bytes
288293
end
289294

295+
local function _periodic_flush()
296+
if need_periodic_flush then
297+
-- no regular flush happened after periodic flush timer had been set
298+
if debug then
299+
ngx_log(DEBUG, "performing periodic flush")
300+
end
301+
_flush()
302+
else
303+
if debug then
304+
ngx_log(DEBUG, "no need to perform periodic flush: regular flush "
305+
.. "happened before")
306+
end
307+
need_periodic_flush = true
308+
end
309+
310+
timer_at(periodic_flush, _periodic_flush)
311+
end
312+
290313
local function _flush_buffer()
291314
local ok, err = timer_at(0, _flush)
315+
316+
need_periodic_flush = false
317+
292318
if not ok then
293319
_write_error(err)
294320
return nil, err
@@ -332,6 +358,8 @@ function _M.init(user_config)
332358
pool_size = v
333359
elseif k == "max_buffer_reuse" then
334360
max_buffer_reuse = v
361+
elseif k == "periodic_flush" then
362+
periodic_flush = v
335363
end
336364
end
337365

@@ -355,6 +383,15 @@ function _M.init(user_config)
355383

356384
logger_initted = true
357385

386+
if periodic_flush then
387+
if debug then
388+
ngx_log(DEBUG, "periodic flush enabled for every "
389+
.. periodic_flush .. " milliseconds")
390+
end
391+
need_periodic_flush = true
392+
timer_at(periodic_flush, _periodic_flush)
393+
end
394+
358395
return logger_initted
359396
end
360397

t/sanity.t

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,3 +648,54 @@ log buffer reuse limit (1) reached, create a new "log_buffer_data"
648648
--- response_body
649649
foo
650650
wrote bytes: 15
651+
652+
653+
654+
=== TEST 16: flush periodically
655+
--- http_config eval: $::HttpConfig
656+
--- config
657+
location /t {
658+
content_by_lua '
659+
ngx.say("foo")
660+
local logger = require "resty.logger.socket"
661+
if not logger.initted() then
662+
local ok, err = logger.init{
663+
host = "127.0.0.1",
664+
port = 29999,
665+
flush_limit = 1000,
666+
drop_limit = 10000,
667+
retry_interval = 1,
668+
timeout = 50,
669+
max_buffer_reuse = 100,
670+
periodic_flush = 0.03, -- 0.03s
671+
}
672+
end
673+
674+
local bytes, err
675+
bytes, err = logger.log("foo")
676+
if err then
677+
ngx.log(ngx.ERR, err)
678+
end
679+
ngx.say("wrote bytes: ", bytes)
680+
681+
ngx.sleep(0.05)
682+
683+
bytes, err = logger.log("bar")
684+
if err then
685+
ngx.log(ngx.ERR, err)
686+
end
687+
ngx.say("wrote bytes: ", bytes)
688+
ngx.sleep(0.05)
689+
';
690+
}
691+
--- request
692+
GET /t
693+
--- wait: 0.1
694+
--- tcp_listen: 29999
695+
--- tcp_reply:
696+
--- tcp_query: foobar
697+
--- tcp_query_len: 6
698+
--- response_body
699+
foo
700+
wrote bytes: 3
701+
wrote bytes: 3

0 commit comments

Comments
 (0)