public
Last active

Worker Pool example in Go

  • Download Gist
gistfile1.go
Go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
package main
 
import (
"fmt"
"net/http"
"os"
"sync"
)
 
var (
requests = 10000
concurrency = 200
url = "http://localhost:3000/"
wait = sync.WaitGroup{}
)
 
func main() {
client := &http.Client{}
output := make(chan string)
 
for i := 0; i < concurrency; i++ {
go httpWorker(client, output)
}
 
for i := 0; i < requests; i++ {
output <- url
fmt.Printf("\r%d", i+1)
}
close(output)
 
fmt.Println("")
os.Stdout.Sync()
fmt.Println("All URLs handled.")
os.Stdout.Sync()
 
wait.Wait()
fmt.Println("All workers done.")
}
 
func httpWorker(client *http.Client, input chan string) {
// Increment the wait group counter
wait.Add(1)
 
// Iterate over channel input work
for req := range input {
if _, err := client.Head(req); err != nil {
fmt.Println(err)
}
}
 
// Finish up this worker
wait.Done()
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.