Skip to content

Commit 9bce287

Browse files
authored
Add Dropped field for monitoring (#1)
1 parent 2d3771f commit 9bce287

2 files changed

Lines changed: 7 additions & 0 deletions

File tree

ringchan.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package ringchan
33
type Ring[T any] struct {
44
// C is the channel to receive values from.
55
C <-chan T
6+
7+
// Number of dropped items due to full buffer.
8+
Dropped int
69
}
710

811
// New creates a ring-buffered channel with fixed capacity from incoming channel.
@@ -29,6 +32,7 @@ func New[T any](in <-chan T, size int) *Ring[T] {
2932
default:
3033
out <- v
3134
}
35+
rc.Dropped++
3236
}
3337
}
3438
}()

ringchan_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ func TestRingChanBasic(t *testing.T) {
3333
if len(got) != len(want) {
3434
t.Fatalf("expected %v values, got %v", len(want), len(got))
3535
}
36+
if rc.Dropped != 2 {
37+
t.Fatalf("expected %d values to be dropped, got %d", 2, rc.Dropped)
38+
}
3639
for i := range want {
3740
if got[i] != want[i] {
3841
t.Errorf("expected %v at index %d, got %v", want[i], i, got[i])

0 commit comments

Comments
 (0)