Skip to content

samber/go-batchify

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

11 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Batchify

tag Go Version GoDoc Build Status Go report Coverage Contributors License

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.

๐Ÿš€ Install

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.

๐Ÿค  Getting started

GoDoc: https://godoc.org/github.com/samber/go-batchify

Simple batch

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)

    // ...
})

Batch with periodic flush

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)

    // ...
})

Sharded batches

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)

    // ...
})

go-batchify + singleflight

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)
    })

    // ...
})

๐Ÿค Contributing

Don't hesitate ;)

# Install some dev dependencies
make tools

# Run tests
make test
# or
make watch-test

๐Ÿ‘ค Contributors

Contributors

๐Ÿ’ซ Show your support

Give a โญ๏ธ if this project helped you!

GitHub Sponsors

๐Ÿ“ License

Copyright ยฉ 2024 Samuel Berthe.

This project is MIT licensed.