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 ¶
var ErrClosed = errors.New("audit: closed")
ErrClosed is returned by Async.Append after Close has started.
Functions ¶
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 ¶
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 ¶
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.
type Auditor ¶
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 ¶
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 ¶
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.