-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2c0b8db
commit d032aee
Showing
8 changed files
with
345 additions
and
186 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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package internal | ||
|
||
import ( | ||
"log" | ||
"time" | ||
) | ||
|
||
type BackgroundFlusher interface { | ||
Start() | ||
Stop() | ||
} | ||
|
||
type backgroundFlusher struct { | ||
ticker *time.Ticker | ||
interval time.Duration | ||
handler LineHandler | ||
stop chan struct{} | ||
} | ||
|
||
func NewBackgroundFlusher(interval time.Duration, handler LineHandler) BackgroundFlusher { | ||
return &backgroundFlusher{ | ||
interval: interval, | ||
handler: handler, | ||
stop: make(chan struct{}), | ||
} | ||
} | ||
|
||
func (f *backgroundFlusher) Start() { | ||
format := f.handler.Format() | ||
if f.ticker != nil { | ||
return | ||
} | ||
f.ticker = time.NewTicker(f.interval) | ||
go func() { | ||
for { | ||
select { | ||
case tick := <-f.ticker.C: | ||
log.Printf("%s -- flushing at: %s\n", format, tick) | ||
err := f.handler.FlushWithThrottling() | ||
if err != nil { | ||
log.Printf("%s -- error during background flush: %s\n", format, err.Error()) | ||
} else { | ||
log.Printf("%s -- flush completed at %s\n", format, time.Now()) | ||
} | ||
case <-f.stop: | ||
return | ||
} | ||
} | ||
}() | ||
} | ||
|
||
func (f *backgroundFlusher) Stop() { | ||
f.ticker.Stop() | ||
f.stop <- struct{}{} | ||
} |
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
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.