/bucket

bucket of goroutines to handle messages

Primary LanguageGoMIT LicenseMIT

bucket 🪣

License Coverage GitHub Workflow Status Go Report Card Go PKG

Bucket call a function with limited size of gorutine and data count.
Size of data calculated by len(data) / processCount with minimum and maximum size configurations.

go get github.com/rakunlabs/bucket

This package based on golang.org/x/sync/errgroup.

Usage

Bucket accepts a function with signature func(context.Context, []T) error.

processBucket := bucket.New(process,
    bucket.WithProcessCount(4),
    bucket.WithMinSize(2),
    bucket.WithMaxSize(100),
)

// 10 items -> 10/4 -> 3 items per bucket, 3,3,3,1 will be processed in 4 gorutine
data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

// processBucket returns error of process function
if err := processBucket.Process(context.Background(), data); err != nil {
    return err
}