https://github.com/plzzzzg/conditional-batch-executor
A batch executor that collects tasks and executes them when conditions are met for Go.
https://github.com/plzzzzg/conditional-batch-executor
batch batch-processing go golang
Last synced: 5 months ago
JSON representation
A batch executor that collects tasks and executes them when conditions are met for Go.
- Host: GitHub
- URL: https://github.com/plzzzzg/conditional-batch-executor
- Owner: plzzzzg
- License: mit
- Created: 2023-08-17T10:02:50.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-08-18T02:45:22.000Z (almost 3 years ago)
- Last Synced: 2023-09-05T03:53:26.726Z (over 2 years ago)
- Topics: batch, batch-processing, go, golang
- Language: Go
- Homepage: https://pkg.go.dev/github.com/plzzzzg/conditional-batch-executor
- Size: 9.77 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Conditional Batch Executor
[](https://pkg.go.dev/github.com/plzzzzg/conditional-batch-executor#section-readme)
[](https://goreportcard.com/report/github.com/plzzzzg/conditional-batch-executor)
[](https://github.com/plzzzzg/conditional-batch-executor/actions/workflows/go.yml)
[](https://codecov.io/gh/plzzzzg/conditional-batch-executor)
A batch worker that collects tasks and executes them when conditions are met.
Caller can get the results asynchronously.
## Install
```shell
go get github.com/plzzzzg/conditional-batch-executor
```
## Examples
```go
// init
worker := conditiaonalbatchexecutor.New(func(tasks []interface{}) map[string]interface{} {
m := map[string]interface{}{}
// do something ...
return m // key is the id from Submit
}, conditiaonalbatchexecutor.Size(3), conditiaonalbatchexecutor.Interval(time.Second*2)) // execute if size of tasks >= 3 OR after 2 seconds since last execution
// submit
resultReciever, err := worker.Submit(idStr, i)
// receive
result := <-resultReciever
```
## Supported Conditions
### Interval
```go
worker := conditiaonalbatchexecutor.New(func(tasks []interface{}) map[string]interface{} {
m := map[string]interface{}{}
return m
}, conditiaonalbatchexecutor.Interval(time.Second*2)) // execute every 2 seconds
```
### Size
```go
worker := conditiaonalbatchexecutor.New(func(tasks []interface{}) map[string]interface{} {
m := map[string]interface{}{}
return m
}, conditiaonalbatchexecutor.Size(2)) // execute then buffer size reaches 2
```
### And
```go
worker := conditiaonalbatchexecutor.New(func(tasks []interface{}) map[string]interface{} {
m := map[string]interface{}{}
return m
}, conditiaonalbatchexecutor.And(Size(2), conditiaonalbatchexecutor.Interval(time.Second*2))) // execute then buffer size reaches 2 AND last execution happened more than 2 min ago
```