Writing
Durability modes and the batcher.
Durability
Durability selects what an ingest call means by “done”: Acked blocks for the server’s fsynced-WAL receipt; FireAndForget returns once the frame is written and a refused batch vanishes without signal. The zero value is Acked — the safe meaning is the one you get by not choosing.
type Durability uint8
Durability selects what an ingest call means by “done”.
It replaces a bare waitAck bool (retired 2026-09-07): a boolean made fire-and-forget an easy accident — false reads like a default, and what it actually means is “no receipt, and a refused batch vanishes without any signal back”. A caller now writes the semantics they want by name.
const (
// DurabilityAcked blocks until the server's INGEST_ACK, which is a
// durability receipt: the batch is in the write-ahead log AND the log is
// fsynced before the ack is sent. The zero value on purpose — the safe
// meaning is the one you get by not choosing.
DurabilityAcked Durability = iota
// DurabilityFireAndForget returns once the frame is written to the
// socket. No receipt exists: a batch the server refuses is dropped with
// no signal back. For producers that prefer throughput to certainty.
DurabilityFireAndForget
)
func (d Durability) String() string
String names the mode for errors and logs.
Ingester
Ingester is the write surface a Batcher sends through. Both Client and ResilientClient satisfy it, so a feed handler can batch AND survive reconnects at once.
type Ingester interface {
IngestCandles(context.Context, StreamID, []Candle, Durability) error
IngestTicks(context.Context, StreamID, []Tick, Durability) error
}
Ingester is the write surface a Batcher needs, which both Client and ResilientClient satisfy. Taking the interface is what lets a feed handler batch AND survive reconnects at once — the combination anyone ingesting a live market feed actually wants, and it has to be EXPORTED for the public SDK to offer that combination at all: a package-level constructor cannot name an unexported parameter type its callers are able to satisfy.
Batcher
Batcher accumulates records for one stream and sends them as batches, flushing on whichever threshold is reached first. Build one with NewCandleBatcher or NewTickBatcher.
Batching converts a synchronous durability receipt into an asynchronous one: Add returns once the record is BUFFERED, so a failed flush surfaces at a later Add, at Flush, or at Close. Always check Close — it is the only point a caller is guaranteed to hear about a background flush that failed, and it is what sends the last partial batch.
func NewCandleBatcher(c Ingester, streamID StreamID, opts BatchOptions) (*Batcher[Candle], error)
func NewTickBatcher(c Ingester, streamID StreamID, opts BatchOptions) (*Batcher[Tick], error)
type Batcher[T any] struct {
// Has unexported fields.
}
Batcher accumulates records for ONE stream and sends them as batches.
b, err := NewCandleBatcher(c, streamID, BatchOptions{})
defer b.Close()
for candle := range feed {
if err := b.Add(ctx, candle); err != nil { ... }
}
return b.Close() // the last partial batch, and the final verdict
Safe for concurrent use: a feed handler fanning out over goroutines is the normal case. One stream per Batcher — a caller with many symbols makes many, which costs one goroutine each and is the right trade in Go.
ALWAYS check the error from Close. It is the only place a caller is guaranteed to learn about a flush that failed after the Add that caused it returned.
func NewCandleBatcher(
c Ingester, streamID StreamID, opts BatchOptions,
) (*Batcher[Candle], error)
NewCandleBatcher returns a Batcher that sends candles for streamID.
A free function rather than a method on Client because Go does not allow methods to introduce type parameters, and the two record kinds share one implementation — the alternative is two near-identical types, which is the duplication this repo keeps tracing its bugs back to.
func NewTickBatcher(
c Ingester, streamID StreamID, opts BatchOptions,
) (*Batcher[Tick], error)
NewTickBatcher returns a Batcher that sends ticks for streamID.
func (b *Batcher[T]) Add(ctx context.Context, records ...T) error
Add buffers records, flushing if that reaches MaxRecords. It returns the error from THAT flush, or a previous background flush that failed; a nil return means the records are buffered, not that they are stored.
func (b *Batcher[T]) Close() error
Close stops the timer loop, sends the last partial batch, and returns the first error this batcher ever hit. Idempotent.
The deliberate choice here is that Close FLUSHES rather than discarding. Records buffered at shutdown were accepted by Add, and dropping them because the process is exiting is the data loss this type would otherwise quietly introduce.
func (b *Batcher[T]) Flush(ctx context.Context) error
Flush sends whatever is buffered, if anything.
BatchOptions
BatchOptions tunes a Batcher: MaxRecords, MaxDelay, Durability. The zero value is usable — 1,000 records, 100ms, acked.
type BatchOptions struct {
// MaxRecords flushes as soon as the batch reaches this many. Default 1000.
// It must leave the encoded batch inside the connection's frame cap; the
// default is ~49KiB of candles against a 16MiB default cap.
MaxRecords int
// MaxDelay flushes when the OLDEST buffered record reaches this age, so a
// stream too quiet to fill a batch still makes progress. Default 100ms.
MaxDelay time.Duration
// Durability is applied to every flush. Default DurabilityAcked — but see
// this file's header: with batching the receipt arrives at a later call,
// not at the Add that supplied the record.
Durability Durability
}
BatchOptions tunes a Batcher. The zero value is usable: 1,000 records, 100ms, acked.