batchify

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 7, 2024 License: MIT Imports: 5 Imported by: 0

Image README

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.

Image

🚀 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.

Image Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Batch

type Batch[I comparable, O any] interface {
	Do(input I) (output O, err error)
	Flush()
	Stop()
}

func NewBatch

func NewBatch[I comparable, O any](bufferSize int, do func([]I) (map[I]O, error)) Batch[I, O]

NewBatch creates a new Batch instance with fixed size and no timer.

func NewBatchWithTimer

func NewBatchWithTimer[I comparable, O any](bufferSize int, do func([]I) (map[I]O, error), ttl time.Duration) Batch[I, O]

func NewShardedBatch

func NewShardedBatch[I comparable, O any](shards int, hasher hasher.Hasher[I], bufferSize int, do func([]I) (map[I]O, error)) Batch[I, O]

func NewShardedBatchWithTimer

func NewShardedBatchWithTimer[I comparable, O any](shards int, hasher hasher.Hasher[I], bufferSize int, do func([]I) (map[I]O, error), ttl time.Duration) Batch[I, O]

type BatchConfig

type BatchConfig[I comparable, O any] struct {
	// contains filtered or unexported fields
}

func NewBatchConfig

func NewBatchConfig[I comparable, O any](bufferSize int, do func([]I) (map[I]O, error)) BatchConfig[I, O]

BatchConfig is a builder for Batch.

func (BatchConfig[I, O]) Build

func (cfg BatchConfig[I, O]) Build() Batch[I, O]

Build creates a new Batch instance.

func (BatchConfig[I, O]) WithSharding

func (cfg BatchConfig[I, O]) WithSharding(nbr int, fn hasher.Hasher[I]) BatchConfig[I, O]

WithSharding enables cache sharding.

func (BatchConfig[I, O]) WithTimer

func (cfg BatchConfig[I, O]) WithTimer(ttl time.Duration) BatchConfig[I, O]

WithTimer sets the max time for a batch buffer

Image Directories

Path Synopsis
example
basic command
singleflight command
pkg

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL