wal

package
v0.9.20 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2026 License: Apache-2.0 Imports: 15 Imported by: 1 module, 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 wal is a durable write-ahead log for asynchronous sinks. Records are JSON-appended to an active segment file; a segment that hits the line (or byte) cap is rotated and handed to the sink's flush function, and is deleted only once that flush succeeds. Segments left by a previous run are replayed by Recover, so a crash costs only what never reached the disk.

The queue knows nothing about the sink: the record type is a type parameter and the remote write is the injected flush function, so it is testable without a server.

Out of scope: deduplication of replayed segments (idempotency is the sink's concern), fsync-level durability (a power cut may lose the last OS-buffered window), and multi-node coordination — each process owns its own directory.

Index

Constants

View Source
const (
	// ActiveName is the file records are appended to before rotation.
	ActiveName = "active.jsonl"
	// SegmentGlob matches the rotated, not-yet-flushed segments.
	SegmentGlob = "segment-*.jsonl"
)

Variables

This section is empty.

Functions

func CountLines

func CountLines(path string) (int, error)

CountLines returns the number of records in a WAL file. A missing file counts as zero.

Types

type Config

type Config struct {
	// Dir is the directory holding the active segment and the pending ones.
	Dir string

	// MaxLines rotates the active segment once it holds this many records.
	MaxLines int

	// MaxBytes rotates the active segment once it reaches this size,
	// whichever comes first with MaxLines. 0 disables the byte cap.
	MaxBytes int

	// FlushInterval is how often the background goroutine rotates and
	// flushes pending segments.
	FlushInterval time.Duration

	// MaxSegments caps how many pending segments may accumulate on disk.
	// When exceeded the oldest are dropped and counted in Dropped(). 0
	// disables the cap.
	MaxSegments int

	// Kind labels the flush-lag metric and this queue's log lines
	// ("usage", "payload").
	Kind string

	Logger *slog.Logger
}

Config holds the queue's tunables. Dir, FlushInterval, MaxLines and Logger are required; MaxBytes and MaxSegments may be left at zero to disable the byte cap and the disk cap respectively.

type Queue

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

Queue is the WAL for records of type T.

func Open

func Open[T any](cfg Config, flush func([]T) error) (*Queue[T], error)

Open creates the directory, opens the active segment, and starts the background rotate/flush ticker. Call Recover once afterwards to drain segments left by a previous run — the constructor does not, so callers can finish wiring the sink in between.

func (*Queue[T]) Close

func (q *Queue[T]) Close() error

Close stops the background goroutine, rotates the active segment, and runs a final flush pass. Unacknowledged segments remain on disk for the next boot to recover.

func (*Queue[T]) Dropped

func (q *Queue[T]) Dropped() uint64

Dropped returns the number of segments dropped due to MaxSegments enforcement.

func (*Queue[T]) FlushPending

func (q *Queue[T]) FlushPending()

FlushPending finds all pending segments and flushes them oldest-first. Before flushing it enforces MaxSegments by dropping the oldest excess. A segment whose flush fails is left on disk for the next pass.

func (*Queue[T]) Recover

func (q *Queue[T]) Recover()

Recover drains any segments left from a previous run (including an active segment that was never cleanly rotated).

func (*Queue[T]) StopBackground

func (q *Queue[T]) StopBackground()

StopBackground stops the rotate/flush ticker without a final flush — the crash-equivalent shutdown. Close is the normal path.

func (*Queue[T]) Write

func (q *Queue[T]) Write(rec T) error

Write appends rec to the active segment. Rotation happens synchronously when MaxLines (or MaxBytes) is reached.

Jump to

Keyboard shortcuts

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