crowdsec/pkg/types/queue.go
Thibault "bui" Koechlin 1dcf9d1ae1
Improved expr debugger (#2495)
* new expr debugger

---------

Co-authored-by: mmetc <92726601+mmetc@users.noreply.github.com>
2023-11-24 11:10:54 +01:00

41 lines
911 B
Go

package types
import (
log "github.com/sirupsen/logrus"
)
// Queue holds a limited size queue
type Queue struct {
Queue []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([]Event, 0),
L: int(^uint(0) >> 1), // max integer value, architecture independent
}
}
q := &Queue{
Queue: make([]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 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() []Event {
return q.Queue
}