kvwatch

package
v0.1.0-alpha.5 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package kvwatch stops a NATS KV watch and waits for its delivery to close, so a caller that returns after stopping its watch leaves no nats.go delivery running behind it. Each caller wraps Stop's error with the bucket it watches.

Index

Constants

View Source
const DrainBudget = 5 * time.Second

DrainBudget bounds Stop's wait for the watch's delivery to close, counted from when the watcher's own Stop returns, whether or not the caller's context has ended. Time in that Stop is not counted.

Variables

View Source
var ErrDeliveryOpen = errors.New("kvwatch: the watch's delivery did not close after Stop")

ErrDeliveryOpen says the watch's update channel was still open when DrainBudget ran out.

Functions

func Stop

func Stop(ctx context.Context, watcher jetstream.KeyWatcher) error

Stop stops the watch and drains its updates until nats.go closes the channel, for at most DrainBudget after the watcher's own Stop returns; ctx's cancellation does not cut the drain short. Entries read here are dropped. At nats.go v1.55.0 Stop only unsubscribes: a delivery already blocked on the full updates buffer ends only when the buffer is read, and the channel closes after it, so on that path returning is a join (graph/follower's TestFollowerRunReturnsAndLeavesNothing drives it with a backlogged watch). The bound is for the one path with nothing to join: WatchAll subscribes before it installs the handler that closes the channel (jetstream/kv.go:1356-1368), and nats.go reads that handler once, when its delivery goroutine exits (nats.go:3830-3834). A watch whose context ends, or whose connection closes, in between leaves the channel open for good with no delivery goroutine running. TestStopGivesUpOnADeliveryThatNeverCloses drives the bound on a scripted watcher; the window itself has not been reproduced against nats.go.

Stop returns nil, or the watch's Stop error when it says more than that the watch was already gone. When the budget runs out it returns ErrDeliveryOpen, joined with the watch's Stop error if there was one. A nil context is refused before the watcher is touched.

Types

This section is empty.

Jump to

Keyboard shortcuts

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