Debian Codegraph @

pkg/sink/bufsink/sized.go

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 package bufsink
2
3 import (
4 "context"
5
6 "github.com/humanlogio/humanlog/internal/pkg/model"
7 "github.com/humanlogio/humanlog/sink"
8 )
9
10 type SizedBuffer struct {
11 Buffered []model.Event
12 flush sink.BatchSink
13 }
14
15 var _ sink.Sink = (*SizedBuffer)(nil)
16
17 func NewSizedBufferedSink(size int, flush sink.BatchSink) *SizedBuffer {
18 return &SizedBuffer{
19 Buffered: make([]model.Event, 0, size),
20 flush: flush,
21 }
22 }
23
24 func (sn *SizedBuffer) Receive(ctx context.Context, ev *model.Event) error {
25 sn.Buffered = append(sn.Buffered, *ev)
26 if len(sn.Buffered) == cap(sn.Buffered) {
27 if err := sn.flush.ReceiveBatch(ctx, sn.Buffered); err != nil {
28 sn.Buffered = sn.Buffered[:len(sn.Buffered)-1]
29 return err
30 }
31 sn.Buffered = sn.Buffered[:0]
32 }
33 return nil
34 }