1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
package client
import (
"fmt"
"net"
"time"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/keepalive"
"net/url"
"google.golang.org/grpc"
)
// DefaultDialOpts hold the default DialOptions for connection to Gitaly over UNIX-socket
var DefaultDialOpts = []grpc.DialOption{}
type connectionType int
const (
invalidConnection connectionType = iota
tcpConnection
tlsConnection
unixConnection
)
// Dial gitaly
func Dial(rawAddress string, connOpts []grpc.DialOption) (*grpc.ClientConn, error) {
var canonicalAddress string
var err error
switch getConnectionType(rawAddress) {
case invalidConnection:
return nil, fmt.Errorf("invalid connection string: %s", rawAddress)
case tlsConnection:
canonicalAddress, err = extractHostFromRemoteURL(rawAddress) // Ensure the form: "host:port" ...
if err != nil {
return nil, err
}
certPool, err := systemCertPool()
if err != nil {
return nil, err
}
creds := credentials.NewClientTLSFromCert(certPool, "")
connOpts = append(connOpts, grpc.WithTransportCredentials(creds))
case tcpConnection:
canonicalAddress, err = extractHostFromRemoteURL(rawAddress) // Ensure the form: "host:port" ...
if err != nil {
return nil, err
}
connOpts = append(connOpts, grpc.WithInsecure())
case unixConnection:
canonicalAddress = rawAddress // This will be overridden by the custom dialer...
connOpts = append(
connOpts,
grpc.WithInsecure(),
// Use a custom dialer to ensure that we don't experience
// issues in environments that have proxy configurations
// https://gitlab.com/gitlab-org/gitaly/merge_requests/1072#note_140408512
grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
path, err := extractPathFromSocketURL(addr)
if err != nil {
return nil, err
}
return net.DialTimeout("unix", path, timeout)
}),
grpc.WithKeepaliveParams(keepalive.ClientParameters{
Time: 20 * time.Second,
PermitWithoutStream: true,
}),
)
}
conn, err := grpc.Dial(canonicalAddress, connOpts...)
if err != nil {
return nil, err
}
return conn, nil
}
func getConnectionType(rawAddress string) connectionType {
u, err := url.Parse(rawAddress)
if err != nil {
return invalidConnection
}
switch u.Scheme {
case "tls":
return tlsConnection
case "unix":
return unixConnection
case "tcp":
return tcpConnection
default:
return invalidConnection
}
}
|