From 5ae97d582cf8380398d92005fa4053a7bf0ef158 Mon Sep 17 00:00:00 2001 From: Sebastien Blot Date: Tue, 16 Aug 2022 15:12:56 +0200 Subject: [PATCH] don't run blackhole cleanup routine when replaying logs --- cmd/crowdsec/crowdsec.go | 15 +++++++++------ cmd/crowdsec/main.go | 2 ++ pkg/leakybucket/blackhole.go | 8 +++----- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/cmd/crowdsec/crowdsec.go b/cmd/crowdsec/crowdsec.go index 2fb7602d6..6f75daf5c 100644 --- a/cmd/crowdsec/crowdsec.go +++ b/cmd/crowdsec/crowdsec.go @@ -70,12 +70,15 @@ func runCrowdsec(cConfig *csconfig.Config, parsers *parser.Parsers) error { parserWg.Wait() bucketWg := &sync.WaitGroup{} - bucketsTomb.Go(func() error { - bucketWg.Add(1) - leakybucket.CleanupBlackhole(&bucketsTomb) - bucketWg.Done() - return nil - }) + log.Infof("BucketsGCEnabled: %v", cConfig.Crowdsec.BucketsGCEnabled) + if !cConfig.Crowdsec.BucketsGCEnabled { + bucketsTomb.Go(func() error { + bucketWg.Add(1) + leakybucket.CleanupBlackhole(&bucketsTomb) + bucketWg.Done() + return nil + }) + } bucketsTomb.Go(func() error { bucketWg.Add(1) /*restore previous state as well if present*/ diff --git a/cmd/crowdsec/main.go b/cmd/crowdsec/main.go index f1bbe75ca..cd3622f28 100644 --- a/cmd/crowdsec/main.go +++ b/cmd/crowdsec/main.go @@ -149,6 +149,8 @@ func LoadAcquisition(cConfig *csconfig.Config) error { flags.Labels = labels flags.Labels["type"] = flags.SingleFileType + cConfig.Crowdsec.BucketsGCEnabled = true + dataSources, err = acquisition.LoadAcquisitionFromDSN(flags.OneShotDSN, flags.Labels) if err != nil { return errors.Wrapf(err, "failed to configure datasource for %s", flags.OneShotDSN) diff --git a/pkg/leakybucket/blackhole.go b/pkg/leakybucket/blackhole.go index 2d27bb61f..19b3efd0b 100644 --- a/pkg/leakybucket/blackhole.go +++ b/pkg/leakybucket/blackhole.go @@ -15,8 +15,7 @@ type Blackhole struct { } type BlackholeExpiration struct { - blExpiration time.Time - cleanupExpiration time.Time //we need a separate expiration for the cleanup to properly handle timemachine buckets + blExpiration time.Time } func NewBlackhole(bucketFactory *BucketFactory) (*Blackhole, error) { @@ -44,7 +43,7 @@ func CleanupBlackhole(bucketsTomb *tomb.Tomb) error { return nil case <-ticker.C: BlackholeTracking.Range(func(key, value interface{}) bool { - cleanupDate := value.(BlackholeExpiration).cleanupExpiration + cleanupDate := value.(BlackholeExpiration).blExpiration if cleanupDate.Before(time.Now().UTC()) { log.Debugf("Expiring blackhole for %s", key) BlackholeTracking.Delete(key) @@ -72,8 +71,7 @@ func (bl *Blackhole) OnBucketOverflow(bucketFactory *BucketFactory) func(*Leaky, } BlackholeTracking.Store(leaky.Mapkey, BlackholeExpiration{ - blExpiration: leaky.Ovflw_ts.Add(bl.duration), - cleanupExpiration: time.Now().UTC().Add(bl.duration), + blExpiration: leaky.Ovflw_ts.Add(bl.duration), }) leaky.logger.Debugf("Blackhole triggered for %s (expiration : %s)", leaky.Mapkey, leaky.Ovflw_ts.Add(bl.duration))