batch

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package batch provides a generic worker that accumulates items and flushes them in batches.

A batch is flushed when it reaches the configured size or when the flush interval elapses, whichever comes first. The caller supplies a Writer that persists each flushed batch (bulk insert, API call, …).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Options

type Options struct {
	Interval    time.Duration // Interval after which writing happens
	MaxItems    int           // MaxItems specifies number of items in a queue to be written
	MaxCapacity int           // MaxCapacity capacity of channel before locking
}

type Worker

type Worker[T any] interface {
	// Start starts worker
	Start(ctx context.Context)
	// Write calls to pass a single item to be written
	Write(ctx context.Context, item *T)
	// Close closes worker
	Close(ctx context.Context)
}

Worker allows writing data in a batch manner events to write fire either by time interval or by number of items waiting to be written

func NewBatchWorker

func NewBatchWorker[T any](writer Writer[T], opt *Options, logger jet.CLoggerFunc) Worker[T]

type Writer

type Writer[T any] interface {
	// Write writes batch items somewhere
	Write(ctx context.Context, items []*T) error
}

Writer interface must be implemented and passed by calling side

Jump to

Keyboard shortcuts

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