blob: 535ee9356f34f12ac60730f559ef3fb91b968def [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001// +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/status"
32 "google.golang.org/grpc/transport"
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 _, ok := status.FromError(err); ok {
54 return err
55 }
56 switch e := err.(type) {
57 case transport.StreamError:
58 return status.Error(e.Code, e.Desc)
59 case transport.ConnectionError:
60 return status.Error(codes.Unavailable, e.Desc)
61 default:
62 switch err {
63 case context.DeadlineExceeded:
64 return status.Error(codes.DeadlineExceeded, err.Error())
65 case context.Canceled:
66 return status.Error(codes.Canceled, err.Error())
67 }
68 }
69 return status.Error(codes.Unknown, err.Error())
70}