https://github.com/kunaltaitkar/goworkerpool
go pool of concurrent workers with the ability to add / kill workers on demand
https://github.com/kunaltaitkar/goworkerpool
channels concurrency go golang goroutines workerpool
Last synced: 4 months ago
JSON representation
go pool of concurrent workers with the ability to add / kill workers on demand
- Host: GitHub
- URL: https://github.com/kunaltaitkar/goworkerpool
- Owner: kunaltaitkar
- Created: 2019-10-06T16:49:14.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-01-28T17:12:26.000Z (over 6 years ago)
- Last Synced: 2024-06-20T03:29:09.306Z (almost 2 years ago)
- Topics: channels, concurrency, go, golang, goroutines, workerpool
- Language: Go
- Homepage:
- Size: 7.81 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
**Workerpool-withCancellableContext**
```
package main
import (
"fmt"
"goworkerpool/workerpool"
"sync"
"time"
)
var count int
var countMutex = &sync.Mutex{}
func main() {
count = 0
// preparing tasks
tasks := []*workerpool.Task{}
for index := 0; index < 1000; index++ {
tasks = append(tasks, workerpool.NewTask(index, incrementCount))
}
//initialize pool
pool := workerpool.NewPoolWithContext(tasks, 10)
// to check count value
ticker := time.NewTicker(1 * time.Millisecond)
// cancel all workers when count is more than 500
go func() {
for range ticker.C {
if count > 500 {
fmt.Println("cancelling tasks...")
pool.Cancel()
return
}
}
}()
// run pool
pool.Run()
time.Sleep(10 * time.Second)
}
//incrementCount- increment count by 1
func incrementCount(data interface{}) error {
countMutex.Lock()
count++
countMutex.Unlock()
return nil
}
```