-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer.go
More file actions
144 lines (121 loc) · 2.91 KB
/
Copy pathbuffer.go
File metadata and controls
144 lines (121 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package pulse
import (
"errors"
"sync"
"sync/atomic"
)
var ErrInvalidBufferCapacity = errors.New("pulse: buffer capacity must be greater than zero")
// RingBuffer is a fixed-size, thread-safe circular buffer for samples.
type RingBuffer struct {
mu sync.Mutex
data []bufferedSample
head int
size int
overflow atomic.Uint64
}
type bufferedSample struct {
sample Sample
walSegment int
}
// NewRingBuffer creates a new ring buffer with fixed capacity.
func NewRingBuffer(capacity int) (*RingBuffer, error) {
if capacity <= 0 {
return nil, ErrInvalidBufferCapacity
}
return &RingBuffer{data: make([]bufferedSample, capacity)}, nil
}
// Push inserts a sample. If full, the oldest sample is evicted.
func (b *RingBuffer) Push(s Sample) {
b.PushWithWALSegment(s, -1)
}
// PushWithWALSegment inserts a sample and tracks the WAL segment id when known.
func (b *RingBuffer) PushWithWALSegment(s Sample, walSegment int) {
b.mu.Lock()
defer b.mu.Unlock()
if len(b.data) == 0 {
return
}
entry := bufferedSample{sample: cloneSample(s), walSegment: walSegment}
if b.size == len(b.data) {
b.data[b.head] = entry
b.head = (b.head + 1) % len(b.data)
b.overflow.Add(1)
return
}
tail := (b.head + b.size) % len(b.data)
b.data[tail] = entry
b.size++
}
// DrainAllWithWALSegments returns all buffered entries and clears the buffer.
func (b *RingBuffer) DrainAllWithWALSegments() []bufferedSample {
b.mu.Lock()
defer b.mu.Unlock()
if b.size == 0 {
return nil
}
out := make([]bufferedSample, 0, b.size)
for i := 0; i < b.size; i++ {
idx := (b.head + i) % len(b.data)
entry := b.data[idx]
entry.sample = cloneSample(entry.sample)
out = append(out, entry)
}
b.head = 0
b.size = 0
return out
}
// DrainAll returns all samples in insertion order and clears the buffer.
func (b *RingBuffer) DrainAll() []Sample {
entries := b.DrainAllWithWALSegments()
if len(entries) == 0 {
return nil
}
out := make([]Sample, 0, len(entries))
for _, entry := range entries {
out = append(out, entry.sample)
}
return out
}
// Peek returns up to n samples in insertion order without removing them.
func (b *RingBuffer) Peek(n int) []Sample {
b.mu.Lock()
defer b.mu.Unlock()
if b.size == 0 || n <= 0 {
return nil
}
if n > b.size {
n = b.size
}
out := make([]Sample, 0, n)
for i := 0; i < n; i++ {
idx := (b.head + i) % len(b.data)
out = append(out, cloneSample(b.data[idx].sample))
}
return out
}
func (b *RingBuffer) Len() int {
b.mu.Lock()
defer b.mu.Unlock()
return b.size
}
func (b *RingBuffer) Cap() int {
return len(b.data)
}
func (b *RingBuffer) IsFull() bool {
b.mu.Lock()
defer b.mu.Unlock()
return b.size == len(b.data)
}
func (b *RingBuffer) OverflowCount() uint64 {
return b.overflow.Load()
}
func cloneSample(in Sample) Sample {
out := in
if len(in.Values) > 0 {
out.Values = make(map[string]float64, len(in.Values))
for k, v := range in.Values {
out.Values[k] = v
}
}
return out
}