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 ¶
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 ¶
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 ¶
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.
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.
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 ¶
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 ¶
StartBookmark returns the bookmark of the last event the watcher is considered to have seen at the subscription time.