Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Batcher ¶
type Batcher[IN, OUT any] struct { // contains filtered or unexported fields }
Example ¶
averageAll := func(targetID string, entries []batch.Entry[float64, float64]) {
var (
sum float64
n int
)
for _, entry := range entries {
sum += entry.Value()
n++
}
avg := sum / float64(n)
for _, e := range entries {
e.Callback(avg)
}
}
b := batch.NewBatcher(
averageAll, "tag1",
batch.WithBatchSize(10),
batch.WithMinWaitTime(time.Second),
)
wg := sync.WaitGroup{}
for i := 0.0; i < 10.0; i++ {
wg.Add(1)
go func(i float64) {
t := time.Now()
b.EnterAndWait(
batch.NewEntry(i, func(out float64) { fmt.Println("duration:", time.Now().Sub(t), "avg:", out) }),
)
wg.Done()
}(i)
}
wg.Wait()
func NewBatcher ¶
NewBatcher construct a new Batcher with tagID. `tagID` is the value that will be passed to Func on every batch. This lets you define the same batch func with multiple Batcher objects; MultiBatcher is using `tagID` internally to handle different batches of entries in parallel.
func (*Batcher[IN, OUT]) EnterAndWait ¶ added in v0.11.25
type Entry ¶
type Entry[IN, OUT any] interface { Value() IN Callback(out OUT) // contains filtered or unexported methods }
type MultiBatcher ¶
type MultiBatcher[IN, OUT any] struct { // contains filtered or unexported fields }
func NewMulti ¶
func NewMulti[IN, OUT any](f Func[IN, OUT], opt ...Option) *MultiBatcher[IN, OUT]
NewMulti creates a pool of Batcher functions. By calling Enter or EnterAndWait you add the item into the Batcher which is identified by 'tagID'.
func (*MultiBatcher[IN, OUT]) Enter ¶
func (fp *MultiBatcher[IN, OUT]) Enter(targetID string, entry Entry[IN, OUT])
func (*MultiBatcher[IN, OUT]) EnterAndWait ¶
func (fp *MultiBatcher[IN, OUT]) EnterAndWait(targetID string, entry Entry[IN, OUT])
type Option ¶
type Option func(*config)
func WithBatchSize ¶
WithBatchSize sets the maximum number of entries to batch together. Defaults to 100.
func WithMaxWorkers ¶
WithMaxWorkers sets the maximum number of workers to use. Defaults to runtime.NumCPU() * 10.
func WithMinWaitTime ¶
WithMinWaitTime sets the minimum amount of time to wait before flushing a batch. Defaults to 0.