Skip to content

Instantly share code, notes, and snippets.

@johnpena
Created January 1, 2020 20:45
Show Gist options
  • Save johnpena/615621c54b2e5d291cebba11c27cd33f to your computer and use it in GitHub Desktop.
Save johnpena/615621c54b2e5d291cebba11c27cd33f to your computer and use it in GitHub Desktop.
Very basic example of a non-queueing worker pool + bouncer in go
package main
import (
"fmt"
"time"
)
func main() {
concurrency := 10
bouncer := make(chan bool, concurrency)
for i := 0; i < concurrency; i++ {
bouncer <- true
}
work := 0
bounces := 0
Loop:
for {
select {
case worker := <- bouncer:
go func() {
time.Sleep(5 * time.Millisecond)
work += 1
bouncer <- worker
}()
default:
bounces += 1
if work > 50 {
break Loop
}
}
}
fmt.Printf("Work: %d\n", work)
fmt.Printf("Bounces: %d\n", bounces)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment