Skip to content

Instantly share code, notes, and snippets.

@vanleantking
Created November 10, 2022 10:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vanleantking/0b78477f4270aaa8fd389aa2bea3a4ce to your computer and use it in GitHub Desktop.
Save vanleantking/0b78477f4270aaa8fd389aa2bea3a4ce to your computer and use it in GitHub Desktop.
workers use buffer channel
https://play.golang.com/p/9uHR2jIDR9y
package main
import (
"fmt"
"time"
)
const (
numberOfURLs = 10000
numberOfWorkers = 5
)
func crawlURL(queue <-chan int, name string) {
for v := range queue {
fmt.Printf("Worker %s is crawling URL %d\n", name, v)
time.Sleep(time.Second)
}
fmt.Printf("Worker %s done and exit\n", name)
}
func startQueue() <-chan int {
queue := make(chan int, 100)
go func() {
for i := 1; i <= numberOfURLs; i++ {
queue <- i
fmt.Printf("URL %d has been enqueued\n", i)
}
close(queue)
}()
return queue
}
func main() {
queue := startQueue()
for i := 1; i <= numberOfWorkers; i++ {
go crawlURL(queue, fmt.Sprintf("%d", i))
}
time.Sleep(time.Minute * 5)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment