Batchify will group and deduplicate concurrent tasks to reduce resource consumption.
Example:
- reduce in-flight requests to a database
- dedupe similar requests during a short period of time
This library is thread-safe.
go get github.com/samber/go-batchify
This library is v0 and follows SemVer strictly.
Some breaking changes might be made to exported APIs before v1.0.0.
GoDoc: https://godoc.org/github.com/samber/go-batchify
import "github.com/samber/go-batchify"
batch := batchify.NewBatch(
10,
func (ids []int) (map[int]string, error) {
return ..., nil
}
)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
id, _ := strconv.Atoi(r.URL.Query().Get("id"))
value, err := batch.Do(id)
// ...
})
import "github.com/samber/go-batchify"
batch := batchify.NewBatchWithTimer(
10,
func (ids []int) (map[int]string, error) {
return ..., nil
},
5*time.Millisecond,
)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
id, _ := strconv.Atoi(r.URL.Query().Get("id"))
value, err := batch.Do(id)
// ...
})
import "github.com/samber/go-batchify"
batch := batchify.NewShardedBatchWithTimer(
5, // 5 shards
func(key int) uint64 { return uint64(key) }, // sharding key
10,
func (ids []int) (map[int]string, error) {
return ..., nil
},
5*time.Millisecond,
)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
id, _ := strconv.Atoi(r.URL.Query().Get("id"))
value, err := batch.Do(id)
// ...
})
import (
"golang.org/x/sync/singleflight"
"github.com/samber/go-batchify"
)
var group singleflight.Group
batch := batchify.NewBatchWithTimer(
10,
func (ids []int) (map[int]string, error) {
return ..., nil
},
5*time.Millisecond,
)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
idStr := r.URL.Query().Get("id")
id, _ := strconv.Atoi(idStr)
value, err, _ = group.Do(idStr, func() (interface{}, error) {
return batch.Do(id)
})
// ...
})
- Ping me on Twitter @samuelberthe (DMs, mentions, whatever :))
- Fork the project
- Fix open issues or request new features
Don't hesitate ;)
# Install some dev dependencies
make tools
# Run tests
make test
# or
make watch-test
Give a โญ๏ธ if this project helped you!
Copyright ยฉ 2024 Samuel Berthe.
This project is MIT licensed.