batcher

package module
v0.0.0-...-628c7b8 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2021 License: MIT Imports: 1 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

batcher

Godoc Go Report Card

Package batcher provides mechanism to perform batch operation.

Inspired by go-batcher.

Install

go get github.com/aperdana/batcher

Usage

Below is an example on how to use batcher. Do check out godoc reference for more info.

package main

import (
	"fmt"
	"time"

	"github.com/aperdana/batcher"
)

func main() {
	// Create a new Batcher
	b := batcher.New(func(batched []interface{}) {
		if len(batched) > 0 {
			fmt.Printf("Here's the batched items: %v\n", batched)
		}
	}, 100*time.Millisecond)

	// Starts listening for items
	b.Listen()

	// Batch item
	b.Batch("I")
	b.Batch("Am")
	b.Batch("A")
	b.Batch("Gopher")

	time.Sleep(110 * time.Millisecond)

	b.Batch("2nd batch")

	time.Sleep(100 * time.Millisecond)

	// Output:
	// Here's the batched items: [I Am A Gopher]
	// Here's the batched items: [2nd batch]
}

Documentation

Overview

Package batcher provides mechanism to perform batch operation.

Example
package main

import (
	"fmt"
	"time"

	"github.com/aperdana/batcher"
)

func main() {
	// Create a new Batcher
	b := batcher.New(func(batched []interface{}) {
		if len(batched) > 0 {
			fmt.Printf("Here's the batched items: %v\n", batched)
		}
	}, 100*time.Millisecond)

	// Starts listening for items
	b.Listen()

	// Batch item
	b.Batch("I")
	b.Batch("Am")
	b.Batch("A")
	b.Batch("Gopher")

	time.Sleep(110 * time.Millisecond)

	b.Batch("2nd batch")

	time.Sleep(100 * time.Millisecond)

}
Output:
Here's the batched items: [I Am A Gopher]
Here's the batched items: [2nd batch]

Index

Examples

Constants

View Source
const (
	// DefaultBufferSize is Batcher's default internal buffer size.
	DefaultBufferSize = 10
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Batcher

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

Batcher is a time-based batcher. When SetMaxBatchSize is used when initializing new Batcher, it will also add size as constraint.

func New

func New(do func([]interface{}), timeout time.Duration, opts ...Option) *Batcher

New returns Batcher that runs do on batched items every timeout.

func (*Batcher) Batch

func (b *Batcher) Batch(m interface{})

Batch sends m to be batched. Listen() must be called first before performing any Batch(), otherwise it will panic.

It is safe to use Batch concurrently.

func (*Batcher) Listen

func (b *Batcher) Listen()

Listen starts a message listener in background.

type Option

type Option func(*Batcher)

Option used to control Batcher

func SetBufferSize

func SetBufferSize(s int) Option

SetBufferSize sets Batcher's internal buffer size. As buffer size increases, Batcher's ability to concurrently handles Batch() also increases.

func SetMaxBatchSize

func SetMaxBatchSize(s int) Option

SetMaxBatchSize sets maximum batch size for every round of batching. When s > 0, Batcher will not only work as a time-limited batcher, but also size-limited.

Jump to

Keyboard shortcuts

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