Documentation
¶
Overview ¶
Package ring provides a bounded channel that never blocks its sender: when the buffer is full, a send discards the oldest value to make room.
It suits streams where the newest values matter most, such as metrics, progress reports or UI state, and a slow receiver must not stall the sender.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Receiver ¶
type Receiver[T any] struct { // C delivers the values in the order they were sent. It is closed when the // ring is closed; values still buffered at that point can be received // before the channel reports that it is closed. C <-chan T // contains filtered or unexported fields }
Receiver is the receiving side of a ring: its values and the count of those it dropped. From returns one, and every Ring embeds one. The zero value is not usable.
func From ¶
From creates a ring of the given size and starts a goroutine that sends every value received from in to it. When in is closed or ctx is done, the goroutine stops reading in and closes the ring.
The ring belongs to that goroutine, so From returns only its receiving side. It panics if size is less than 1.
Example ¶
package main
import (
"context"
"fmt"
"github.com/floatdrop/chan/ring"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel() // Stops From if the loop below exits early.
progress := make(chan int)
go func() {
defer close(progress)
for pct := range 101 {
progress <- pct
}
}()
// The producer is never held up by a slow receiver; the receiver sees the
// latest updates and skips the ones it was too slow for.
latest := ring.From(ctx, progress, 1)
last := 0
for pct := range latest.C {
last = pct // Render the progress bar.
}
fmt.Printf("finished at %d%%\n", last) // The newest value is never dropped.
}
Output: finished at 100%
type Ring ¶
Ring is a buffered channel that keeps the newest values. Ring.Send never blocks: when the buffer is full, it discards the oldest value first.
Values are received from C, which Ring has from its embedded Receiver. Like a channel, a Ring may be used by several senders and receivers at once and is closed by a sender: Send and Close panic once the Ring is closed.
A Ring must be created with New: Send panics on the zero value.
func New ¶
New returns an empty Ring that holds up to size values. It panics if size is less than 1.
Example ¶
package main
import (
"fmt"
"github.com/floatdrop/chan/ring"
)
func main() {
r := ring.New[int](3)
for i := range 5 {
r.Send(i) // Never blocks, even though nobody is receiving yet.
}
r.Close()
for v := range r.C {
fmt.Println(v)
}
fmt.Println("dropped:", r.Dropped())
}
Output: 2 3 4 dropped: 2
func (*Ring[T]) Close ¶
func (r *Ring[T]) Close()
Close closes C. Values already in the Ring can still be received.
func (*Ring[T]) Send ¶
func (r *Ring[T]) Send(v T)
Send adds v to the Ring without blocking. If the Ring is full, Send first discards the oldest value, which Receiver.Dropped counts.