max_tasks.go 487 B

1234567891011121314151617181920212223242526272829303132
  1. package main
  2. const MAXREQS = 50
  3. var sem = make(chan int, MAXREQS)
  4. type Request struct {
  5. a, b int
  6. replyc chan int
  7. }
  8. func process(r *Request) {
  9. // do something
  10. }
  11. func handle(r *Request) {
  12. sem <- 1 // doesn't matter what we put in it
  13. process(r)
  14. <-sem // one empty place in the buffer: the next request can start
  15. }
  16. func server(service chan *Request) {
  17. for {
  18. request := <-service
  19. go handle(request)
  20. }
  21. }
  22. func main() {
  23. service := make(chan *Request)
  24. go server(service)
  25. }