go17.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. "fmt"
  23. "io"
  24. "net"
  25. "net/http"
  26. "os"
  27. netctx "golang.org/x/net/context"
  28. "google.golang.org/grpc/codes"
  29. "google.golang.org/grpc/status"
  30. "google.golang.org/grpc/transport"
  31. )
  32. // dialContext connects to the address on the named network.
  33. func dialContext(ctx context.Context, network, address string) (net.Conn, error) {
  34. return (&net.Dialer{}).DialContext(ctx, network, address)
  35. }
  36. func sendHTTPRequest(ctx context.Context, req *http.Request, conn net.Conn) error {
  37. req = req.WithContext(ctx)
  38. if err := req.Write(conn); err != nil {
  39. return fmt.Errorf("failed to write the HTTP request: %v", err)
  40. }
  41. return nil
  42. }
  43. // toRPCErr converts an error into an error from the status package.
  44. func toRPCErr(err error) error {
  45. if _, ok := status.FromError(err); ok {
  46. return err
  47. }
  48. switch e := err.(type) {
  49. case transport.StreamError:
  50. return status.Error(e.Code, e.Desc)
  51. case transport.ConnectionError:
  52. return status.Error(codes.Unavailable, e.Desc)
  53. default:
  54. switch err {
  55. case context.DeadlineExceeded, netctx.DeadlineExceeded:
  56. return status.Error(codes.DeadlineExceeded, err.Error())
  57. case context.Canceled, netctx.Canceled:
  58. return status.Error(codes.Canceled, err.Error())
  59. case ErrClientConnClosing:
  60. return status.Error(codes.FailedPrecondition, err.Error())
  61. }
  62. }
  63. return status.Error(codes.Unknown, err.Error())
  64. }
  65. // convertCode converts a standard Go error into its canonical code. Note that
  66. // this is only used to translate the error returned by the server applications.
  67. func convertCode(err error) codes.Code {
  68. switch err {
  69. case nil:
  70. return codes.OK
  71. case io.EOF:
  72. return codes.OutOfRange
  73. case io.ErrClosedPipe, io.ErrNoProgress, io.ErrShortBuffer, io.ErrShortWrite, io.ErrUnexpectedEOF:
  74. return codes.FailedPrecondition
  75. case os.ErrInvalid:
  76. return codes.InvalidArgument
  77. case context.Canceled, netctx.Canceled:
  78. return codes.Canceled
  79. case context.DeadlineExceeded, netctx.DeadlineExceeded:
  80. return codes.DeadlineExceeded
  81. }
  82. switch {
  83. case os.IsExist(err):
  84. return codes.AlreadyExists
  85. case os.IsNotExist(err):
  86. return codes.NotFound
  87. case os.IsPermission(err):
  88. return codes.PermissionDenied
  89. }
  90. return codes.Unknown
  91. }