Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. type Request struct {
  2. Path string
  3. Random *pb.Random
  4. }
  5.  
  6. const stopRequestPath = "STOP"
  7.  
  8. func startWorkers(requestQueue *chan Request, noWorkers int) func() {
  9. var wg sync.WaitGroup
  10. for i := 0; i < noWorkers; i++ {
  11. startWorker(requestQueue, &wg)
  12. }
  13. // Returns a function that stops as many workers as were just started
  14. return func() {
  15. wg.Add(noWorkers)
  16. stopRequest := Request{Path: stopRequestPath}
  17. for i := 0; i < noWorkers; i++ {
  18. *requestQueue <- stopRequest
  19. }
  20. wg.Wait()
  21. }
  22. }
  23.  
  24. func startWorker(requestQueue *chan Request, wg *sync.WaitGroup) {
  25. go func() {
  26. for {
  27. request := <- *requestQueue
  28. if (request.Path == stopRequestPath) {
  29. wg.Done()
  30. return
  31. }
  32. get(request.Path, request.Random)
  33. }
  34. }()
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement