client.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package netrpc
  2. import (
  3. "net/rpc"
  4. "golang.org/x/net/context"
  5. "github.com/cryptix/exp/todoKitSvc/reqrep"
  6. "github.com/cryptix/exp/todoKitSvc/todosvc"
  7. )
  8. func NewClient(c *rpc.Client) todosvc.Endpoints {
  9. return todosvc.Endpoints{
  10. Add: func(ctx context.Context, request interface{}) (interface{}, error) {
  11. var (
  12. errs = make(chan error, 1)
  13. responses = make(chan interface{}, 1)
  14. )
  15. go func() {
  16. var response reqrep.AddResponse
  17. if err := c.Call("todosvc.Add", request, &response); err != nil {
  18. errs <- err
  19. return
  20. }
  21. responses <- response
  22. }()
  23. select {
  24. case <-ctx.Done():
  25. return nil, context.DeadlineExceeded
  26. case err := <-errs:
  27. return nil, err
  28. case response := <-responses:
  29. return response, nil
  30. }
  31. },
  32. List: func(ctx context.Context, request interface{}) (interface{}, error) {
  33. var (
  34. errs = make(chan error, 1)
  35. responses = make(chan interface{}, 1)
  36. )
  37. go func() {
  38. var response reqrep.ListResponse
  39. if err := c.Call("todosvc.List", request, &response); err != nil {
  40. errs <- err
  41. return
  42. }
  43. responses <- response
  44. }()
  45. select {
  46. case <-ctx.Done():
  47. return nil, context.DeadlineExceeded
  48. case err := <-errs:
  49. return nil, err
  50. case response := <-responses:
  51. return response, nil
  52. }
  53. },
  54. }
  55. }