go17.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // +build go1.7
  2. /*
  3. *
  4. * Copyright 2016 gRPC authors.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. */
  19. package grpc
  20. import (
  21. "context"
  22. "io"
  23. "net"
  24. "net/http"
  25. "os"
  26. netctx "golang.org/x/net/context"
  27. "google.golang.org/grpc/codes"
  28. "google.golang.org/grpc/status"
  29. "google.golang.org/grpc/transport"
  30. )
  31. // dialContext connects to the address on the named network.
  32. func dialContext(ctx context.Context, network, address string) (net.Conn, error) {
  33. return (&net.Dialer{}).DialContext(ctx, network, address)
  34. }
  35. func sendHTTPRequest(ctx context.Context, req *http.Request, conn net.Conn) error {
  36. req = req.WithContext(ctx)
  37. if err := req.Write(conn); err != nil {
  38. return err
  39. }
  40. return nil
  41. }
  42. // toRPCErr converts an error into an error from the status package.
  43. func toRPCErr(err error) error {
  44. if _, ok := status.FromError(err); ok {
  45. return err
  46. }
  47. switch e := err.(type) {
  48. case transport.StreamError:
  49. return status.Error(e.Code, e.Desc)
  50. case transport.ConnectionError:
  51. return status.Error(codes.Unavailable, e.Desc)
  52. default:
  53. switch err {
  54. case context.DeadlineExceeded, netctx.DeadlineExceeded:
  55. return status.Error(codes.DeadlineExceeded, err.Error())
  56. case context.Canceled, netctx.Canceled:
  57. return status.Error(codes.Canceled, err.Error())
  58. case ErrClientConnClosing:
  59. return status.Error(codes.FailedPrecondition, err.Error())
  60. }
  61. }
  62. return status.Error(codes.Unknown, err.Error())
  63. }
  64. // convertCode converts a standard Go error into its canonical code. Note that
  65. // this is only used to translate the error returned by the server applications.
  66. func convertCode(err error) codes.Code {
  67. switch err {
  68. case nil:
  69. return codes.OK
  70. case io.EOF:
  71. return codes.OutOfRange
  72. case io.ErrClosedPipe, io.ErrNoProgress, io.ErrShortBuffer, io.ErrShortWrite, io.ErrUnexpectedEOF:
  73. return codes.FailedPrecondition
  74. case os.ErrInvalid:
  75. return codes.InvalidArgument
  76. case context.Canceled, netctx.Canceled:
  77. return codes.Canceled
  78. case context.DeadlineExceeded, netctx.DeadlineExceeded:
  79. return codes.DeadlineExceeded
  80. }
  81. switch {
  82. case os.IsExist(err):
  83. return codes.AlreadyExists
  84. case os.IsNotExist(err):
  85. return codes.NotFound
  86. case os.IsPermission(err):
  87. return codes.PermissionDenied
  88. }
  89. return codes.Unknown
  90. }