Skip to content

Commit

Permalink
Merge pull request #272 from riemann/backoff
Browse files Browse the repository at this point in the history
Use truncated exponential backoff for reconnection
  • Loading branch information
jamtur01 authored Oct 16, 2023
2 parents 42e3d11 + 52d01f1 commit 87fb130
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/riemann/tools/riemann_client_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ module Tools
class RiemannClientWrapper
attr_reader :options

BACKOFF_TMIN = 0.5 # Minimum delay between reconnection attempts
BACKOFF_TMAX = 30.0 # Maximum delay
BACKOFF_FACTOR = 2

def initialize(options)
@options = options

Expand All @@ -18,15 +22,25 @@ def initialize(options)

@worker = Thread.new do
Thread.current.abort_on_exception = true
backoff_delay = BACKOFF_TMIN

loop do
events = []

events << @queue.pop
events << @queue.pop while !@queue.empty? && events.size < @max_bulk_size

client.bulk_send(events)
backoff_delay = BACKOFF_TMIN
rescue StandardError => e
warn "Dropping #{events.size} event#{'s' if events.size > 1} due to #{e}"
sleep(backoff_delay)

dropped_count = events.size + @queue.size
@queue.clear
warn "Dropped #{dropped_count} event#{'s' if dropped_count > 1} due to #{e}"

backoff_delay *= BACKOFF_FACTOR
backoff_delay = BACKOFF_TMAX if backoff_delay > BACKOFF_TMAX
end
end

Expand Down

0 comments on commit 87fb130

Please sign in to comment.