stream

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: MIT Imports: 19 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 stream holds long-running inputs (a command's output or a tailed file) and fans their rows out to every connection that shows them.

A Hub owns one Buffer per input. Every row gets a hub-wide sequence number, and a merge buffer receives rows from its inputs under the same lock that assigns the number, so merged rows are always in seq order. Rows are immutable once published, so snapshots share them without copying.

Index

Constants

View Source
const (
	DefaultBufferSize = 2000
	DefaultBacklog    = 200
	MaxLineBytes      = 16 * 1024

	// DefaultIdleStop is how long an input keeps running with no page
	// subscribed. Short tab closes keep history; an abandoned input (a page
	// closed for good, or a cmd edited under --watch) stops and frees its rows.
	DefaultIdleStop = 5 * time.Minute
)

Defaults for InputSpec fields left at their zero value.

Variables

View Source
var Formats = []string{"auto", "json", "logfmt", "lines"}

Formats lists the accepted values of InputSpec.Format.

Functions

This section is empty.

Types

type Buffer

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

Buffer is a ring of the newest rows of one input or merge.

func (*Buffer) Snapshot

func (b *Buffer) Snapshot() []Row

Snapshot returns the buffered rows, oldest first. The slice and its rows are shared with other callers and must not be modified.

func (*Buffer) Subscribe

func (b *Buffer) Subscribe() *Subscription

Subscribe starts the input if it isn't running and returns a subscription whose channel holds at most one pending wake-up.

type Hub

type Hub struct {

	// IdleStop is how long an input runs after its last subscriber leaves.
	IdleStop time.Duration
	// contains filtered or unexported fields
}

Hub owns every Buffer of one server.

func NewHub

func NewHub() *Hub

NewHub returns an empty hub. Inputs start lazily on their first Subscribe.

func (*Hub) Close

func (h *Hub) Close() error

Close stops every input, kills their process groups and waits for them.

func (*Hub) Input

func (h *Hub) Input(spec InputSpec) (*Buffer, error)

Input returns the buffer for spec, creating it on first use. Two calls with equal specs share one buffer; any difference gets a buffer of its own.

func (*Hub) Merge

func (h *Hub) Merge(name string, inputs []*Buffer, size int) (*Buffer, error)

Merge returns the buffer that interleaves inputs in seq order, creating it on first use and seeding it with the inputs' current rows. Subscribing to it starts every input. Inputs must be Input buffers, not other merges.

type InputSpec

type InputSpec struct {
	Name    string            `json:"name"` // source name, copied into each row's "source"
	Cmd     string            `json:"cmd"`  // run with /bin/sh -c
	Dir     string            `json:"dir"`  // working directory for Cmd
	Env     map[string]string `json:"env"`  // extra environment for Cmd
	File    string            `json:"file"` // absolute path to tail
	Format  string            `json:"format"`
	Buffer  int               `json:"buffer"`  // rows kept
	Backlog int               `json:"backlog"` // lines read from the end of File on start
	Restart string            `json:"restart"` // on-exit or never
}

InputSpec describes one stream input. Exactly one of Cmd or File is set.

func (InputSpec) Normalize

func (s InputSpec) Normalize() (InputSpec, error)

Normalize fills defaults and rejects invalid specs. Input calls it, and config validation can call it to report errors before serving.

type Row

type Row = map[string]interface{}

Row is one published log row. It must not be modified after publish.

type Subscription

type Subscription struct {
	C <-chan struct{}
	// contains filtered or unexported fields
}

Subscription delivers a wake-up on C whenever its buffer gains rows.

func (*Subscription) Close

func (s *Subscription) Close()

Close stops wake-ups. The input keeps running for other subscribers, and for the hub's IdleStop after the last one leaves. Close is idempotent.

Jump to

Keyboard shortcuts

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