Last active
May 16, 2020 14:51
-
-
Save adampresley/5eaf8b406e342eb8a51ce7b468c2fd18 to your computer and use it in GitHub Desktop.
Example using the worker pool
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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