crowdsec/pkg/leakybucket/queue.go

43 lines
994 B
Go
Raw Permalink Normal View History

2020-05-15 09:39:16 +00:00
package leakybucket
import (
"github.com/crowdsecurity/crowdsec/pkg/types"
log "github.com/sirupsen/logrus"
)
// Queue holds a limited size queue
2020-05-15 09:39:16 +00:00
type Queue struct {
Queue []types.Event
L int //capacity
}
// NewQueue create a new queue with a size of l
func NewQueue(l int) *Queue {
if l == -1 {
return &Queue{
Queue: make([]types.Event, 0),
L: int(^uint(0) >> 1), // max integer value, architecture independent
}
}
q := &Queue{
Queue: make([]types.Event, 0, l),
L: l,
}
log.WithFields(log.Fields{"Capacity": q.L}).Debugf("Creating queue")
return q
}
// Add an event in the queue. If it has already l elements, the first
// element is dropped before adding the new m element
func (q *Queue) Add(m types.Event) {
for len(q.Queue) > q.L { //we allow to add one element more than the true capacity
q.Queue = q.Queue[1:]
}
q.Queue = append(q.Queue, m)
}
// GetQueue returns the entire queue
func (q *Queue) GetQueue() []types.Event {
return q.Queue
}