Skip to content

refact pkg/leakybucket: simpler timer logic, remove defer call from loop #3627

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions pkg/leakybucket/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,7 @@
/* for now mimic a leak routine */
//LeakRoutine us the life of a bucket. It dies when the bucket underflows or overflows
func LeakRoutine(leaky *Leaky) error {
var (
durationTickerChan = make(<-chan time.Time)
durationTicker *time.Ticker
firstEvent = true
)
firstEvent := true

defer trace.CatchPanic(fmt.Sprintf("crowdsec/LeakRoutine/%s", leaky.Name))

Expand All @@ -233,11 +229,29 @@
if err != nil {
leaky.logger.Errorf("Problem at bucket initializiation. Bail out %T : %v", f, err)
close(leaky.Signal)
return fmt.Errorf("Problem at bucket initializiation. Bail out %T : %v", f, err)
return fmt.Errorf("problem at bucket initializiation. Bail out %T : %v", f, err)

Check warning on line 232 in pkg/leakybucket/bucket.go

View check run for this annotation

Codecov / codecov/patch

pkg/leakybucket/bucket.go#L232

Added line #L232 was not covered by tests
}
}

leaky.logger.Debugf("Leaky routine starting, lifetime : %s", leaky.Duration)

timer := time.NewTimer(leaky.Duration)
defer timer.Stop()

drain := func() {
select {
case <-timer.C:
default:

Check warning on line 244 in pkg/leakybucket/bucket.go

View check run for this annotation

Codecov / codecov/patch

pkg/leakybucket/bucket.go#L242-L244

Added lines #L242 - L244 were not covered by tests
}
}

reset := func() {
if !timer.Stop() {
drain()
}

Check warning on line 251 in pkg/leakybucket/bucket.go

View check run for this annotation

Codecov / codecov/patch

pkg/leakybucket/bucket.go#L250-L251

Added lines #L250 - L251 were not covered by tests
timer.Reset(leaky.Duration)
}

for {
select {
/*receiving an event*/
Expand Down Expand Up @@ -276,13 +290,7 @@

// reinitialize the durationTicker when it's not a counter bucket
if !leaky.timedOverflow || firstEvent {
if firstEvent {
durationTicker = time.NewTicker(leaky.Duration)
durationTickerChan = durationTicker.C
defer durationTicker.Stop()
} else {
durationTicker.Reset(leaky.Duration)
}
reset()
}
firstEvent = false
/*we overflowed*/
Expand All @@ -301,7 +309,7 @@
leaky.logger.Tracef("Returning from leaky routine.")
return nil
/*we underflow or reach bucket deadline (timers)*/
case <-durationTickerChan:
case <-timer.C:
var (
alert types.RuntimeAlert
err error
Expand Down
Loading