Created
May 10, 2021 04:38
-
-
Save boseji/601633ba7555ac47c125e795d1bf4be5 to your computer and use it in GitHub Desktop.
Golang Simple Worker Pool Example
This file contains hidden or 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
// Copyright 2020 Abhijit Bose. All rights reserved. | |
// Use of this source code is governed by a Apache 2.0 license | |
package main | |
import ( | |
"log" | |
"sync" | |
"time" | |
) | |
const limit = 4 // Maximum 4 Go routines at a time | |
// Used for some randomness in sleep patterns | |
var sleepList = [limit]time.Duration{ | |
20 * time.Millisecond, | |
40 * time.Millisecond, | |
10 * time.Millisecond, | |
80 * time.Millisecond, | |
} | |
// Type of Work that would be provided | |
type work func() | |
// Group of workers | |
type group struct { | |
jobs chan work | |
allDone chan bool | |
} | |
func createGroup(n int) group { | |
g := group{ | |
jobs: make(chan work), | |
allDone: make(chan bool), | |
} | |
var wg sync.WaitGroup | |
// Start Workers | |
for i := 0; i < n; i++ { | |
wg.Add(1) | |
go func(id int) { | |
defer wg.Done() | |
for { | |
select { | |
case f, ok := <-g.jobs: | |
if !ok { | |
log.Println("Channel Close Exit from", id) | |
return | |
} | |
log.Println("Start", id) | |
f() | |
log.Println("Finished", id) | |
} | |
} | |
}(i) | |
} | |
// All done Signalling Routine | |
go func() { | |
wg.Wait() | |
log.Println("All workers Exitted") | |
g.allDone <- true | |
}() | |
return g | |
} | |
func (g group) Add(w work) { | |
g.jobs <- w | |
} | |
func (g group) Close() { | |
close(g.jobs) | |
<-g.allDone | |
} | |
func main() { | |
g := createGroup(limit) | |
defer g.Close() | |
for i := 0; i < (limit * 4); i++ { | |
v := i % limit | |
g.Add(func() { | |
dur := sleepList[v] | |
log.Println("Sleeping for", dur) | |
time.Sleep(dur) | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment