github.com/crowdsecurity/crowdsec@v1.6.1/pkg/leakybucket/timemachine.go (about) 1 package leakybucket 2 3 import ( 4 "time" 5 6 "github.com/crowdsecurity/crowdsec/pkg/types" 7 log "github.com/sirupsen/logrus" 8 ) 9 10 func TimeMachinePour(l *Leaky, msg types.Event) { 11 var ( 12 d time.Time 13 err error 14 ) 15 if msg.MarshaledTime == "" { 16 log.WithFields(log.Fields{ 17 "evt_type": msg.Line.Labels["type"], 18 "evt_src": msg.Line.Src, 19 "scenario": l.Name, 20 }).Warningf("Trying to process event without evt.StrTime. Event cannot be poured to scenario") 21 return 22 } 23 24 err = d.UnmarshalText([]byte(msg.MarshaledTime)) 25 if err != nil { 26 log.Warningf("Failed unmarshaling event time (%s) : %v", msg.MarshaledTime, err) 27 return 28 } 29 30 l.Total_count += 1 31 l.mutex.Lock() 32 if l.First_ts.IsZero() { 33 l.logger.Debugf("First event, bucket creation time : %s", d) 34 l.First_ts = d 35 } 36 l.Last_ts = d 37 l.mutex.Unlock() 38 if l.Limiter.AllowN(d, 1) || l.conditionalOverflow { 39 l.logger.Tracef("Time-Pouring event %s (tokens:%f)", d, l.Limiter.GetTokensCount()) 40 l.Queue.Add(msg) 41 } else { 42 l.Ovflw_ts = d 43 l.logger.Debugf("Bucket overflow at %s", l.Ovflw_ts) 44 l.Queue.Add(msg) 45 l.Out <- l.Queue 46 } 47 } 48 49 func NewTimeMachine(g BucketFactory) *Leaky { 50 l := NewLeaky(g) 51 g.logger.Tracef("Instantiating timeMachine bucket") 52 l.Pour = TimeMachinePour 53 l.Mode = types.TIMEMACHINE 54 return l 55 }