From 379916dc2bb7446f802be6d0356bf99b31b42682 Mon Sep 17 00:00:00 2001 From: Vsevolod Strukchinsky Date: Fri, 20 Jun 2025 00:51:03 +0500 Subject: [PATCH 1/2] Add Dropped field for monitoring --- ringchan.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ringchan.go b/ringchan.go index bd9262a..69f7755 100644 --- a/ringchan.go +++ b/ringchan.go @@ -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. @@ -29,6 +32,7 @@ func New[T any](in <-chan T, size int) *Ring[T] { default: out <- v } + rc.Dropped++ } } }() From d3fb1e4f257086ce5b919f9fd73c45715dc2230b Mon Sep 17 00:00:00 2001 From: Vsevolod Strukchinsky Date: Fri, 20 Jun 2025 00:52:29 +0500 Subject: [PATCH 2/2] Adjust test to check Dropeed --- ringchan_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ringchan_test.go b/ringchan_test.go index ce6de36..1f3f8bc 100644 --- a/ringchan_test.go +++ b/ringchan_test.go @@ -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])