fix ticker mix up (#1807)

Co-authored-by: sabban <15465465+sabban@users.noreply.github.com>
This commit is contained in:
Manuel Sabban 2022-10-13 14:30:27 +02:00 committed by GitHub
parent 4b3c9c2806
commit 7359586f1c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -195,8 +195,8 @@ func FromFactory(bucketFactory BucketFactory) *Leaky {
func LeakRoutine(leaky *Leaky) error { func LeakRoutine(leaky *Leaky) error {
var ( var (
durationTicker <-chan time.Time = make(<-chan time.Time) durationTicker <-chan time.Time = make(<-chan time.Time)
underflowTicker *time.Ticker firstEvent bool = true
) )
defer types.CatchPanic(fmt.Sprintf("crowdsec/LeakRoutine/%s", leaky.Name)) defer types.CatchPanic(fmt.Sprintf("crowdsec/LeakRoutine/%s", leaky.Name))
@ -240,12 +240,19 @@ func LeakRoutine(leaky *Leaky) error {
leaky.Pour(leaky, *msg) // glue for now leaky.Pour(leaky, *msg) // glue for now
//Clear cache on behalf of pour //Clear cache on behalf of pour
if underflowTicker != nil {
underflowTicker.Stop() // if durationTicker isn't initialized, then we're pouring our first event
if firstEvent {
durationTicker = time.NewTicker(leaky.Duration).C
} }
underflowTicker = time.NewTicker(leaky.Duration)
durationTicker = underflowTicker.C // reinitialize the durationTicker when it's not a counter bucket
defer underflowTicker.Stop() if !leaky.timedOverflow {
ticker := time.NewTicker(leaky.Duration)
durationTicker = ticker.C
defer ticker.Stop()
}
firstEvent = false
/*we overflowed*/ /*we overflowed*/
case ofw := <-leaky.Out: case ofw := <-leaky.Out:
leaky.overflow(ofw) leaky.overflow(ofw)