Преглед на файлове

fix ticker mix up (#1807)

Co-authored-by: sabban <15465465+sabban@users.noreply.github.com>
Manuel Sabban преди 2 години
родител
ревизия
7359586f1c
променени са 1 файла, в които са добавени 14 реда и са изтрити 7 реда
  1. 14 7
      pkg/leakybucket/bucket.go

+ 14 - 7
pkg/leakybucket/bucket.go

@@ -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)
-		underflowTicker *time.Ticker
+		durationTicker <-chan time.Time = make(<-chan time.Time)
+		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
+			}
+
+			// reinitialize the durationTicker when it's not a counter bucket
+			if !leaky.timedOverflow {
+				ticker := time.NewTicker(leaky.Duration)
+				durationTicker = ticker.C
+				defer ticker.Stop()
 			}
 			}
-			underflowTicker = time.NewTicker(leaky.Duration)
-			durationTicker = underflowTicker.C
-			defer underflowTicker.Stop()
+			firstEvent = false
 		/*we overflowed*/
 		/*we overflowed*/
 		case ofw := <-leaky.Out:
 		case ofw := <-leaky.Out:
 			leaky.overflow(ofw)
 			leaky.overflow(ofw)