2020-05-15 09:39:16 +00:00
|
|
|
package leakybucket
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TimeMachinePour(l *Leaky, msg types.Event) {
|
|
|
|
var (
|
|
|
|
d time.Time
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if msg.MarshaledTime == "" {
|
2021-02-25 10:26:46 +00:00
|
|
|
log.Warningf("Trying to time-machine event without timestamp : %s", spew.Sdump(msg))
|
2020-05-15 09:39:16 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = d.UnmarshalText([]byte(msg.MarshaledTime))
|
|
|
|
if err != nil {
|
|
|
|
log.Warningf("Failed unmarshaling event time (%s) : %v", msg.MarshaledTime, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
l.Total_count += 1
|
2021-02-25 10:26:46 +00:00
|
|
|
l.mutex.Lock()
|
2020-05-15 09:39:16 +00:00
|
|
|
if l.First_ts.IsZero() {
|
|
|
|
l.logger.Debugf("First event, bucket creation time : %s", d)
|
|
|
|
l.First_ts = d
|
|
|
|
}
|
|
|
|
l.Last_ts = d
|
2021-02-25 10:26:46 +00:00
|
|
|
l.mutex.Unlock()
|
2020-05-15 09:39:16 +00:00
|
|
|
|
|
|
|
if l.Limiter.AllowN(d, 1) {
|
|
|
|
l.logger.Tracef("Time-Pouring event %s (tokens:%f)", d, l.Limiter.GetTokensCount())
|
|
|
|
l.Queue.Add(msg)
|
|
|
|
} else {
|
|
|
|
l.Ovflw_ts = d
|
|
|
|
l.logger.Debugf("Bucket overflow at %s", l.Ovflw_ts)
|
|
|
|
l.Queue.Add(msg)
|
|
|
|
l.Out <- l.Queue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewTimeMachine(g BucketFactory) *Leaky {
|
|
|
|
l := NewLeaky(g)
|
2022-09-27 15:13:43 +00:00
|
|
|
g.logger.Tracef("Instantiating timeMachine bucket")
|
2020-05-15 09:39:16 +00:00
|
|
|
l.Pour = TimeMachinePour
|
|
|
|
l.Mode = TIMEMACHINE
|
|
|
|
return l
|
|
|
|
}
|