Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/leocarmona/workerpool
Golang workerpool
https://github.com/leocarmona/workerpool
concurrency go golang golang-worker golang-worker-pool golang-workpool worker-pool
Last synced: about 1 month ago
JSON representation
Golang workerpool
- Host: GitHub
- URL: https://github.com/leocarmona/workerpool
- Owner: leocarmona
- License: mit
- Created: 2021-12-29T14:41:13.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2022-01-24T04:11:21.000Z (about 3 years ago)
- Last Synced: 2024-08-27T22:18:32.717Z (5 months ago)
- Topics: concurrency, go, golang, golang-worker, golang-worker-pool, golang-workpool, worker-pool
- Language: Go
- Homepage:
- Size: 43.9 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Golang WorkerPool
![Build status](https://github.com/leocarmona/workerpool/actions/workflows/main.yaml/badge.svg)
![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/leocarmona/workerpool)
[![GoDoc](https://pkg.go.dev/badge/github.com/leocarmona/workerpool)](https://pkg.go.dev/github.com/leocarmona/workerpool)
[![License](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/leocarmona/workerpool/blob/main/LICENSE)## Installation
To install this library in your project, do you need to run:
```
go get github.com/leocarmona/[email protected]
```## How to use
### All commands available
```go
package mainimport (
"fmt"
"github.com/leocarmona/workerpool"
"runtime/debug"
"time"
)func main() {
wp := workerpool.New("io", &workerpool.Settings{
MinWorkers: 64,
MaxWorkers: 256,
IdleTimeout: 15 * time.Second,
UpScaling: 4,
DownScaling: 2,
Queue: 4096,
PanicHandler: func(panicErr interface{}) {
fmt.Printf("Worker recovered from a panic: %v\nStack trace: %s\n", panicErr, string(debug.Stack()))
},
})task := func() { fmt.Println("hi") }
timeout := time.Second
deadline := time.Now().Add(timeout)// Executions
wp.Submit(task)
wp.SubmitAndWait(task)
wp.SubmitAndWaitWithTimeout(timeout, task)
wp.SubmitAndWaitWithDeadline(deadline, task)wp.TrySubmit(task)
wp.TrySubmitAndWait(task)
wp.TrySubmitAndWaitWithTimeout(timeout, task)
wp.TrySubmitAndWaitWithDeadline(deadline, task)// Manage Workers/Goroutines
wp.Burst(1)
wp.ScaleUp(1)
wp.ScaleDown(1)
wp.ReleaseIdleWorkers()wp.Stop()
wp.StopAndWait()
wp.StopAndWaitWithTimeout(timeout)
wp.StopAndWaitWithDeadline(deadline)wp.Stopped()
// Metrics
wp.Metrics().RunningWorkers()
wp.Metrics().IdleWorkers()
wp.Metrics().MinWorkers()
wp.Metrics().MaxWorkers()
wp.Metrics().QueueCapacity()
wp.Metrics().SubmittedTasks()
wp.Metrics().WaitingTasks()
wp.Metrics().SuccessfulTasks()
wp.Metrics().FailedTasks()
wp.Metrics().CompletedTasks()
wp.Metrics().Snapshot()
wp.Metrics().String()// Settings
wp.MinWorkers()
wp.MaxWorkers()
wp.IdleTimeout()
wp.UpScaling()
wp.DownScaling()
wp.QueueCapacity()
wp.PanicHandler()wp.SetMinWorkers(0)
wp.SetMaxWorkers(1)
wp.SetIdleTimeout(15 * time.Second)
wp.SetUpScaling(1)
wp.SetDownScaling(1)
wp.SetPanicHandler(func(panicErr interface{}) {
fmt.Printf("Worker recovered from a panic: %v\nStack trace: %s\n", panicErr, string(debug.Stack()))
})wp.String()
}
```## API Reference
Full API reference is available at https://pkg.go.dev/github.com/leocarmona/workerpool