Skip to content

Instantly share code, notes, and snippets.

@kreshikhin
Created February 6, 2014 17:50
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 kreshikhin/8849152 to your computer and use it in GitHub Desktop.
Save kreshikhin/8849152 to your computer and use it in GitHub Desktop.
var sem = make(chan int, MaxOutstanding)
func handle(r *Request) {
<-sem // Wait for active queue to drain.
process(r) // May take a long time.
sem <- 1 // Done; enable next request to run.
}
func init() {
for i := 0; i < MaxOutstanding; i++ {
sem <- 1
}
}
func Serve(queue chan *Request) {
for {
req := <-queue
go handle(req) // Don't wait for handle to finish.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment