multiplex_server.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright 2009 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package main
  5. import "fmt"
  6. type Request struct {
  7. a, b int
  8. replyc chan int // reply channel inside the Request
  9. }
  10. type binOp func(a, b int) int
  11. func run(op binOp, req *Request) {
  12. req.replyc <- op(req.a, req.b)
  13. }
  14. func server(op binOp, service chan *Request) {
  15. for {
  16. req := <-service // requests arrive here
  17. // start goroutine for request:
  18. go run(op, req) // don't wait for op
  19. }
  20. }
  21. func startServer(op binOp) chan *Request {
  22. reqChan := make(chan *Request)
  23. go server(op, reqChan)
  24. return reqChan
  25. }
  26. func main() {
  27. adder := startServer(func(a, b int) int { return a + b })
  28. const N = 100
  29. var reqs [N]Request
  30. for i := 0; i < N; i++ {
  31. req := &reqs[i]
  32. req.a = i
  33. req.b = i + N
  34. req.replyc = make(chan int)
  35. adder <- req
  36. }
  37. // checks:
  38. for i := N - 1; i >= 0; i-- { // doesn't matter what order
  39. if <-reqs[i].replyc != N+2*i {
  40. fmt.Println("fail at", i)
  41. } else {
  42. fmt.Println("Request ", i, " is ok!")
  43. }
  44. }
  45. fmt.Println("done")
  46. }