forked from flachnetz/dd-zipkin-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffer.go
44 lines (35 loc) · 837 Bytes
/
buffer.go
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
package zipkinproxy
import (
"github.com/flachnetz/dd-zipkin-proxy/proxy"
"sync"
)
type SpansBuffer struct {
lock sync.Mutex
position uint
spans []proxy.Span
}
func NewSpansBuffer(capacity uint) *SpansBuffer {
spans := make([]proxy.Span, capacity)
return &SpansBuffer{spans: spans}
}
func (buffer *SpansBuffer) ReadFrom(tracesCh <-chan proxy.Trace) {
for trace := range tracesCh {
buffer.lock.Lock()
for _, span := range trace {
buffer.spans[buffer.position] = span
buffer.position = (buffer.position + 1) % uint(len(buffer.spans))
}
buffer.lock.Unlock()
}
}
func (buffer *SpansBuffer) ToSlice() []proxy.Span {
buffer.lock.Lock()
defer buffer.lock.Unlock()
var result []proxy.Span
for _, span := range buffer.spans {
if span.Id != 0 {
result = append(result, span)
}
}
return result
}