mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 06:19:44 +08:00
32 lines
550 B
Go
32 lines
550 B
Go
package main
|
|
|
|
const MAXREQS = 50
|
|
var sem = make(chan int, MAXREQS)
|
|
|
|
type Request struct {
|
|
a, b int
|
|
replyc chan int
|
|
}
|
|
|
|
func process(r *Request) {
|
|
// do something
|
|
}
|
|
|
|
func handle(r *Request) {
|
|
sem <- 1 // doesn't matter what we put in it
|
|
process(r)
|
|
<-sem // one empty place in the buffer: the next request can start
|
|
}
|
|
|
|
func server(service chan *Request) {
|
|
for {
|
|
request := <-service
|
|
go handle(request)
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
service := make(chan *Request)
|
|
go server(service)
|
|
}
|