Documentation
¶
Overview ¶
Package batch groups values received from a channel into slices.
A batch is sent when it reaches its size, when its timeout expires (see WithTimeout), when a split function says the next value starts a new batch (see NewFunc), or when the input channel is closed. Batches are never empty and never share memory: the receiver owns every slice it gets.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func New ¶
New reads values from in and sends them in batches of up to size values to the returned channel, in the order they were received.
When in is closed, New sends the last, possibly shorter, batch and closes the channel. When ctx is done, New stops reading and closes the channel; the batch it was filling is discarded. To shut down without losing values, close in.
New panics if size is less than 1.
Example ¶
package main
import (
"context"
"fmt"
"github.com/floatdrop/chan/batch"
)
func main() {
in := make(chan string, 5)
for _, v := range []string{"A", "B", "C", "D", "E"} {
in <- v
}
close(in)
for b := range batch.New(context.Background(), in, 3) {
fmt.Println(b)
}
}
Output: [A B C] [D E]
func NewFunc ¶
func NewFunc[T any](ctx context.Context, in <-chan T, size int, split func(prev, next T) bool, opts ...Option) <-chan []T
NewFunc is like New, but it also ends a batch where split reports true: split is called with the last value of the current batch and the next value, and if it returns true the current batch is sent and next starts a new one.
To group consecutive values that share a key, split on a change of key:
func(prev, next Event) bool { return prev.UserID != next.UserID }
Example ¶
package main
import (
"context"
"fmt"
"github.com/floatdrop/chan/batch"
)
func main() {
type event struct{ user, action string }
in := make(chan event, 5)
for _, e := range []event{
{"alice", "login"}, {"alice", "view"}, {"bob", "login"}, {"alice", "logout"}, {"bob", "logout"},
} {
in <- e
}
close(in)
sameUser := func(prev, next event) bool { return prev.user != next.user }
for b := range batch.NewFunc(context.Background(), in, 100, sameUser) {
fmt.Println(b)
}
}
Output: [{alice login} {alice view}] [{bob login}] [{alice logout}] [{bob logout}]
Types ¶
type Option ¶
type Option func(*config)
Option configures New and NewFunc.
func WithBuffer ¶
WithBuffer sets the capacity of the output channel. The default is 0: the channel is unbuffered. WithBuffer panics if n is negative.
func WithTimeout ¶
WithTimeout bounds how long a batch can stay unfinished: once d has passed since its first value arrived, the batch is sent even if it is not full. Without this option, a batch waits until it is full or the input is closed. To batch by time alone, pass math.MaxInt as the size.
WithTimeout panics if d is not positive.
Example ¶
package main
import (
"context"
"fmt"
"time"
"github.com/floatdrop/chan/batch"
)
func main() {
ctx := context.Background()
rows := make(chan string)
go func() {
defer close(rows)
rows <- "first"
rows <- "second"
time.Sleep(300 * time.Millisecond) // A lull: the batch is sent before it fills up.
rows <- "third"
}()
// Insert rows in batches of 1000, but never keep a row waiting for more than 50ms.
for b := range batch.New(ctx, rows, 1000, batch.WithTimeout(50*time.Millisecond)) {
fmt.Println(b)
}
}
Output: [first second] [third]