Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Batch ¶
type Batch[T TypeConstraint] []T
Batch[T TypeConstraint] is a slice of elements used to define a processing batch. A Batch knows how to produce a clone of its elements.
type Executor ¶
type Executor[T TypeConstraint] struct { // contains filtered or unexported fields }
Executor runs an executor function over a batch of T.
The Executor handles the slicing of Push-ed input into batches of fixed size.
When the stream of input is complete, a call to Flush() executes the last (possibly incomplete) batch.
Example ¶
// nolint: forbidigo
package main
import (
"fmt"
"github.com/fredbi/go-patterns/batchers"
)
type testItem struct {
A int
}
func makeTestItems(n int) []testItem {
fixtures := make([]testItem, n)
for i := 0; i < n; i++ {
fixtures[i] = testItem{
A: i,
}
}
return fixtures
}
func main() {
// This example pushes a few test items into 2 batch executors.
const n = 42
batchExecutor := batchers.NewExecutor[testItem](10, func(in batchers.Batch[testItem]) {
if len(in) == 0 {
return
}
fmt.Printf("processing batch [%d items]: [%d-%d]\n", len(in), in[0].A, in[len(in)-1].A)
})
batchExecutorWithPointers := batchers.NewPointerExecutor[testItem](10, func(in batchers.Batch[*testItem]) {
if len(in) == 0 {
return
}
fmt.Printf("processing batch [%d pointer items]: [%d-%d]\n", len(in), in[0].A, in[len(in)-1].A)
})
for _, item := range makeTestItems(n) {
batchExecutor.Push(item)
// we actually clone immediately the value that is pointed to, so we may safely pass the iterated (constant) pointer here
batchExecutorWithPointers.Push(&item) //nolint:gosec
}
batchExecutor.Flush()
batchExecutorWithPointers.Flush()
}
Output: processing batch [10 items]: [0-9] processing batch [10 pointer items]: [0-9] processing batch [10 items]: [10-19] processing batch [10 pointer items]: [10-19] processing batch [10 items]: [20-29] processing batch [10 pointer items]: [20-29] processing batch [10 items]: [30-39] processing batch [10 pointer items]: [30-39] processing batch [2 items]: [40-41] processing batch [2 pointer items]: [40-41]
func NewExecutor ¶
func NewExecutor[T TypeConstraint](batchSize int, executor func(Batch[T]), opts ...Option) *Executor[T]
type Option ¶
type Option func(*options)
Option for an executor.
At this moment, no options are provided.
type PointerExecutor ¶
type PointerExecutor[T TypeConstraint] struct { // contains filtered or unexported fields }
PointerExecutor runs an executor function over a batch of pointers *T.
A shallow copy of *T is performed when assembling into a new batch.
Apart from the pointer logic, the PointerExecutor behaves like the Executor.
func NewPointerExecutor ¶
func NewPointerExecutor[T TypeConstraint](batchSize int, executor func(Batch[*T]), opts ...Option) *PointerExecutor[T]
func (PointerExecutor) Executed ¶
func (e PointerExecutor) Executed() uint64
Executed yields the count of batch executions.
func (*PointerExecutor[T]) Flush ¶
func (e *PointerExecutor[T]) Flush()
func (*PointerExecutor[T]) Push ¶
func (e *PointerExecutor[T]) Push(in *T)