This commit is contained in:
Sebastien Blot 2022-08-18 09:43:40 +02:00
parent 5ae97d582c
commit 56f457d4e3
No known key found for this signature in database
GPG key ID: DFC2902F40449F6A
2 changed files with 16 additions and 11 deletions

View file

@ -70,11 +70,12 @@ func runCrowdsec(cConfig *csconfig.Config, parsers *parser.Parsers) error {
parserWg.Wait() parserWg.Wait()
bucketWg := &sync.WaitGroup{} bucketWg := &sync.WaitGroup{}
log.Infof("BucketsGCEnabled: %v", cConfig.Crowdsec.BucketsGCEnabled) //Only start the blackhole GC routine if buckets GC is not enabled, ie we are replaying a file
//This is because the GC routine expects events to happen in real time, which is not the case during a replay.
if !cConfig.Crowdsec.BucketsGCEnabled { if !cConfig.Crowdsec.BucketsGCEnabled {
bucketsTomb.Go(func() error { bucketsTomb.Go(func() error {
bucketWg.Add(1) bucketWg.Add(1)
leakybucket.CleanupBlackhole(&bucketsTomb) leakybucket.BlackholeGC(&bucketsTomb)
bucketWg.Done() bucketWg.Done()
return nil return nil
}) })

View file

@ -30,7 +30,18 @@ func NewBlackhole(bucketFactory *BucketFactory) (*Blackhole, error) {
}, nil }, nil
} }
func CleanupBlackhole(bucketsTomb *tomb.Tomb) error { func CleanupBlackhole(lastEvent time.Time) {
BlackholeTracking.Range(func(key, value interface{}) bool {
cleanupDate := value.(BlackholeExpiration).blExpiration
if cleanupDate.Before(lastEvent) {
log.Debugf("Expiring blackhole for %s", key)
BlackholeTracking.Delete(key)
}
return true
})
}
func BlackholeGC(bucketsTomb *tomb.Tomb) error {
ticker := time.NewTicker(10 * time.Second) ticker := time.NewTicker(10 * time.Second)
for { for {
select { select {
@ -42,14 +53,7 @@ func CleanupBlackhole(bucketsTomb *tomb.Tomb) error {
}) })
return nil return nil
case <-ticker.C: case <-ticker.C:
BlackholeTracking.Range(func(key, value interface{}) bool { CleanupBlackhole(time.Now().UTC())
cleanupDate := value.(BlackholeExpiration).blExpiration
if cleanupDate.Before(time.Now().UTC()) {
log.Debugf("Expiring blackhole for %s", key)
BlackholeTracking.Delete(key)
}
return true
})
} }
} }
} }