go17.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // +build go1.7
  2. /*
  3. * Copyright 2016, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. package grpc
  34. import (
  35. "context"
  36. "io"
  37. "net"
  38. "net/http"
  39. "os"
  40. "google.golang.org/grpc/codes"
  41. "google.golang.org/grpc/status"
  42. "google.golang.org/grpc/transport"
  43. netctx "golang.org/x/net/context"
  44. )
  45. // dialContext connects to the address on the named network.
  46. func dialContext(ctx context.Context, network, address string) (net.Conn, error) {
  47. return (&net.Dialer{}).DialContext(ctx, network, address)
  48. }
  49. func sendHTTPRequest(ctx context.Context, req *http.Request, conn net.Conn) error {
  50. req = req.WithContext(ctx)
  51. if err := req.Write(conn); err != nil {
  52. return err
  53. }
  54. return nil
  55. }
  56. // toRPCErr converts an error into an error from the status package.
  57. func toRPCErr(err error) error {
  58. if _, ok := status.FromError(err); ok {
  59. return err
  60. }
  61. switch e := err.(type) {
  62. case transport.StreamError:
  63. return status.Error(e.Code, e.Desc)
  64. case transport.ConnectionError:
  65. return status.Error(codes.Internal, e.Desc)
  66. default:
  67. switch err {
  68. case context.DeadlineExceeded, netctx.DeadlineExceeded:
  69. return status.Error(codes.DeadlineExceeded, err.Error())
  70. case context.Canceled, netctx.Canceled:
  71. return status.Error(codes.Canceled, err.Error())
  72. case ErrClientConnClosing:
  73. return status.Error(codes.FailedPrecondition, err.Error())
  74. }
  75. }
  76. return status.Error(codes.Unknown, err.Error())
  77. }
  78. // convertCode converts a standard Go error into its canonical code. Note that
  79. // this is only used to translate the error returned by the server applications.
  80. func convertCode(err error) codes.Code {
  81. switch err {
  82. case nil:
  83. return codes.OK
  84. case io.EOF:
  85. return codes.OutOfRange
  86. case io.ErrClosedPipe, io.ErrNoProgress, io.ErrShortBuffer, io.ErrShortWrite, io.ErrUnexpectedEOF:
  87. return codes.FailedPrecondition
  88. case os.ErrInvalid:
  89. return codes.InvalidArgument
  90. case context.Canceled, netctx.Canceled:
  91. return codes.Canceled
  92. case context.DeadlineExceeded, netctx.DeadlineExceeded:
  93. return codes.DeadlineExceeded
  94. }
  95. switch {
  96. case os.IsExist(err):
  97. return codes.AlreadyExists
  98. case os.IsNotExist(err):
  99. return codes.NotFound
  100. case os.IsPermission(err):
  101. return codes.PermissionDenied
  102. }
  103. return codes.Unknown
  104. }