fix ticker (#1858)

Co-authored-by: sabban <15465465+sabban@users.noreply.github.com>
This commit is contained in:
Manuel Sabban 2022-11-04 13:56:43 +01:00 committed by GitHub
parent 668627f890
commit 8aca00326d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -195,8 +195,9 @@ func FromFactory(bucketFactory BucketFactory) *Leaky {
func LeakRoutine(leaky *Leaky) error {
var (
durationTicker <-chan time.Time = make(<-chan time.Time)
firstEvent bool = true
durationTickerChan <-chan time.Time = make(<-chan time.Time)
durationTicker *time.Ticker
firstEvent bool = true
)
defer types.CatchPanic(fmt.Sprintf("crowdsec/LeakRoutine/%s", leaky.Name))
@ -242,15 +243,16 @@ func LeakRoutine(leaky *Leaky) error {
//Clear cache on behalf of pour
// if durationTicker isn't initialized, then we're pouring our first event
if firstEvent {
durationTicker = time.NewTicker(leaky.Duration).C
}
// reinitialize the durationTicker when it's not a counter bucket
if !leaky.timedOverflow {
ticker := time.NewTicker(leaky.Duration)
durationTicker = ticker.C
defer ticker.Stop()
if !leaky.timedOverflow || firstEvent {
if firstEvent {
durationTicker = time.NewTicker(leaky.Duration)
durationTickerChan = durationTicker.C
defer durationTicker.Stop()
} else {
durationTicker.Reset(leaky.Duration)
}
}
firstEvent = false
/*we overflowed*/
@ -266,7 +268,7 @@ func LeakRoutine(leaky *Leaky) error {
leaky.logger.Tracef("Returning from leaky routine.")
return nil
/*we underflow or reach bucket deadline (timers)*/
case <-durationTicker:
case <-durationTickerChan:
var (
alert types.RuntimeAlert
err error