eventbuffer

package
v1.17.1 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: MPL-2.0 Imports: 10 Imported by: 0 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 eventbuffer implements a circular buffer of watch events shared by many event feeds.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Buffer

type Buffer struct {
	// contains filtered or unexported fields
}

Buffer is a circular buffer of events shared by many feeds.

Every event producer (e.g. a resource collection) publishes its events to its own Feed, but all feeds are backed by the same circular buffer, so that the memory is shared across the producers instead of being reserved per producer.

Events of a single feed are linked together (see entry.prevPos), so a watcher only ever walks the events of the feed it is subscribed to, no matter how many unrelated events were published in the meantime. Watchers are woken up only when an event is published to their feed.

Event bookmarks are global positions in the buffer, shared by all feeds (see encodeBookmark).

func New

func New(initialCapacity, maxCapacity, gap int) *Buffer

New creates a new event Buffer.

The buffer starts with initialCapacity slots and grows up to maxCapacity as the events are published. The gap is the number of slots reserved as a safety margin between the watchers and the write position: new watchers never start from the events within the gap.

The arguments are clamped to the range the buffer can operate in, so that a misconfigured buffer degrades into a shallow history instead of failing at the publish time.

func (*Buffer) Cleanup

func (buf *Buffer) Cleanup() int

Cleanup drops the events which are of no use to any watch, and returns the number of the events it dropped.

An event is dropped once it is consumed by every watcher of its feed and it was already published as of the previous sweep. The buffer keeps the slot itself (the capacity never shrinks), it only releases the event, so that the resources it references can be garbage collected.

The dropped events can no longer be replayed, so the watches which try to start from them fail the same way they do for the events pushed out of the buffer: a bookmark pointing at a dropped event is rejected with an invalid bookmark error, and TailEvents rewinds only as far as the events which are still there.

func (*Buffer) NewFeed

func (buf *Buffer) NewFeed(ns resource.Namespace, typ resource.Type) *Feed

NewFeed creates a new event feed backed by the buffer.

The namespace and the type are only used to describe the feed in the error messages.

func (*Buffer) NumFeeds

func (buf *Buffer) NumFeeds() int

NumFeeds returns the number of the feeds registered with the buffer.

The cleanup walks every feed ever created, so this is the metric which tells whether the feeds are created once per event producer.

func (*Buffer) RunCleanup

func (buf *Buffer) RunCleanup(ctx context.Context, interval time.Duration)

RunCleanup sweeps the buffer on every tick of the interval until the context is canceled.

type Feed

type Feed struct {
	// contains filtered or unexported fields
}

Feed is a view of the shared event buffer for a single event producer.

All fields but the immutable buf/ns/typ are protected by the buffer mutex.

func (*Feed) Publish

func (f *Feed) Publish(event state.Event)

Publish an event to the feed.

Publish overwrites the event bookmark, and it wakes up the watchers of this feed only.

func (*Feed) Subscribe

func (f *Feed) Subscribe(opts SubscribeOptions) (*Watcher, error)

Subscribe registers a new watcher of the feed.

Subscribe returns an error if the bookmark passed in the options is not valid for this feed.

type SubscribeOptions

type SubscribeOptions struct {
	// Filter, if set, selects the events delivered by the watcher.
	//
	// Filter is called on a copy of the event, so it might rewrite the event fields (but not the
	// resources the event references). It is called with the buffer mutex held, so it should be cheap.
	Filter func(*state.Event) bool

	// TailFilter, if set, selects the events counted by TailEvents.
	//
	// TailFilter should not modify the event.
	TailFilter func(*state.Event) bool

	// Bookmark, if set, starts the watcher right after the event the bookmark points to.
	Bookmark state.Bookmark
	// TailEvents, if positive, rewinds the watcher to deliver that many events which are still
	// available in the buffer.
	TailEvents int
}

SubscribeOptions configure the starting position of a new Watcher.

With no options set the watcher starts from the events published after the subscription.

type Watcher

type Watcher struct {
	// contains filtered or unexported fields
}

Watcher is a single subscriber of a Feed.

func (*Watcher) Close

func (w *Watcher) Close()

Close removes the watcher from its feed.

The watcher can't be used after it is closed.

func (*Watcher) Next

func (w *Watcher) Next(ctx context.Context) ([]state.Event, error)

Next returns the events published to the watcher's feed since the previous call.

Next blocks until at least a single event passing the watcher filter is available, and it returns nil events and nil error if the context gets canceled while waiting.

Next returns an error if the watcher missed the events which were pushed out of the buffer.

func (*Watcher) StartBookmark

func (w *Watcher) StartBookmark() state.Bookmark

StartBookmark returns the bookmark of the last event the watcher is considered to have seen at the subscription time.

Jump to

Keyboard shortcuts

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