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 ¶
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.
type Option ¶
type Option func(*Batcher)
Option used to control Batcher
func SetBufferSize ¶
SetBufferSize sets Batcher's internal buffer size. As buffer size increases, Batcher's ability to concurrently handles Batch() also increases.
func SetMaxBatchSize ¶
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.