2023-02-15 01:52:11 +00:00
|
|
|
package logstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/benbjohnson/clock"
|
|
|
|
"github.com/zitadel/logging"
|
|
|
|
)
|
|
|
|
|
2023-09-15 14:58:45 +00:00
|
|
|
type bulkSink[T LogRecord[T]] interface {
|
|
|
|
SendBulk(ctx context.Context, bulk []T) error
|
2023-02-15 01:52:11 +00:00
|
|
|
}
|
|
|
|
|
2023-09-15 14:58:45 +00:00
|
|
|
type bulkSinkFunc[T LogRecord[T]] func(ctx context.Context, bulk []T) error
|
2023-02-15 01:52:11 +00:00
|
|
|
|
2023-09-15 14:58:45 +00:00
|
|
|
func (s bulkSinkFunc[T]) SendBulk(ctx context.Context, bulk []T) error {
|
|
|
|
return s(ctx, bulk)
|
2023-02-15 01:52:11 +00:00
|
|
|
}
|
|
|
|
|
2023-09-15 14:58:45 +00:00
|
|
|
type debouncer[T LogRecord[T]] struct {
|
2023-02-15 01:52:11 +00:00
|
|
|
// Storing context.Context in a struct is generally bad practice
|
|
|
|
// https://go.dev/blog/context-and-structs
|
|
|
|
// However, debouncer starts a go routine that triggers side effects itself.
|
|
|
|
// So, there is no incoming context.Context available when these events trigger.
|
|
|
|
// The only context we can use for the side effects is the app context.
|
|
|
|
// Because this can be cancelled by os signals, it's the better solution than creating new background contexts.
|
|
|
|
binarySignaledCtx context.Context
|
|
|
|
clock clock.Clock
|
|
|
|
ticker *clock.Ticker
|
|
|
|
mux sync.Mutex
|
|
|
|
cfg DebouncerConfig
|
2023-09-15 14:58:45 +00:00
|
|
|
storage bulkSink[T]
|
|
|
|
cache []T
|
2023-02-15 01:52:11 +00:00
|
|
|
cacheLen uint
|
|
|
|
}
|
|
|
|
|
|
|
|
type DebouncerConfig struct {
|
|
|
|
MinFrequency time.Duration
|
|
|
|
MaxBulkSize uint
|
|
|
|
}
|
|
|
|
|
2023-09-15 14:58:45 +00:00
|
|
|
func newDebouncer[T LogRecord[T]](binarySignaledCtx context.Context, cfg DebouncerConfig, clock clock.Clock, ship bulkSink[T]) *debouncer[T] {
|
|
|
|
a := &debouncer[T]{
|
2023-02-15 01:52:11 +00:00
|
|
|
binarySignaledCtx: binarySignaledCtx,
|
|
|
|
clock: clock,
|
|
|
|
cfg: cfg,
|
|
|
|
storage: ship,
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.MinFrequency > 0 {
|
|
|
|
a.ticker = clock.Ticker(cfg.MinFrequency)
|
|
|
|
go a.shipOnTicks()
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2023-09-15 14:58:45 +00:00
|
|
|
func (d *debouncer[T]) add(item T) {
|
2023-02-15 01:52:11 +00:00
|
|
|
d.mux.Lock()
|
|
|
|
defer d.mux.Unlock()
|
|
|
|
d.cache = append(d.cache, item)
|
|
|
|
d.cacheLen++
|
|
|
|
if d.cfg.MaxBulkSize > 0 && d.cacheLen >= d.cfg.MaxBulkSize {
|
|
|
|
// Add should not block and release the lock
|
|
|
|
go d.ship()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-15 14:58:45 +00:00
|
|
|
func (d *debouncer[T]) ship() {
|
2023-02-15 01:52:11 +00:00
|
|
|
if d.cacheLen == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
d.mux.Lock()
|
|
|
|
defer d.mux.Unlock()
|
2023-09-15 14:58:45 +00:00
|
|
|
if err := d.storage.SendBulk(d.binarySignaledCtx, d.cache); err != nil {
|
2023-02-15 01:52:11 +00:00
|
|
|
logging.WithError(err).WithField("size", len(d.cache)).Error("storing bulk failed")
|
|
|
|
}
|
|
|
|
d.cache = nil
|
|
|
|
d.cacheLen = 0
|
|
|
|
if d.cfg.MinFrequency > 0 {
|
|
|
|
d.ticker.Reset(d.cfg.MinFrequency)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-15 14:58:45 +00:00
|
|
|
func (d *debouncer[T]) shipOnTicks() {
|
2023-02-15 01:52:11 +00:00
|
|
|
for range d.ticker.C {
|
|
|
|
d.ship()
|
|
|
|
}
|
|
|
|
}
|