audit

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2026 License: MIT Imports: 12 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 audit decouples security-event persistence from request handling.

The synchronous path writes every audit event to the store before the caller returns. The asynchronous path queues events in memory and flushes them from a worker, so the hot Use path does not wait for a store round trip per event.

Durability semantics are explicit: Async buffers in memory. A process crash before a flush loses queued events. Callers must call Close to drain. Spool removes even that window: a failed store write is fsynced to a local file and relayed in order once the store recovers, so an event that Append reports success for is never memory-only.

Spool is a write-ahead fallback for audit events. The healthy path is a plain passthrough to the store — the Postgres outbox and the synchronous allow-path commit keep doing their jobs. When a store write fails, the event is appended to a local JSONL segment and fsynced before Append returns, then relayed in order once the store accepts writes again.

Durability contract: an event Append reports success for is either in the store or in an fsynced file on local disk. A process crash loses nothing — the next Spool on the same directory replays leftover segments first. Only losing the disk itself (host eviction on ephemeral container fs) can drop events, and only those written inside the outage window.

Ordering: while a backlog exists, new events spool behind it rather than racing the relay — the audit record stays insertion-ordered.

Duplicates: an ambiguous store failure (timeout where the commit state is unknown) can produce a row both in the store and in the spool. Audit prefers a duplicate over a loss; spooled rows carry the original `at`.

Index

Constants

This section is empty.

Variables

View Source
var ErrClosed = errors.New("audit: closed")

ErrClosed is returned by Async.Append after Close has started.

Functions

func IsDropped

func IsDropped(err error) bool

IsDropped returns true if an Append returned because the auditor was closed or the caller's context was canceled before the event could be queued. This is a best-effort signal and should be logged; it does not make the event durable.

Types

type Async

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

Async queues events in memory and flushes them from a worker.

Backpressure: Append blocks until the queue has room, the auditor is closed, or the caller's context is canceled. If the caller's context is canceled before the event is queued, the event is dropped and ctx.Err() is returned.

func NewAsync

func NewAsync(s store.Store, cap int) *Async

NewAsync creates an async auditor that flushes when the batch is full or when no more events are immediately available in the channel.

func NewAsyncWithInterval

func NewAsyncWithInterval(s store.Store, cap int, interval time.Duration) *Async

NewAsyncWithInterval creates an async auditor that waits up to interval for the batch to fill before flushing. A zero interval flushes immediately after the channel is drained.

func (*Async) Append

func (a *Async) Append(ctx context.Context, e protocol.AuditEvent) error

func (*Async) Close

func (a *Async) Close() error

type Auditor

type Auditor interface {
	Append(ctx context.Context, e protocol.AuditEvent) error
	Close() error
}

Auditor persists security events. Implementations may be synchronous or buffered. Close must be called before the owning store is closed.

type Spool

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

Spool wraps the synchronous store path with a durable on-disk fallback.

func NewSpool

func NewSpool(s store.Store, dir string) *Spool

NewSpool creates a spooling auditor over s. dir holds the active segment and relay segments; it is created if missing, and leftover segments from a previous (crashed) instance are replayed first.

func (*Spool) Append

func (sp *Spool) Append(ctx context.Context, e protocol.AuditEvent) error

Append writes the event to the store, or to the fsynced spool when the store refuses. Returns nil once the event is durable in one of the two.

func (*Spool) Close

func (sp *Spool) Close() error

Close stops the relay, runs a final drain, and closes the segment file. Events that still cannot reach the store stay on disk for the next run.

func (*Spool) Pending

func (sp *Spool) Pending() int

Pending reports how many events sit on disk awaiting relay — the lag signal a monitor wants.

type Sync

type Sync struct {
	Store store.Store
}

Sync writes every event directly to the store.

func (*Sync) Append

func (s *Sync) Append(ctx context.Context, e protocol.AuditEvent) error

func (*Sync) Close

func (s *Sync) Close() error

Jump to

Keyboard shortcuts

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