batcher

package
v1.66.0 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package batcher implements a generic batcher.

It uses two types:

Item - the thing to be batched
Result - the result from the batching

And one function of type CommitBatchFn which is called to do the actual batching.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Batcher

type Batcher[Item, Result any] struct {
	// contains filtered or unexported fields
}

Batcher holds info about the current items waiting to be acted on.

func New

func New[Item, Result any](ctx context.Context, f any, commit CommitBatchFn[Item, Result], opt Options) (*Batcher[Item, Result], error)

New creates a Batcher for Item and Result calling commit to do the actual committing.

func (*Batcher[Item, Result]) Batching

func (b *Batcher[Item, Result]) Batching() bool

Batching returns true if batching is active

func (*Batcher[Item, Result]) Commit

func (b *Batcher[Item, Result]) Commit(ctx context.Context, name string, item Item) (entry Result, err error)

Commit commits the Item getting a Result or error using a batch call, first adding it to the batch and then waiting for the batch to complete in a synchronous way if async is not set.

If async is set then this will return no error and a nil/empty Result.

This should not be called if batching is off - check first with IsBatching.

func (*Batcher[Item, Result]) Shutdown

func (b *Batcher[Item, Result]) Shutdown()

Shutdown finishes any pending batches then shuts everything down.

This is registered as an atexit handler by New.

type CommitBatchFn

type CommitBatchFn[Item, Result any] func(ctx context.Context, items []Item, results []Result, errors []error) (err error)

CommitBatchFn is called to commit a batch of Item and return Result to the callers.

It should commit the batch of items then for each result i (of which there should be len(items)) it should set either results[i] or errors[i]

type Options

type Options struct {
	Mode                  string        // mode of the batcher "sync", "async" or "off"
	Size                  int           // size of batch
	Timeout               time.Duration // timeout before committing the batch
	MaxBatchSize          int           // max size the batch can be
	DefaultTimeoutSync    time.Duration // default time to kick off the batch if nothing added for this long (sync)
	DefaultTimeoutAsync   time.Duration // default time to kick off the batch if nothing added for this long (async)
	DefaultBatchSizeAsync int           // default batch size if async
}

Options for configuring the batcher

func (*Options) FsOptions

func (opt *Options) FsOptions(extra string) []fs.Option

FsOptions returns the batch mode fs.Options

Jump to

Keyboard shortcuts

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