Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/n-r-w/retrypool
Go worker pool with error control
https://github.com/n-r-w/retrypool
go workerpool
Last synced: 6 days ago
JSON representation
Go worker pool with error control
- Host: GitHub
- URL: https://github.com/n-r-w/retrypool
- Owner: n-r-w
- License: mit
- Created: 2024-04-02T07:53:30.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2024-04-02T13:06:29.000Z (8 months ago)
- Last Synced: 2024-06-19T20:59:10.739Z (5 months ago)
- Topics: go, workerpool
- Language: Go
- Homepage:
- Size: 23.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![Go Reference](https://pkg.go.dev/badge/github.com/n-r-w/retrypool.svg)](https://pkg.go.dev/github.com/n-r-w/retrypool)
[![Go Coverage](https://github.com/n-r-w/retrypool/wiki/coverage.svg)](https://raw.githack.com/wiki/n-r-w/retrypool/coverage.html)
![CI Status](https://github.com/n-r-w/retrypool/actions/workflows/go.yml/badge.svg)
[![Stability](http://badges.github.io/stability-badges/dist/stable.svg)](http://github.com/badges/stability-badges)
[![Go Report](https://goreportcard.com/badge/github.com/n-r-w/retrypool)](https://goreportcard.com/badge/github.com/n-r-w/retrypool)# RetryPool
The RetryPool package provides a mechanism for executing tasks with automatic retry and error handling capabilities. It allows you to define a work function that performs the actual task, an error handling function that determines whether to retry or stop based on the error, and a success handling function that is called when the task is successfully completed.
The RetryPool is created with a specified number of worker goroutines and a maximum number of tasks that can be queued. Tasks are added to the pool using the Add method, and the pool executes them concurrently. The pool automatically retries failed tasks based on the error handling function and configurable retry settings.
Its also supports various configuration options, such as maximum age for a task, retry delay, maximum retry delay, and processing delay. These options allow you to fine-tune the behavior of the RetryPool according to your specific requirements.
## Installation
```bash
go get github.com/n-r-w/retrypool
```## Usage
```go
package mainimport (
"context"
"fmt"
"time""github.com/n-r-w/retrypool"
)type Data struct {
Value int
}func main() {
// Define your work function
workFunc := func(ctx context.Context, v Data, age time.Duration) error {
if age < time.Second/2 {
fmt.Printf("Processing with error: %v after %v\n", v, age)
return fmt.Errorf("error %v", v)
}fmt.Printf("Processing: %v after %v\n", v, age)
return nil
}// Define your error handling function
errorFunc := func(err error, v Data, age time.Duration, closing bool) bool {
fmt.Printf("Error: %v after %v\n", v, age)if closing {
fmt.Printf("Closing: %v after %v\n", v, age)
}return age < time.Minute // Retry for a minute
}// Define your success handling function
successFunc := func(v Data, age time.Duration) {
fmt.Printf("Success: %v after %v\n", v, age)
}// Create a new RetryPool
pool := retrypool.New(100, 100, 2, workFunc, errorFunc,
retrypool.WithMaxAge[Data](time.Minute*10),
retrypool.WithRetryDelay[Data](time.Second/2),
retrypool.WithMaxRetryDelay[Data](time.Second),
retrypool.WithProcessingDelay[Data](time.Hour),
retrypool.WithSuccessFunc[Data](successFunc))// Add tasks to the pool
pool.Add(Data{Value: 1})
time.Sleep(time.Second * 2)pool.Stop()
}
```Output:
```
Processing with error: {1} after 19.191µs
Error: {1} after 36.772µs
Processing: {1} after 1.000079775s
Success: {1} after 1.000079775s
```