batching

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package batching provides a write-coalescing decorator for the EntityStateRepository port. It collects Ingest calls into an in-memory buffer and flushes them to the underlying store as a single Save transaction whenever the buffer fills up, the deadline fires, or Close is invoked.

Use case: streaming adapters that submit one observation per gRPC/HTTP call against a SQLite backend. Per-call commits cost one fsync each; batching coalesces N writes into one fsync, trading some per-write latency for substantially higher write throughput.

The decorator is opt-in via the operator config; default is "no batching", preserving the original per-call commit semantics for deployments that prefer low latency over throughput.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	MaxBatch int
	MaxWait  time.Duration
}

Config tunes the batcher. Both must be positive; otherwise New returns an error rather than silently disabling.

type Repo

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

Repo wraps an upstream EntityStateRepository with a buffered Ingest path. Reads and the explicit batch Save bypass the buffer and go straight to the upstream — this matches Chronos's design that reads should always see a consistent snapshot, while writes can tolerate small batching latency.

func New

func New(upstream ports.EntityStateRepository, cfg Config) (*Repo, error)

New wraps upstream with batched-Ingest semantics. Callers must call Close when shutting down to drain the buffer; otherwise the most recent buffered writes are lost.

func (*Repo) Close

func (r *Repo) Close(ctx context.Context) error

Close drains the buffer and prevents further Ingest calls. Reads remain available because they go straight to the upstream.

func (*Repo) Count

func (r *Repo) Count(ctx context.Context, adapterName string) (int64, error)

Count passes through to upstream.

func (*Repo) DeleteOlderThan

func (r *Repo) DeleteOlderThan(ctx context.Context, cutoff time.Time, adapterName string) error

DeleteOlderThan passes through to upstream.

func (*Repo) Flush

func (r *Repo) Flush(ctx context.Context) error

Flush drains the buffer immediately. Safe to call concurrently with Ingest; the timer is also driven through this path.

func (*Repo) Ingest

func (r *Repo) Ingest(ctx context.Context, adapterName string, state chronos.EntityState) error

Ingest buffers the state. The flush is triggered when the buffer hits maxBatch; otherwise a timer drains it at maxWait. Returns an error only when validation fails — successful buffering is reported as nil even though no row has been persisted yet.

func (*Repo) ListByEntity

func (r *Repo) ListByEntity(ctx context.Context, entityID uuid.UUID) ([]chronos.EntityState, error)

ListByEntity passes through to upstream.

func (*Repo) ListByScope

func (r *Repo) ListByScope(ctx context.Context, scopeID uuid.UUID) ([]chronos.EntityState, error)

ListByScope passes through to upstream.

func (*Repo) ListScopes

func (r *Repo) ListScopes(ctx context.Context) ([]uuid.UUID, error)

ListScopes passes through to upstream.

func (*Repo) Save

func (r *Repo) Save(ctx context.Context, adapterName string, states []chronos.EntityState) error

Save bypasses the buffer for callers that already have a batch.

Jump to

Keyboard shortcuts

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