chore: refactor background flushing #175
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR separates the responsibility for background flushing of the SDK's
buffer
channel into a type solely responsible for background flushing.Prior to this PR, the flush ticker goroutine also applied request throttling logic. This behavior is only ever enabled for the
event
handler.The throttling logic used the
RealLineHandler
's mutex to apply throttling. I'm not sure if that's a good idea or a bad idea, but it relied on running inside theRealLineHandler
's ticker goroutine - theSleep
needed to run in a goroutine in order unobtrusively blockFlush
es until the throttling time had ended.No existing tests cover this behavior, so we should backfill some before merging this PR.✅RealLineHandler
tests using the throttling behaviorThis PR doesn't use the mutex, but simply remembers the future time when it will be ok to send requests again, and if
Flush
FlushWithThrottling
is called before that time is up, theFlushWithThrottling
usestime.Sleep
to wait until the cooldown period ends.Other Changes in this PR:
1m
from10m
, which should be enoughlockOnThrottledError
tothrottleRequestsOnBackpressure
FlushWithThrottling()
throttleOnSleepDuration
configurable for testingidea: is the
LineHandlerOption
interface worth it given this API is entirely internal anyway?🟢 the option API allows the
NewLineHandler
factory to have a small number of required fields❔compare with standard library types like
http.Client
, which has a lot of fields, but it seems that all of theirzero
values are designed to be appropriate defaults, and no other initialization is required.question: does the throttling logic in flush need to be protected by a mutex or atomic operators?
In practice:
In theory:
WaitGroup
?) might require enabling throttling for all the formats, and might require changes so that theresumeAt
field is atomically read and written.idea: allow opt-out of background flushing in the future
The telegraf output plugin has an awkward line where, if the user has set
immediate_flush = true
it sets the background flush interval to a very large number. Because there is no API for disabling background flushes completely.