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 ¶
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 ¶
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, counted in Dropped(), and their
// records in relay_records_lost_total{kind=Kind}. 0 disables the cap.
MaxSegments int
// FlushOnRotate hands a segment to the flush function as soon as it
// rotates on a full cap instead of on the next tick, so a busy queue
// flushes in segment-sized steps rather than one tick-sized burst. After a
// failed flush only the tick retries.
FlushOnRotate bool
// 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 ¶
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 ¶
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 ¶
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 ¶
Write appends rec to the active segment. Rotation happens synchronously when MaxLines (or MaxBytes) is reached.
func (*Queue[T]) WriteBatch ¶ added in v0.11.7
WriteBatch appends recs in order under one lock and flushes once, rotating mid-batch wherever Write would have. A record that fails to marshal is skipped and reported in the returned error; the rest are still written.