Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ringchan.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ 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.
Expand All @@ -29,6 +32,7 @@ func New[T any](in <-chan T, size int) *Ring[T] {
default:
out <- v
}
rc.Dropped++
}
}
}()
Expand Down
3 changes: 3 additions & 0 deletions ringchan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ func TestRingChanBasic(t *testing.T) {
if len(got) != len(want) {
t.Fatalf("expected %v values, got %v", len(want), len(got))
}
if rc.Dropped != 2 {
t.Fatalf("expected %d values to be dropped, got %d", 2, rc.Dropped)
}
for i := range want {
if got[i] != want[i] {
t.Errorf("expected %v at index %d, got %v", want[i], i, got[i])
Expand Down
Loading