@@ -52,6 +52,8 @@ local port
5252local path
5353local 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
5759local buffer_size = 0
@@ -77,7 +79,6 @@ local logger_initted
7779local counter = 0
7880
7981
80-
8182local function _write_error (msg )
8283 last_error = msg
8384end
@@ -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
288293end
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+
290313local 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
359396end
360397
0 commit comments