Skip to content

Instantly share code, notes, and snippets.

@alexrios
Last active August 12, 2020 22:54
Show Gist options
  • Save alexrios/03e58b277bcb0e30d148eefaf0469f07 to your computer and use it in GitHub Desktop.
Save alexrios/03e58b277bcb0e30d148eefaf0469f07 to your computer and use it in GitHub Desktop.
Golang Worker Pool
package main
import (
"fmt"
"sync"
"time"
"strconv"
)
type Job struct {
ID int
MetaData string
}
type Result struct {
Job *Job
Value int
}
func main() {
startTime := time.Now()
runApp()
diff := time.Now().Sub(startTime)
fmt.Println("total time taken ", diff.Seconds(), "seconds")
}
func runApp(){
done := make(chan bool)
results := make(chan *Result)
go readResults(results, done)
totalJobs := 1000
jobs := createJobs(totalJobs)
executeWorkerPool(1000, jobs, results)
<-done
}
func createJobs(jobs int) chan *Job {
jobsChan := make(chan *Job)
go func(out chan *Job) {
defer close(out)
for i := 0; i <= jobs; i++ {
out <- &Job{i, strconv.Itoa(i)}
}
}(jobsChan)
return jobsChan
}
func worker(wg *sync.WaitGroup, in chan *Job, out chan *Result) {
defer wg.Done()
for job := range in {
time.Sleep(1 * time.Second)
out <- &Result{job, job.ID}
}
}
func executeWorkerPool(workers int, in chan *Job, out chan *Result){
defer close(out)
wg:= &sync.WaitGroup{}
for i := 0;i<=workers; i++ {
wg.Add(1)
go worker(wg, in, out)
}
wg.Wait()
}
func readResults(results chan *Result, done chan bool){
for r := range results {
fmt.Printf("Job %v\n", r.Job.ID)
}
done <- true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment