mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-12 19:32:48 -06:00
b2f2e73944
This introduces a new trigger mode for the decision log plugin: decision_logs.reporting.trigger=immediate The immediate trigger mode will upload events as soon as enough events are received to hit the configured upload limit. If not enough events are received within the configured min-max delay, the events received so far are flushed and uploaded. Signed-off-by: Sebastian Spaink <sebastianspaink@gmail.com>
65 lines
1.2 KiB
Go
65 lines
1.2 KiB
Go
// Copyright 2018 The OPA Authors. All rights reserved.
|
|
// Use of this source code is governed by an Apache2
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package logs
|
|
|
|
import (
|
|
"container/list"
|
|
)
|
|
|
|
// logBuffer implements a circular FIFO buffer for the plugin that caps memory
|
|
// usage at the configured limit. If the buffer size is exceeded, events from
|
|
// the front of the buffer are dropped.
|
|
type logBuffer struct {
|
|
usage int64
|
|
limit int64
|
|
l *list.List
|
|
}
|
|
|
|
type logBufferElem struct {
|
|
bs []byte
|
|
}
|
|
|
|
func newLogBuffer(limit int64) *logBuffer {
|
|
return &logBuffer{
|
|
limit: limit,
|
|
usage: 0,
|
|
l: list.New(),
|
|
}
|
|
}
|
|
|
|
func (lb *logBuffer) Push(bs []byte) (dropped int) {
|
|
size := int64(len(bs))
|
|
|
|
if lb.limit > 0 {
|
|
for elem := lb.l.Front(); elem != nil && (lb.usage+size > lb.limit); elem = elem.Next() {
|
|
drop := elem.Value.(logBufferElem).bs
|
|
lb.l.Remove(elem)
|
|
lb.usage -= int64(len(drop))
|
|
dropped++
|
|
}
|
|
}
|
|
|
|
elem := logBufferElem{bs}
|
|
|
|
lb.l.PushBack(elem)
|
|
lb.usage += size
|
|
return dropped
|
|
}
|
|
|
|
func (lb *logBuffer) Pop() []byte {
|
|
elem := lb.l.Front()
|
|
if elem != nil {
|
|
e := elem.Value.(logBufferElem)
|
|
lb.usage -= int64(len(e.bs))
|
|
lb.l.Remove(elem)
|
|
return e.bs
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (lb *logBuffer) Len() int {
|
|
return lb.l.Len()
|
|
}
|