blob: b1db21af6f4d28f9e77c5b17807ffd8573ee545d [file] [log] [blame]
Serge Bazanskicc25bdf2018-10-25 14:02:58 +02001// +build go1.6,!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
21package grpc
22
23import (
24 "fmt"
25 "io"
26 "net"
27 "net/http"
28
29 "golang.org/x/net/context"
30 "google.golang.org/grpc/codes"
31 "google.golang.org/grpc/internal/transport"
32 "google.golang.org/grpc/status"
33)
34
35// dialContext connects to the address on the named network.
36func dialContext(ctx context.Context, network, address string) (net.Conn, error) {
37 return (&net.Dialer{Cancel: ctx.Done()}).Dial(network, address)
38}
39
40func sendHTTPRequest(ctx context.Context, req *http.Request, conn net.Conn) error {
41 req.Cancel = ctx.Done()
42 if err := req.Write(conn); err != nil {
43 return fmt.Errorf("failed to write the HTTP request: %v", err)
44 }
45 return nil
46}
47
48// toRPCErr converts an error into an error from the status package.
49func toRPCErr(err error) error {
50 if err == nil || err == io.EOF {
51 return err
52 }
53 if err == io.ErrUnexpectedEOF {
54 return status.Error(codes.Internal, err.Error())
55 }
56 if _, ok := status.FromError(err); ok {
57 return err
58 }
59 switch e := err.(type) {
60 case transport.ConnectionError:
61 return status.Error(codes.Unavailable, e.Desc)
62 default:
63 switch err {
64 case context.DeadlineExceeded:
65 return status.Error(codes.DeadlineExceeded, err.Error())
66 case context.Canceled:
67 return status.Error(codes.Canceled, err.Error())
68 }
69 }
70 return status.Error(codes.Unknown, err.Error())
71}