bitbucket.org/Aishee/synsec@v0.0.0-20210414005726-236fc01a153d/pkg/leakybucket/timemachine.go (about) 1 package leakybucket 2 3 import ( 4 "time" 5 6 "bitbucket.org/Aishee/synsec/pkg/types" 7 "github.com/davecgh/go-spew/spew" 8 log "github.com/sirupsen/logrus" 9 ) 10 11 func TimeMachinePour(l *Leaky, msg types.Event) { 12 var ( 13 d time.Time 14 err error 15 ) 16 if msg.MarshaledTime == "" { 17 log.Warningf("Trying to time-machine event without timestamp : %s", spew.Sdump(msg)) 18 return 19 } 20 21 err = d.UnmarshalText([]byte(msg.MarshaledTime)) 22 if err != nil { 23 log.Warningf("Failed unmarshaling event time (%s) : %v", msg.MarshaledTime, err) 24 return 25 } 26 27 l.Total_count += 1 28 l.mutex.Lock() 29 if l.First_ts.IsZero() { 30 l.logger.Debugf("First event, bucket creation time : %s", d) 31 l.First_ts = d 32 } 33 l.Last_ts = d 34 l.mutex.Unlock() 35 36 if l.Limiter.AllowN(d, 1) { 37 l.logger.Tracef("Time-Pouring event %s (tokens:%f)", d, l.Limiter.GetTokensCount()) 38 l.Queue.Add(msg) 39 } else { 40 l.Ovflw_ts = d 41 l.logger.Debugf("Bucket overflow at %s", l.Ovflw_ts) 42 l.Queue.Add(msg) 43 l.Out <- l.Queue 44 } 45 } 46 47 func NewTimeMachine(g BucketFactory) *Leaky { 48 l := NewLeaky(g) 49 g.logger.Tracef("Instanciating timeMachine bucket") 50 l.Pour = TimeMachinePour 51 l.Mode = TIMEMACHINE 52 return l 53 }