batcher

package
v1.4.7 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2019 License: MIT, MIT Imports: 2 Imported by: 0

README

batcher

Build Status GoDoc Code of Conduct

The batching resiliency pattern for golang.

Creating a batcher takes two parameters:

  • the timeout to wait while collecting a batch
  • the function to run once a batch has been collected

You can also optionally set a prefilter to fail queries before they enter the batch.

b := batcher.New(10*time.Millisecond, func(params []interface{}) error {
	// do something with the batch of parameters
	return nil
})

b.Prefilter(func(param interface{}) error {
	// do some sort of sanity check on the parameter, and return an error if it fails
	return nil
})

for i := 0; i < 10; i++ {
	go b.Run(i)
}

Documentation

Overview

Package batcher implements the batching resiliency pattern for Go.

Index

Examples

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
}

Batcher implements the batching resiliency pattern

Example
b := New(10*time.Millisecond, func(params []interface{}) error {
	// do something with the batch of parameters
	return nil
})

b.Prefilter(func(param interface{}) error {
	// do some sort of sanity check on the parameter, and return an error if it fails
	return nil
})

for i := 0; i < 10; i++ {
	go b.Run(i)
}
Output:

func New

func New(timeout time.Duration, doWork func([]interface{}) error) *Batcher

New constructs a new batcher that will batch all calls to Run that occur within `timeout` time before calling doWork just once for the entire batch. The doWork function must be safe to run concurrently with itself as this may occur, especially when the timeout is small.

func (*Batcher) Prefilter

func (b *Batcher) Prefilter(filter func(interface{}) error)

Prefilter specifies an optional function that can be used to run initial checks on parameters passed to Run before being added to the batch. If the prefilter returns a non-nil error, that error is returned immediately from Run and the batcher is not invoked. A prefilter cannot safely be specified for a batcher if Run has already been invoked. The filter function specified must be concurrency-safe.

func (*Batcher) Run

func (b *Batcher) Run(param interface{}) error

Run runs the work function with the given parameter, possibly including it in a batch with other calls to Run that occur within the specified timeout. It is safe to call Run concurrently on the same batcher.

Jump to

Keyboard shortcuts

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