OSDN Git Service

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