From ca4cd6d5599706206576941b21a5e3763738a31e Mon Sep 17 00:00:00 2001 From: "Thibault \"bui\" Koechlin" Date: Thu, 30 Jun 2022 17:36:01 +0200 Subject: [PATCH] attempt to fix ticker leak (#1620) --- pkg/leakybucket/bucket.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/pkg/leakybucket/bucket.go b/pkg/leakybucket/bucket.go index a1a7e971e..f9b360241 100644 --- a/pkg/leakybucket/bucket.go +++ b/pkg/leakybucket/bucket.go @@ -195,7 +195,8 @@ func FromFactory(bucketFactory BucketFactory) *Leaky { func LeakRoutine(leaky *Leaky) error { var ( - durationTicker <-chan time.Time = make(<-chan time.Time) + durationTicker <-chan time.Time = make(<-chan time.Time) + underflowTicker *time.Ticker ) defer types.CatchPanic(fmt.Sprintf("crowdsec/LeakRoutine/%s", leaky.Name)) @@ -239,9 +240,12 @@ func LeakRoutine(leaky *Leaky) error { leaky.Pour(leaky, *msg) // glue for now //Clear cache on behalf of pour - tmp := time.NewTicker(leaky.Duration) - durationTicker = tmp.C - defer tmp.Stop() + if underflowTicker != nil { + underflowTicker.Stop() + } + underflowTicker = time.NewTicker(leaky.Duration) + durationTicker = underflowTicker.C + defer underflowTicker.Stop() /*we overflowed*/ case ofw := <-leaky.Out: leaky.overflow(ofw)