Created
February 6, 2014 17:50
-
-
Save kreshikhin/8849152 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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