-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added custom_size_based_buffer class
Added the feature for size and time based flushing of buffer. Added config options for max_interval and max_size. Once either one is reached the events stored in the buffer will be flushed.
- Loading branch information
1 parent
7e4d38a
commit df78334
Showing
3 changed files
with
82 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
module LogStash | ||
module Outputs | ||
class CustomSizeBasedBuffer | ||
def initialize(max_size, max_interval, &flush_callback) | ||
@max_size = max_size | ||
@max_interval = max_interval | ||
@flush_callback = flush_callback | ||
@buffer = [] | ||
@mutex = Mutex.new | ||
@last_flush_time = Time.now | ||
|
||
start_flusher_thread | ||
end | ||
|
||
def <<(event) | ||
@mutex.synchronize do | ||
@buffer << event | ||
flush if @buffer.size >= @max_size | ||
end | ||
end | ||
|
||
private | ||
def start_flusher_thread | ||
Thread.new do | ||
loop do | ||
sleep @max_interval | ||
flush_if_needed | ||
end | ||
end | ||
end | ||
|
||
def flush_if_needed | ||
@mutex.synchronize do | ||
if Time.now - @last_flush_time >= @max_interval | ||
flush | ||
end | ||
end | ||
end | ||
|
||
def flush | ||
return if @buffer.empty? | ||
|
||
@flush_callback.call(@buffer) | ||
@buffer.clear | ||
@last_flush_time = Time.now | ||
end | ||
end | ||
end | ||
end | ||
|