fix: Revert "Reset the flush interval timer when flush is requested or batch is ready. (#8953)" (#9800)

This reverts commit a6d2c4f254.
This commit is contained in:
Joshua Powers 2021-09-30 10:28:48 -06:00 committed by GitHub
parent 11193a3b4c
commit 70afc94d12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 16 deletions

View File

@ -775,7 +775,7 @@ func (a *Agent) runOutputs(
func (a *Agent) flushLoop( func (a *Agent) flushLoop(
ctx context.Context, ctx context.Context,
output *models.RunningOutput, output *models.RunningOutput,
ticker *RollingTicker, ticker Ticker,
) { ) {
logError := func(err error) { logError := func(err error) {
if err != nil { if err != nil {
@ -804,11 +804,15 @@ func (a *Agent) flushLoop(
case <-ticker.Elapsed(): case <-ticker.Elapsed():
logError(a.flushOnce(output, ticker, output.Write)) logError(a.flushOnce(output, ticker, output.Write))
case <-flushRequested: case <-flushRequested:
ticker.Reset()
logError(a.flushOnce(output, ticker, output.Write)) logError(a.flushOnce(output, ticker, output.Write))
case <-output.BatchReady: case <-output.BatchReady:
ticker.Reset() // Favor the ticker over batch ready
logError(a.flushOnce(output, ticker, output.WriteBatch)) select {
case <-ticker.Elapsed():
logError(a.flushOnce(output, ticker, output.Write))
default:
logError(a.flushOnce(output, ticker, output.WriteBatch))
}
} }
} }
} }

View File

@ -214,7 +214,6 @@ type RollingTicker struct {
ch chan time.Time ch chan time.Time
cancel context.CancelFunc cancel context.CancelFunc
wg sync.WaitGroup wg sync.WaitGroup
timer *clock.Timer
} }
func NewRollingTicker(interval, jitter time.Duration) *RollingTicker { func NewRollingTicker(interval, jitter time.Duration) *RollingTicker {
@ -231,12 +230,12 @@ func newRollingTicker(interval, jitter time.Duration, clock clock.Clock) *Rollin
} }
d := t.next() d := t.next()
t.timer = clock.Timer(d) timer := clock.Timer(d)
t.wg.Add(1) t.wg.Add(1)
go func() { go func() {
defer t.wg.Done() defer t.wg.Done()
t.run(ctx) t.run(ctx, timer)
}() }()
return t return t
@ -246,28 +245,24 @@ func (t *RollingTicker) next() time.Duration {
return t.interval + internal.RandomDuration(t.jitter) return t.interval + internal.RandomDuration(t.jitter)
} }
func (t *RollingTicker) run(ctx context.Context) { func (t *RollingTicker) run(ctx context.Context, timer *clock.Timer) {
for { for {
select { select {
case <-ctx.Done(): case <-ctx.Done():
t.timer.Stop() timer.Stop()
return return
case now := <-t.timer.C: case now := <-timer.C:
select { select {
case t.ch <- now: case t.ch <- now:
default: default:
} }
t.Reset() d := t.next()
timer.Reset(d)
} }
} }
} }
// Reset the ticker to the next interval + jitter.
func (t *RollingTicker) Reset() {
t.timer.Reset(t.next())
}
func (t *RollingTicker) Elapsed() <-chan time.Time { func (t *RollingTicker) Elapsed() <-chan time.Time {
return t.ch return t.ch
} }