-
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. Updated kusto.rb
- Loading branch information
1 parent
cbb7444
commit 5b2af15
Showing
2 changed files
with
66 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,74 @@ | ||
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 | ||
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 | ||
@shutdown = false | ||
@flusher_condition = ConditionVariable.new | ||
|
||
start_flusher_thread | ||
end | ||
|
||
def <<(event) | ||
@mutex.synchronize do | ||
@buffer << event | ||
flush if @buffer.size >= @max_size | ||
end | ||
|
||
def <<(event) | ||
@mutex.synchronize do | ||
@buffer << event | ||
flush if @buffer.size >= @max_size | ||
end | ||
end | ||
|
||
def shutdown | ||
@mutex.synchronize do | ||
@shutdown = true | ||
@flusher_condition.signal # Wake up the flusher thread | ||
end | ||
|
||
private | ||
def start_flusher_thread | ||
Thread.new do | ||
loop do | ||
sleep @max_interval | ||
flush_if_needed | ||
@flusher_thread.join | ||
flush # Ensure final flush after shutdown | ||
end | ||
|
||
private | ||
|
||
def start_flusher_thread | ||
@flusher_thread = Thread.new do | ||
loop do | ||
@mutex.synchronize do | ||
break if @shutdown | ||
if Time.now - @last_flush_time >= @max_interval | ||
flush | ||
end | ||
@flusher_condition.wait(@mutex, @max_interval) # Wait for either the interval or shutdown signal | ||
end | ||
end | ||
end | ||
|
||
def flush_if_needed | ||
@mutex.synchronize do | ||
if Time.now - @last_flush_time >= @max_interval | ||
flush | ||
end | ||
end | ||
|
||
|
||
def flush_if_needed | ||
@mutex.synchronize do | ||
if Time.now - @last_flush_time >= @max_interval | ||
flush | ||
end | ||
end | ||
|
||
def flush | ||
return if @buffer.empty? | ||
|
||
end | ||
|
||
def flush | ||
return if @buffer.empty? | ||
|
||
begin | ||
@flush_callback.call(@buffer) | ||
rescue => e | ||
# Log the error and continue, | ||
puts "Error during flush: #{e.message}" | ||
puts e.backtrace.join("\n") | ||
ensure | ||
@buffer.clear | ||
@last_flush_time = Time.now | ||
end | ||
end | ||
end | ||
end | ||
end |