A lightweight Go library for batching values from a channel with support for size-based and optional timeout-based flushing.
- Batch items from a channel based on size
- Flush batches after a timeout, even if not full
- Supports context cancellation with
WithContext
option - Zero-dependency, idiomatic Go
go get github.com/floatdrop/batchan
package main
import (
"fmt"
"time"
"github.com/floatdrop/batchan"
)
func main() {
input := make(chan string, 5)
batches := batchan.New(input, 3)
go func() {
inputs := []string{"A", "B", "C", "D", "E"}
for _, v := range inputs {
input <- v
}
close(input)
}()
for v := range batches {
fmt.Println("Got:", v)
}
// Output:
// Got: [A B C]
// Got: [D E]
}
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.