/batcher

Compose concurrent single items into batches on the fly.

Primary LanguageGo

Batcher

Compose concurrent single items into batches on the fly.

Usage

Sync batcher either buffer an item and unblock the caller or return a batch to the caller.

package main

import (
	"context"
	"time"

	"github.com/makasim/batcher"
)

func main() {
	b := batcher.NewSync[string](10, time.Second)
	defer b.Shutdown(context.Background())

	// add items to the batch concurrently
	batch := b.Batch(`foo`)
	if batch == nil {
		// the foo item goes to someone else batch
		// we are done here
		return
	}

	// process batch
}

Async batcher always buffer and unblock the caller and call the batch function in separate goroutine.

package main

import (
    "context"
    "time"
    
    "github.com/makasim/batcher"
)

func main() {
    b := batcher.NewAsync[int](1, time.Second*60, func(items []int) {
        // process items
    })
    defer b.Shutdown(context.Background())

	b.Batch(123)
}

Results

Gist