Skip to content

Commit

Permalink
Flush event buffer at least once per 60 seconds
Browse files Browse the repository at this point in the history
Using a static timeout per event just resets the timer every time an
event is emitted, which can continue until the buffer fills up.
  • Loading branch information
jgaskins committed Aug 2, 2024
1 parent ede5c18 commit afc24cd
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/honeybadger/event_dispatch.cr
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,14 @@ module Honeybadger
private def run
next_event = nil

# Main run loop
# 1. Buffer messages until either 60 seconds have passed or the buffer
# exceeds 5MB
# 2. Flush the buffer to the Honeybadger API
# 3. Clear the buffer
loop do
buffering = true
wait_time = 60.seconds

while buffering
# If the previous iteration exceeded the buffer cap, we flushed it and
Expand All @@ -48,6 +54,7 @@ module Honeybadger
next_event = nil
end

started_waiting = Time.monotonic
select
when event = @channel.receive
# Limits for events endpoint: https://docs.honeybadger.io/api/reporting-events/#limits
Expand All @@ -64,7 +71,10 @@ module Honeybadger
buffering = false
end
end
when timeout(1.second)

wait_time -= Time.monotonic - started_waiting
when timeout(wait_time)
buffering = false
next_event = nil
end
end
Expand Down

0 comments on commit afc24cd

Please sign in to comment.