-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathringchan.go
More file actions
41 lines (34 loc) · 777 Bytes
/
ringchan.go
File metadata and controls
41 lines (34 loc) · 777 Bytes
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
package ringchan
type Ring[T any] struct {
// C is the channel to receive values from.
C <-chan T
// Number of dropped items due to full buffer.
Dropped int
}
// New creates a ring-buffered channel with fixed capacity from incoming channel.
// When full, new inserts will drop the oldest items to make space.
func New[T any](in <-chan T, size int) *Ring[T] {
out := make(chan T, size)
rc := &Ring[T]{
C: out,
}
go func() {
defer close(out)
for v := range in {
select {
case out <- v:
default:
// Do non-blocking receive to drop the oldest item
// if the buffer is full. This avoids blocking in case of an empty buffer.
select {
case <-out:
out <- v
default:
out <- v
}
rc.Dropped++
}
}
}()
return rc
}