https://github.com/rashadansari/go-batch
A simple batching library in Golang
https://github.com/rashadansari/go-batch
batch batching go golang
Last synced: 8 months ago
JSON representation
A simple batching library in Golang
- Host: GitHub
- URL: https://github.com/rashadansari/go-batch
- Owner: RashadAnsari
- License: mit
- Created: 2021-09-01T14:39:47.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-10-16T13:49:11.000Z (over 1 year ago)
- Last Synced: 2025-04-21T12:42:53.758Z (12 months ago)
- Topics: batch, batching, go, golang
- Language: Go
- Homepage:
- Size: 18.6 KB
- Stars: 6
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Go Batch
A simple batching library in Golang.
## Guid
### Installation
```bash
go get github.com/RashadAnsari/go-batch/v2
```
### Example
```go
package main
import (
"context"
"log"
"math/rand"
"reflect"
"time"
goBatch "github.com/RashadAnsari/go-batch/v2"
)
func main() {
ctx, canl := context.WithCancel(context.Background())
batch := goBatch.New[int](
goBatch.WithSize(10),
goBatch.WithMaxWait(1*time.Second),
goBatch.WithContext(ctx),
)
go func() {
for {
output := <-batch.Output
log.Printf("output: %v, size: %d\n",
output, reflect.ValueOf(output).Len())
}
}()
for i := 1; i <= 100; i++ {
batch.Input <- i
}
time.Sleep(1 * time.Second)
for i := 1; i <= 100; i++ {
batch.Input <- i
if rand.Intn(2) == 0 {
time.Sleep(300 * time.Millisecond)
}
}
canl()
}
```