Created
January 21, 2019 23:06
-
-
Save davecremins/81eda4be17116f01ab6e445c43f40df2 to your computer and use it in GitHub Desktop.
Very simple example of a worker pool pattern in Golang
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" | |
"sync" | |
"time" | |
) | |
type Job chan int | |
type Pool chan Job | |
type Worker struct { | |
Id int | |
Pool | |
JobCh Job | |
WG *sync.WaitGroup | |
} | |
func NewWorker(id int, pool Pool, wg *sync.WaitGroup) Worker { | |
return Worker{ | |
Id: id, | |
Pool: pool, | |
JobCh: make(Job), | |
WG: wg, | |
} | |
} | |
func main() { | |
pool := make(Pool) | |
defer close(pool) | |
var wg sync.WaitGroup | |
i, wc := 0, 5 | |
for i < wc { | |
wg.Add(1) | |
i++ | |
worker := NewWorker(i, pool, &wg) | |
go worker.ReceiveData() | |
} | |
time.Sleep(time.Second) | |
wg.Add(1) | |
go SendData(wc, pool, &wg) | |
wg.Wait() | |
fmt.Println("Finished") | |
} | |
func SendData(wc int, pool Pool, wg *sync.WaitGroup) { | |
defer wg.Done() | |
for i := 0; i < 10; i++ { | |
input := <-pool | |
input <- i | |
time.Sleep(500 * time.Millisecond) | |
} | |
for i := 0; i < wc; i++ { | |
jobCh := <-pool | |
close(jobCh) | |
} | |
} | |
func (w *Worker) ReceiveData() { | |
fmt.Printf("Worker %d is listening\n", w.Id) | |
for { | |
w.Pool <- w.JobCh | |
v, ok := <-w.JobCh | |
if ok { | |
fmt.Println("Worker:", w.Id) | |
fmt.Println("Recevied value:", v) | |
} else { | |
fmt.Println("Shutting down worker:", w.Id) | |
w.WG.Done() | |
return | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment