batch

package module
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2022 License: Apache-2.0 Imports: 3 Imported by: 1

README

Batch

Go Reference Go Report Card

Batcher collect a data and flush it if the batch is full or the interval is elapsed.

Installation

go get github.com/gotidy/batch

Examples

batch := New(flusher)
batch.Put("some data")

Documentation

Go Reference

License

Apache 2.0

Documentation

Index

Constants

View Source
const (
	// DefaultBatchSize is the default batch size.
	DefaultBatchSize = 100
	// DefaultFlushInterval is the default flush interval.
	DefaultFlushInterval = time.Second
)

Variables

This section is empty.

Functions

func WithBatchSize

func WithBatchSize(size int) func(*Batcher)

WithBatchSize sets batch size.

func WithFlushInterval

func WithFlushInterval(interval time.Duration) func(*Batcher)

WithFlushInterval sets flush interval.

Types

type Batcher

type Batcher struct {
	BatchSize     int
	FlushInterval time.Duration
	// contains filtered or unexported fields
}

Batcher accumulate messages in an internal buffer and flushes it if it is full or flush interval is expired.

func New

func New(f Flusher, opts ...Option) *Batcher

New creates a new batcher with defined Flusher and options. If Flusher is nil, then Batcher panic.

func (*Batcher) Close

func (b *Batcher) Close()

Close the batcher.

func (*Batcher) Put

func (b *Batcher) Put(v interface{})

Put accumulate value into internal buffer.

func (*Batcher) Recuperate

func (b *Batcher) Recuperate(batch []interface{})

Recuperate the a batch slice fo reusing. Recuperation extremely decrease memory allocations.

type Flusher

type Flusher interface {
	Flush(batch []interface{}) (recuperate bool)
}

Flusher describes the interface of the batch flusher.

type FlusherFunc

type FlusherFunc func(batch []interface{})

FlusherFunc implements the Flusher interface for the flushing function. Batch slice is recuperated automatically after returning.

func (FlusherFunc) Flush

func (f FlusherFunc) Flush(batch []interface{}) (recuperate bool)

Flush the batch.

type FlusherNoRecuperateFunc

type FlusherNoRecuperateFunc func(batch []interface{})

FlusherNoRecuperateFunc implements the Flusher interface for the flushing function. The batch slice will not be recuperated. However it can be recuperated later with Batcher.Recuperate function.

func (FlusherNoRecuperateFunc) Flush

func (f FlusherNoRecuperateFunc) Flush(batch []interface{}) (recuperate bool)

Flush the batch.

type Option

type Option func(*Batcher)

Option sets the batcher option.

Jump to

Keyboard shortcuts

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