forked from grpc/grpc-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclientconn.go
280 lines (254 loc) · 9.37 KB
/
clientconn.go
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
*
* Copyright 2014, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
package grpc
import (
"context"
"errors"
"fmt"
"net/http"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/stats"
)
var (
// ErrClientConnClosing indicates that the operation is illegal because
// the ClientConn is closing.
ErrClientConnClosing = errors.New("grpc: the client connection is closing")
// ErrClientConnTimeout indicates that the ClientConn cannot establish the
// underlying connections within the specified timeout.
// DEPRECATED: Please use context.DeadlineExceeded instead. This error will be
// removed in Q1 2017.
ErrClientConnTimeout = errors.New("grpc: timed out when dialing")
// errNoTransportSecurity indicates that there is no transport security
// being set for ClientConn. Users should either set one or explicitly
// call WithInsecure DialOption to disable security.
errNoTransportSecurity = errors.New("grpc: no transport security set (use grpc.WithInsecure() explicitly or set credentials)")
// errTransportCredentialsMissing indicates that users want to transmit security
// information (e.g., oauth2 token) which requires secure connection on an insecure
// connection.
errTransportCredentialsMissing = errors.New("grpc: the credentials require transport level security (use grpc.WithTransportCredentials() to set)")
// errCredentialsConflict indicates that grpc.WithTransportCredentials()
// and grpc.WithInsecure() are both called for a connection.
errCredentialsConflict = errors.New("grpc: transport credentials are set for an insecure connection (grpc.WithTransportCredentials() and grpc.WithInsecure() are both called)")
)
type clientOptions struct {
codec Codec
cp Compressor
dc Decompressor
// All may be zero:
perRPCCreds []credentials.PerRPCCredentials
userAgent string
statsHandler stats.Handler
transportCreds credentials.TransportCredentials // only checked for non-nil for now
insecure bool // not TLS
}
// DialOption is a client option.
//
// Despite its name, it does not necessarily have anything to do with
// dialing.
//
// TODO: rename this.
type DialOption func(*clientOptions)
// WithCodec returns a DialOption which sets a codec for message marshaling and unmarshaling.
func WithCodec(c Codec) DialOption {
return func(o *clientOptions) {
o.codec = c
}
}
// WithCompressor returns a DialOption which sets a CompressorGenerator for generating message
// compressor.
func WithCompressor(cp Compressor) DialOption {
return func(o *clientOptions) {
o.cp = cp
}
}
// WithDecompressor returns a DialOption which sets a DecompressorGenerator for generating
// message decompressor.
func WithDecompressor(dc Decompressor) DialOption {
return func(o *clientOptions) {
o.dc = dc
}
}
// WithPerRPCCredentials returns an option which sets
// credentials which will place auth state on each outbound RPC.
func WithPerRPCCredentials(creds credentials.PerRPCCredentials) DialOption {
return func(o *clientOptions) {
o.perRPCCreds = append(o.perRPCCreds, creds)
}
}
// WithStatsHandler returns a DialOption that specifies the stats handler
// for all the RPCs and underlying network connections in this ClientConn.
func WithStatsHandler(h stats.Handler) DialOption {
return func(o *clientOptions) {
o.statsHandler = h
}
}
// WithUserAgent returns a DialOption that specifies a user agent string for all the RPCs.
func WithUserAgent(s string) DialOption {
return func(o *clientOptions) {
o.userAgent = s
}
}
// NewClient returns a new gRPC client for the provided target server.
// If the provided HTTP client is nil, http.DefaultClient is used.
// The target should be a URL scheme and authority, without a path.
// For example, "https://api.example.com" for TLS or "http://10.0.5.3:5000"
// for unencrypted HTTP/2.
//
// The returned type is named "ClientConn" for legacy reasons. It does
// not necessarily represent one actual connection. (It might be zero
// or multiple.)
func NewClient(hc *http.Client, target string, opts ...DialOption) (*ClientConn, error) {
if hc == nil {
hc = http.DefaultClient
}
if target == "" {
return nil, errors.New("NewClientConn: missing required target parameter")
}
cc := &ClientConn{
hc: hc,
target: target,
}
for _, opt := range opts {
opt(&cc.opts)
}
// Set defaults.
if cc.opts.codec == nil {
cc.opts.codec = protoCodec{}
}
return cc, nil
}
// ConnectivityState indicates the state of a client connection.
type ConnectivityState int
const (
// Idle indicates the ClientConn is idle.
Idle ConnectivityState = iota
// Connecting indicates the ClienConn is connecting.
Connecting
// Ready indicates the ClientConn is ready for work.
Ready
// TransientFailure indicates the ClientConn has seen a failure but expects to recover.
TransientFailure
// Shutdown indicates the ClientConn has started shutting down.
Shutdown
)
func (s ConnectivityState) String() string {
switch s {
case Idle:
return "IDLE"
case Connecting:
return "CONNECTING"
case Ready:
return "READY"
case TransientFailure:
return "TRANSIENT_FAILURE"
case Shutdown:
return "SHUTDOWN"
default:
panic(fmt.Sprintf("unknown connectivity state: %d", s))
}
}
// ClientConn is a gRPC client.
//
// Despite its name, it is not necessarily a single
// connection. Depending on its underlying transport, it could be
// using zero or multiple TCP or other connections, and changing over
// time.
type ClientConn struct {
target string // server URL prefix (scheme + authority, optional port), without path ("https://api.example.com"); use http:// for h2c
opts clientOptions
hc *http.Client
sc *ServiceConfig // TODO(bradfitz): support; may be nil for now
}
func (cc *ClientConn) getMethodConfig(method string) (m MethodConfig, ok bool) {
if cc.sc == nil {
return
}
m, ok = cc.sc.Methods[method]
return
}
// Close tears down the ClientConn and all underlying connections.
func (cc *ClientConn) Close() error {
// TODO(bradfitz): something? maybe just close some cancel
// chan that we then merge into all http Request's context
// with some new context.Context impl? And then do some
// http.Transport.CloseIdleConnections? But first research
// what callers of this actually expect. It's unclear.
return nil
}
// WithTransportCredentials is controls whether to use TLS or not for connections.
//
// Deprecated: this is only respected in a minimal form to let
// existing code in the wild work. Uew NewClient instead.
func WithTransportCredentials(creds credentials.TransportCredentials) DialOption {
return func(o *clientOptions) {
o.transportCreds = creds
}
}
// WithInsecure returns a DialOption which disables transport security for this ClientConn.
// WithInsecure is mutually exclusive with use of WithTransportCredentials or https
// endpoints.
func WithInsecure() DialOption {
return func(o *clientOptions) { o.insecure = true }
}
// DialContext is the old way to create a gRPC client.
//
// Deprecated: use NewClient instead.
func DialContext(ctx context.Context, target string, opts ...DialOption) (*ClientConn, error) {
var o clientOptions
for _, opt := range opts {
opt(&o)
}
if (o.transportCreds != nil) == o.insecure {
return nil, fmt.Errorf("only one of TransportCredentials or Insecure may be used")
}
if o.transportCreds != nil {
if o.transportCreds.Info().SecurityProtocol == "tls" {
target = "https://" + target
// TODO(bradfitz): care about the rest? use the interface?
// Not today. Prefer to delete the interace.
} else {
return nil, fmt.Errorf("unsupported TransportCredentials %+v", o.transportCreds.Info())
}
}
if o.insecure {
panic("TODO: implement insecure http2.Transport dialing")
}
return NewClient(nil, target, opts...)
}
// Dial is the old way to create a gRPC client.
//
// Deprecated: use NewClient instead. This only exists to let existing code
// in the wild work.
func Dial(target string, opts ...DialOption) (*ClientConn, error) {
return DialContext(context.Background(), target, opts...)
}