ring

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2026 License: MIT Imports: 3 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

func From[T any](ctx context.Context, in <-chan T, size int) *Receiver[T]

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%

func (*Receiver[T]) Dropped

func (r *Receiver[T]) Dropped() uint64

Dropped reports how many values were discarded to make room for newer ones.

type Ring

type Ring[T any] struct {
	Receiver[T]
	// contains filtered or unexported fields
}

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

func New[T any](size int) *Ring[T]

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL