Documentation
¶
Overview ¶
Package logsink provides an asynchronous, batching io.Writer decorator for the process log.
Why ¶
Culvert's process logger is composed as
log.New(io.MultiWriter(os.Stdout, *fileutil.RotatingFile), "[Culvert] ", log.LstdFlags)
and it is written from the proxy REQUEST goroutine: handleRequest emits one line per proxied request (POLICY_ALLOW / POLICY_BLOCK / POLICY_DROP / POLICY_DEFAULT_DENY / …). That coupled proxy latency to log-sink latency twice over:
- log.Logger holds its own mutex ACROSS the destination Write, and
- fileutil.RotatingFile holds a second mutex across its write(2) and across the remove/rename/reopen rotation stall.
Both writes are unbuffered, so every allowed request paid two syscalls inside a process-global critical section. Measured on the pre-change composition (4-core Xeon @2.10GHz, stdout redirected to a file, the verbatim POLICY_ALLOW format string):
sink serial 4x parallel format only 509 ns/op 155 ns/op (scales: no shared state) stdout+file 2798 ns/op 3204 ns/op (does NOT scale: gets WORSE)
A hot path that gets slower as cores are added is a throughput ceiling, not a constant cost: the sink capped the whole proxy at ~310k logged requests/sec regardless of how many cores the gateway has.
The fix ¶
This is the pattern the codebase already applies to its other per-request sinks — internal/reqlog (JSONL request log), internal/logstore (queryable history) and internal/syslog (SIEM): callers enqueue on a bounded channel and ONE drain goroutine owns the destination writer. The request goroutine's critical section becomes a buffer copy plus a channel send.
Contract ¶
The queue is a shock absorber, NOT a load shedder. logstore and syslog drop on a full queue; the process log must not — it is the operator's record of what the gateway did. A full queue therefore BLOCKS the caller, which is exactly the pre-change behavior, so this is never worse than the synchronous write it replaces: bursts up to queueDepth are absorbed completely, and only sustained saturation (the sink genuinely cannot keep up) reverts to the old coupling. Saturation is counted and surfaced via Backpressure() so the degraded state is visible rather than silent.
Ordering is preserved: a single drain goroutine writes every line, so the log stays strictly FIFO and the bytes written per line are unchanged.
Durability: the drain loop flushes as soon as the queue is empty, so an idle process still gets one write(2) per line and log visibility is not delayed. Only a backlog batches, and only for as long as the backlog lasts. What is genuinely new is that an abrupt process death (SIGKILL, OOM) can lose the in-flight batch; the orderly paths do not, because Close flushes and package main's fatal helper flushes before os.Exit.
Re-entrancy: this package is the process log's sink, so it must never log through it. Its own diagnostics go straight to os.Stderr.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
Writer is an asynchronous, batching io.Writer decorator. Its Write is safe for concurrent use and does not touch the destination writer; a single background goroutine owns that. The zero value is not usable — call New.
func New ¶
New returns a Writer that forwards to dst from a single background goroutine and starts that goroutine. The caller must Close it to flush and stop the goroutine; Close does not close dst.
func (*Writer) Backpressure ¶
Backpressure reports how many lines had to wait for room in the queue. Non-zero means the sink is not keeping up with the log rate and request latency is once again coupled to sink latency — the signal to move the log to a faster volume or raise the log level. No line is ever dropped, so this is a saturation gauge, not a loss counter.
func (*Writer) Close ¶
Close drains everything still queued, flushes it, and stops the drain goroutine. It does NOT close the destination writer — the caller owns that. Idempotent and safe to call concurrently with Write.
func (*Writer) Sync ¶
func (w *Writer) Sync()
Sync blocks until every line enqueued before the call has been flushed to the destination. It is the deterministic alternative to sleeping: shutdown uses Close (which drains), the fatal-exit path uses Sync, and tests that assert on sink contents call it.
ONE deadline covers BOTH phases — reaching the drain goroutine and waiting for it to finish flushing — so a sink that wedges inside Flush cannot hang the caller forever. Returning without the guarantee is strictly better than deadlocking a shutdown hook.
func (*Writer) Write ¶
Write copies p and hands it to the drain goroutine. It always reports the full length written and a nil error: log.Logger discards Write errors from Printf anyway, and reporting a short write would make it retry. Genuine sink failures are counted and surfaced via WriteErrors.
The copy is mandatory, not defensive: log.Logger reuses one internal buffer across calls, so the bytes behind p are overwritten as soon as Write returns.
func (*Writer) WriteErrors ¶
WriteErrors reports how many lines failed to reach the destination writer.