Skip to content

Instantly share code, notes, and snippets.

@bernardolm
Forked from alexrios/workerpool.go
Last active August 12, 2020 23:19
Show Gist options
  • Save bernardolm/4d15a8ceacfb8e89da887a73d0d4f685 to your computer and use it in GitHub Desktop.
Save bernardolm/4d15a8ceacfb8e89da887a73d0d4f685 to your computer and use it in GitHub Desktop.
Golang Worker Pool
package main
import (
"fmt"
"strconv"
"sync"
"time"
)
type Job struct {
ID int
MetaData string
}
type Result struct {
Job *Job
Value int
}
func main() {
fmt.Printf("main\n")
defer fmt.Printf("end main\n")
startTime := time.Now()
runApp()
diff := time.Since(startTime)
fmt.Println("total time taken ", diff.Seconds(), "seconds")
}
func runApp() {
fmt.Printf("runApp\n")
defer fmt.Printf("end runApp\n")
done := make(chan bool)
results := make(chan *Result)
go readResults(results, done)
totalJobs := 5
jobs := createJobs(totalJobs)
executeWorkerPool(3, jobs, results)
<-done
}
func createJobs(jobs int) chan *Job {
fmt.Printf("createJobs jobs:%d\n", jobs)
defer fmt.Printf("end createJobs jobs:%d\n", jobs)
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) {
fmt.Printf("worker wg:%v\n", wg)
defer fmt.Printf("end worker\n")
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) {
fmt.Printf("executeWorkerPool workers:%d\n", workers)
defer fmt.Printf("end executeWorkerPool\n")
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) {
fmt.Printf("readResults results:%v done:%v\n", results, done)
defer fmt.Printf("end readResults\n")
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