batcher

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2019 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package batcher supports batching of items. Create a Batcher with a handler and add items to it. Items are accumulated while handler calls are in progress; when the handler returns, it will be called again with items accumulated since the last call. Multiple concurrent calls to the handler are supported.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Batcher

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

A Batcher batches items.

func New

func New(itemType reflect.Type, maxHandlers int, handler func(interface{}) error) *Batcher

New creates a new Batcher.

itemType is type that will be batched. For example, if you want to create batches of *Entry, pass reflect.TypeOf(&Entry{}) for itemType.

maxHandlers is the maximum number of handlers that will run concurrently.

handler is a function that will be called on each bundle. If itemExample is of type T, the argument to handler is of type []T.

func (*Batcher) Add

func (b *Batcher) Add(ctx context.Context, item interface{}) error

Add adds an item to the batcher. It blocks until the handler has processed the item and reports the error that the handler returned. If Shutdown has been called, Add immediately returns an error.

func (*Batcher) AddNoWait

func (b *Batcher) AddNoWait(item interface{}) <-chan error

AddNoWait adds an item to the batcher and returns immediately. When the handler is called on the item, the handler's error return value will be sent to the channel returned from AddNoWait.

func (*Batcher) Shutdown

func (b *Batcher) Shutdown()

Shutdown waits for all active calls to Add to finish, then returns. After Shutdown is called, all subsequent calls to Add fail. Shutdown should be called only once.

Jump to

Keyboard shortcuts

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