@@ -52,6 +52,8 @@ local port
52
52
local path
53
53
local max_buffer_reuse = 10000 -- reuse buffer for at most 10000
54
54
-- times
55
+ local periodic_flush = nil
56
+ local need_periodic_flush = nil
55
57
56
58
-- internal variables
57
59
local buffer_size = 0
@@ -77,7 +79,6 @@ local logger_initted
77
79
local counter = 0
78
80
79
81
80
-
81
82
local function _write_error (msg )
82
83
last_error = msg
83
84
end
@@ -279,6 +280,10 @@ local function _flush()
279
280
.. err
280
281
_write_error (err_msg )
281
282
return nil , err_msg
283
+ else
284
+ if debug then
285
+ ngx_log (DEBUG , " send " .. bytes .. " bytes" )
286
+ end
282
287
end
283
288
284
289
buffer_size = buffer_size - # send_buffer
@@ -287,8 +292,29 @@ local function _flush()
287
292
return bytes
288
293
end
289
294
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
+
290
313
local function _flush_buffer ()
291
314
local ok , err = timer_at (0 , _flush )
315
+
316
+ need_periodic_flush = false
317
+
292
318
if not ok then
293
319
_write_error (err )
294
320
return nil , err
@@ -332,6 +358,8 @@ function _M.init(user_config)
332
358
pool_size = v
333
359
elseif k == " max_buffer_reuse" then
334
360
max_buffer_reuse = v
361
+ elseif k == " periodic_flush" then
362
+ periodic_flush = v
335
363
end
336
364
end
337
365
@@ -355,6 +383,15 @@ function _M.init(user_config)
355
383
356
384
logger_initted = true
357
385
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
+
358
395
return logger_initted
359
396
end
360
397
0 commit comments