Skip to content

Instantly share code, notes, and snippets.

@adampresley
Last active May 16, 2020 14:51
Show Gist options
  • Save adampresley/5eaf8b406e342eb8a51ce7b468c2fd18 to your computer and use it in GitHub Desktop.
Save adampresley/5eaf8b406e342eb8a51ce7b468c2fd18 to your computer and use it in GitHub Desktop.
Example using the worker pool
package main
import (
"fmt"
"time"
"pkg/workerpool"
)
type Job struct {
Index int
}
func (j *Job) Work(workerID int) {
fmt.Printf("Worker %d sleeping on index %d...\n", workerID, j.Index)
time.Sleep(2 * time.Second)
}
func main() {
var pool workerpool.IPool
pool = workerpool.NewPool(workerpool.PoolConfig{
MaxJobQueue: 100,
MaxWorkers: 10,
MaxWorkerWaitTime: 3 * time.Second,
})
pool.Start()
for index := 0; index < 30; index++ {
job := &Job{Index: index}
pool.QueueJob(job)
}
pool.Wait()
pool.Shutdown()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment