clientconn.go 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  1. /*
  2. *
  3. * Copyright 2014 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package grpc
  19. import (
  20. "errors"
  21. "math"
  22. "net"
  23. "reflect"
  24. "strings"
  25. "sync"
  26. "time"
  27. "golang.org/x/net/context"
  28. "golang.org/x/net/trace"
  29. "google.golang.org/grpc/balancer"
  30. "google.golang.org/grpc/connectivity"
  31. "google.golang.org/grpc/credentials"
  32. "google.golang.org/grpc/grpclog"
  33. "google.golang.org/grpc/keepalive"
  34. "google.golang.org/grpc/resolver"
  35. "google.golang.org/grpc/stats"
  36. "google.golang.org/grpc/transport"
  37. )
  38. var (
  39. // ErrClientConnClosing indicates that the operation is illegal because
  40. // the ClientConn is closing.
  41. ErrClientConnClosing = errors.New("grpc: the client connection is closing")
  42. // ErrClientConnTimeout indicates that the ClientConn cannot establish the
  43. // underlying connections within the specified timeout.
  44. // DEPRECATED: Please use context.DeadlineExceeded instead.
  45. ErrClientConnTimeout = errors.New("grpc: timed out when dialing")
  46. // errNoTransportSecurity indicates that there is no transport security
  47. // being set for ClientConn. Users should either set one or explicitly
  48. // call WithInsecure DialOption to disable security.
  49. errNoTransportSecurity = errors.New("grpc: no transport security set (use grpc.WithInsecure() explicitly or set credentials)")
  50. // errTransportCredentialsMissing indicates that users want to transmit security
  51. // information (e.g., oauth2 token) which requires secure connection on an insecure
  52. // connection.
  53. errTransportCredentialsMissing = errors.New("grpc: the credentials require transport level security (use grpc.WithTransportCredentials() to set)")
  54. // errCredentialsConflict indicates that grpc.WithTransportCredentials()
  55. // and grpc.WithInsecure() are both called for a connection.
  56. errCredentialsConflict = errors.New("grpc: transport credentials are set for an insecure connection (grpc.WithTransportCredentials() and grpc.WithInsecure() are both called)")
  57. // errNetworkIO indicates that the connection is down due to some network I/O error.
  58. errNetworkIO = errors.New("grpc: failed with network I/O error")
  59. // errConnDrain indicates that the connection starts to be drained and does not accept any new RPCs.
  60. errConnDrain = errors.New("grpc: the connection is drained")
  61. // errConnClosing indicates that the connection is closing.
  62. errConnClosing = errors.New("grpc: the connection is closing")
  63. // errConnUnavailable indicates that the connection is unavailable.
  64. errConnUnavailable = errors.New("grpc: the connection is unavailable")
  65. // errBalancerClosed indicates that the balancer is closed.
  66. errBalancerClosed = errors.New("grpc: balancer is closed")
  67. // minimum time to give a connection to complete
  68. minConnectTimeout = 20 * time.Second
  69. )
  70. // dialOptions configure a Dial call. dialOptions are set by the DialOption
  71. // values passed to Dial.
  72. type dialOptions struct {
  73. unaryInt UnaryClientInterceptor
  74. streamInt StreamClientInterceptor
  75. codec Codec
  76. cp Compressor
  77. dc Decompressor
  78. bs backoffStrategy
  79. block bool
  80. insecure bool
  81. timeout time.Duration
  82. scChan <-chan ServiceConfig
  83. copts transport.ConnectOptions
  84. callOptions []CallOption
  85. // This is to support v1 balancer.
  86. balancerBuilder balancer.Builder
  87. }
  88. const (
  89. defaultClientMaxReceiveMessageSize = 1024 * 1024 * 4
  90. defaultClientMaxSendMessageSize = math.MaxInt32
  91. )
  92. // DialOption configures how we set up the connection.
  93. type DialOption func(*dialOptions)
  94. // WithInitialWindowSize returns a DialOption which sets the value for initial window size on a stream.
  95. // The lower bound for window size is 64K and any value smaller than that will be ignored.
  96. func WithInitialWindowSize(s int32) DialOption {
  97. return func(o *dialOptions) {
  98. o.copts.InitialWindowSize = s
  99. }
  100. }
  101. // WithInitialConnWindowSize returns a DialOption which sets the value for initial window size on a connection.
  102. // The lower bound for window size is 64K and any value smaller than that will be ignored.
  103. func WithInitialConnWindowSize(s int32) DialOption {
  104. return func(o *dialOptions) {
  105. o.copts.InitialConnWindowSize = s
  106. }
  107. }
  108. // WithMaxMsgSize returns a DialOption which sets the maximum message size the client can receive. Deprecated: use WithDefaultCallOptions(MaxCallRecvMsgSize(s)) instead.
  109. func WithMaxMsgSize(s int) DialOption {
  110. return WithDefaultCallOptions(MaxCallRecvMsgSize(s))
  111. }
  112. // WithDefaultCallOptions returns a DialOption which sets the default CallOptions for calls over the connection.
  113. func WithDefaultCallOptions(cos ...CallOption) DialOption {
  114. return func(o *dialOptions) {
  115. o.callOptions = append(o.callOptions, cos...)
  116. }
  117. }
  118. // WithCodec returns a DialOption which sets a codec for message marshaling and unmarshaling.
  119. func WithCodec(c Codec) DialOption {
  120. return func(o *dialOptions) {
  121. o.codec = c
  122. }
  123. }
  124. // WithCompressor returns a DialOption which sets a CompressorGenerator for generating message
  125. // compressor.
  126. func WithCompressor(cp Compressor) DialOption {
  127. return func(o *dialOptions) {
  128. o.cp = cp
  129. }
  130. }
  131. // WithDecompressor returns a DialOption which sets a DecompressorGenerator for generating
  132. // message decompressor.
  133. func WithDecompressor(dc Decompressor) DialOption {
  134. return func(o *dialOptions) {
  135. o.dc = dc
  136. }
  137. }
  138. // WithBalancer returns a DialOption which sets a load balancer with the v1 API.
  139. // Name resolver will be ignored if this DialOption is specified.
  140. // Deprecated: use the new balancer APIs in balancer package instead.
  141. func WithBalancer(b Balancer) DialOption {
  142. return func(o *dialOptions) {
  143. o.balancerBuilder = &balancerWrapperBuilder{
  144. b: b,
  145. }
  146. }
  147. }
  148. // WithServiceConfig returns a DialOption which has a channel to read the service configuration.
  149. func WithServiceConfig(c <-chan ServiceConfig) DialOption {
  150. return func(o *dialOptions) {
  151. o.scChan = c
  152. }
  153. }
  154. // WithBackoffMaxDelay configures the dialer to use the provided maximum delay
  155. // when backing off after failed connection attempts.
  156. func WithBackoffMaxDelay(md time.Duration) DialOption {
  157. return WithBackoffConfig(BackoffConfig{MaxDelay: md})
  158. }
  159. // WithBackoffConfig configures the dialer to use the provided backoff
  160. // parameters after connection failures.
  161. //
  162. // Use WithBackoffMaxDelay until more parameters on BackoffConfig are opened up
  163. // for use.
  164. func WithBackoffConfig(b BackoffConfig) DialOption {
  165. // Set defaults to ensure that provided BackoffConfig is valid and
  166. // unexported fields get default values.
  167. setDefaults(&b)
  168. return withBackoff(b)
  169. }
  170. // withBackoff sets the backoff strategy used for retries after a
  171. // failed connection attempt.
  172. //
  173. // This can be exported if arbitrary backoff strategies are allowed by gRPC.
  174. func withBackoff(bs backoffStrategy) DialOption {
  175. return func(o *dialOptions) {
  176. o.bs = bs
  177. }
  178. }
  179. // WithBlock returns a DialOption which makes caller of Dial blocks until the underlying
  180. // connection is up. Without this, Dial returns immediately and connecting the server
  181. // happens in background.
  182. func WithBlock() DialOption {
  183. return func(o *dialOptions) {
  184. o.block = true
  185. }
  186. }
  187. // WithInsecure returns a DialOption which disables transport security for this ClientConn.
  188. // Note that transport security is required unless WithInsecure is set.
  189. func WithInsecure() DialOption {
  190. return func(o *dialOptions) {
  191. o.insecure = true
  192. }
  193. }
  194. // WithTransportCredentials returns a DialOption which configures a
  195. // connection level security credentials (e.g., TLS/SSL).
  196. func WithTransportCredentials(creds credentials.TransportCredentials) DialOption {
  197. return func(o *dialOptions) {
  198. o.copts.TransportCredentials = creds
  199. }
  200. }
  201. // WithPerRPCCredentials returns a DialOption which sets
  202. // credentials and places auth state on each outbound RPC.
  203. func WithPerRPCCredentials(creds credentials.PerRPCCredentials) DialOption {
  204. return func(o *dialOptions) {
  205. o.copts.PerRPCCredentials = append(o.copts.PerRPCCredentials, creds)
  206. }
  207. }
  208. // WithTimeout returns a DialOption that configures a timeout for dialing a ClientConn
  209. // initially. This is valid if and only if WithBlock() is present.
  210. // Deprecated: use DialContext and context.WithTimeout instead.
  211. func WithTimeout(d time.Duration) DialOption {
  212. return func(o *dialOptions) {
  213. o.timeout = d
  214. }
  215. }
  216. // WithDialer returns a DialOption that specifies a function to use for dialing network addresses.
  217. // If FailOnNonTempDialError() is set to true, and an error is returned by f, gRPC checks the error's
  218. // Temporary() method to decide if it should try to reconnect to the network address.
  219. func WithDialer(f func(string, time.Duration) (net.Conn, error)) DialOption {
  220. return func(o *dialOptions) {
  221. o.copts.Dialer = func(ctx context.Context, addr string) (net.Conn, error) {
  222. if deadline, ok := ctx.Deadline(); ok {
  223. return f(addr, deadline.Sub(time.Now()))
  224. }
  225. return f(addr, 0)
  226. }
  227. }
  228. }
  229. // WithStatsHandler returns a DialOption that specifies the stats handler
  230. // for all the RPCs and underlying network connections in this ClientConn.
  231. func WithStatsHandler(h stats.Handler) DialOption {
  232. return func(o *dialOptions) {
  233. o.copts.StatsHandler = h
  234. }
  235. }
  236. // FailOnNonTempDialError returns a DialOption that specifies if gRPC fails on non-temporary dial errors.
  237. // If f is true, and dialer returns a non-temporary error, gRPC will fail the connection to the network
  238. // address and won't try to reconnect.
  239. // The default value of FailOnNonTempDialError is false.
  240. // This is an EXPERIMENTAL API.
  241. func FailOnNonTempDialError(f bool) DialOption {
  242. return func(o *dialOptions) {
  243. o.copts.FailOnNonTempDialError = f
  244. }
  245. }
  246. // WithUserAgent returns a DialOption that specifies a user agent string for all the RPCs.
  247. func WithUserAgent(s string) DialOption {
  248. return func(o *dialOptions) {
  249. o.copts.UserAgent = s
  250. }
  251. }
  252. // WithKeepaliveParams returns a DialOption that specifies keepalive paramaters for the client transport.
  253. func WithKeepaliveParams(kp keepalive.ClientParameters) DialOption {
  254. return func(o *dialOptions) {
  255. o.copts.KeepaliveParams = kp
  256. }
  257. }
  258. // WithUnaryInterceptor returns a DialOption that specifies the interceptor for unary RPCs.
  259. func WithUnaryInterceptor(f UnaryClientInterceptor) DialOption {
  260. return func(o *dialOptions) {
  261. o.unaryInt = f
  262. }
  263. }
  264. // WithStreamInterceptor returns a DialOption that specifies the interceptor for streaming RPCs.
  265. func WithStreamInterceptor(f StreamClientInterceptor) DialOption {
  266. return func(o *dialOptions) {
  267. o.streamInt = f
  268. }
  269. }
  270. // WithAuthority returns a DialOption that specifies the value to be used as
  271. // the :authority pseudo-header. This value only works with WithInsecure and
  272. // has no effect if TransportCredentials are present.
  273. func WithAuthority(a string) DialOption {
  274. return func(o *dialOptions) {
  275. o.copts.Authority = a
  276. }
  277. }
  278. // Dial creates a client connection to the given target.
  279. func Dial(target string, opts ...DialOption) (*ClientConn, error) {
  280. return DialContext(context.Background(), target, opts...)
  281. }
  282. // DialContext creates a client connection to the given target. ctx can be used to
  283. // cancel or expire the pending connection. Once this function returns, the
  284. // cancellation and expiration of ctx will be noop. Users should call ClientConn.Close
  285. // to terminate all the pending operations after this function returns.
  286. func DialContext(ctx context.Context, target string, opts ...DialOption) (conn *ClientConn, err error) {
  287. cc := &ClientConn{
  288. target: target,
  289. csMgr: &connectivityStateManager{},
  290. conns: make(map[*addrConn]struct{}),
  291. }
  292. cc.csEvltr = &connectivityStateEvaluator{csMgr: cc.csMgr}
  293. cc.ctx, cc.cancel = context.WithCancel(context.Background())
  294. for _, opt := range opts {
  295. opt(&cc.dopts)
  296. }
  297. cc.mkp = cc.dopts.copts.KeepaliveParams
  298. if cc.dopts.copts.Dialer == nil {
  299. cc.dopts.copts.Dialer = newProxyDialer(
  300. func(ctx context.Context, addr string) (net.Conn, error) {
  301. return dialContext(ctx, "tcp", addr)
  302. },
  303. )
  304. }
  305. if cc.dopts.copts.UserAgent != "" {
  306. cc.dopts.copts.UserAgent += " " + grpcUA
  307. } else {
  308. cc.dopts.copts.UserAgent = grpcUA
  309. }
  310. if cc.dopts.timeout > 0 {
  311. var cancel context.CancelFunc
  312. ctx, cancel = context.WithTimeout(ctx, cc.dopts.timeout)
  313. defer cancel()
  314. }
  315. defer func() {
  316. select {
  317. case <-ctx.Done():
  318. conn, err = nil, ctx.Err()
  319. default:
  320. }
  321. if err != nil {
  322. cc.Close()
  323. }
  324. }()
  325. scSet := false
  326. if cc.dopts.scChan != nil {
  327. // Try to get an initial service config.
  328. select {
  329. case sc, ok := <-cc.dopts.scChan:
  330. if ok {
  331. cc.sc = sc
  332. scSet = true
  333. }
  334. default:
  335. }
  336. }
  337. // Set defaults.
  338. if cc.dopts.codec == nil {
  339. cc.dopts.codec = protoCodec{}
  340. }
  341. if cc.dopts.bs == nil {
  342. cc.dopts.bs = DefaultBackoffConfig
  343. }
  344. creds := cc.dopts.copts.TransportCredentials
  345. if creds != nil && creds.Info().ServerName != "" {
  346. cc.authority = creds.Info().ServerName
  347. } else if cc.dopts.insecure && cc.dopts.copts.Authority != "" {
  348. cc.authority = cc.dopts.copts.Authority
  349. } else {
  350. cc.authority = target
  351. }
  352. // TODO(bar) parse scheme and start resolver.
  353. if cc.dopts.balancerBuilder != nil {
  354. var credsClone credentials.TransportCredentials
  355. if creds != nil {
  356. credsClone = creds.Clone()
  357. }
  358. buildOpts := balancer.BuildOptions{
  359. DialCreds: credsClone,
  360. Dialer: cc.dopts.copts.Dialer,
  361. }
  362. // Build should not take long time. So it's ok to not have a goroutine for it.
  363. // TODO(bar) init balancer after first resolver result to support service config balancer.
  364. cc.balancer = cc.dopts.balancerBuilder.Build(&ccBalancerWrapper{cc: cc}, buildOpts)
  365. } else {
  366. waitC := make(chan error, 1)
  367. go func() {
  368. defer close(waitC)
  369. // No balancer, or no resolver within the balancer. Connect directly.
  370. ac, err := cc.newAddrConn([]resolver.Address{{Addr: target}})
  371. if err != nil {
  372. waitC <- err
  373. return
  374. }
  375. if err := ac.connect(cc.dopts.block); err != nil {
  376. waitC <- err
  377. return
  378. }
  379. }()
  380. select {
  381. case <-ctx.Done():
  382. return nil, ctx.Err()
  383. case err := <-waitC:
  384. if err != nil {
  385. return nil, err
  386. }
  387. }
  388. }
  389. if cc.dopts.scChan != nil && !scSet {
  390. // Blocking wait for the initial service config.
  391. select {
  392. case sc, ok := <-cc.dopts.scChan:
  393. if ok {
  394. cc.sc = sc
  395. }
  396. case <-ctx.Done():
  397. return nil, ctx.Err()
  398. }
  399. }
  400. if cc.dopts.scChan != nil {
  401. go cc.scWatcher()
  402. }
  403. if cc.balancer != nil {
  404. // Unblock balancer initialization with a fake resolver update.
  405. // The balancer wrapper will not read the addresses, so an empty list works.
  406. // TODO(bar) remove this after the real resolver is started.
  407. cc.balancer.HandleResolvedAddrs([]resolver.Address{}, nil)
  408. }
  409. // A blocking dial blocks until the clientConn is ready.
  410. if cc.dopts.block {
  411. for {
  412. s := cc.GetState()
  413. if s == connectivity.Ready {
  414. break
  415. }
  416. if !cc.WaitForStateChange(ctx, s) {
  417. // ctx got timeout or canceled.
  418. return nil, ctx.Err()
  419. }
  420. }
  421. }
  422. return cc, nil
  423. }
  424. // connectivityStateEvaluator gets updated by addrConns when their
  425. // states transition, based on which it evaluates the state of
  426. // ClientConn.
  427. // Note: This code will eventually sit in the balancer in the new design.
  428. type connectivityStateEvaluator struct {
  429. csMgr *connectivityStateManager
  430. mu sync.Mutex
  431. numReady uint64 // Number of addrConns in ready state.
  432. numConnecting uint64 // Number of addrConns in connecting state.
  433. numTransientFailure uint64 // Number of addrConns in transientFailure.
  434. }
  435. // recordTransition records state change happening in every addrConn and based on
  436. // that it evaluates what state the ClientConn is in.
  437. // It can only transition between connectivity.Ready, connectivity.Connecting and connectivity.TransientFailure. Other states,
  438. // Idle and connectivity.Shutdown are transitioned into by ClientConn; in the begining of the connection
  439. // before any addrConn is created ClientConn is in idle state. In the end when ClientConn
  440. // closes it is in connectivity.Shutdown state.
  441. // TODO Note that in later releases, a ClientConn with no activity will be put into an Idle state.
  442. func (cse *connectivityStateEvaluator) recordTransition(oldState, newState connectivity.State) {
  443. cse.mu.Lock()
  444. defer cse.mu.Unlock()
  445. // Update counters.
  446. for idx, state := range []connectivity.State{oldState, newState} {
  447. updateVal := 2*uint64(idx) - 1 // -1 for oldState and +1 for new.
  448. switch state {
  449. case connectivity.Ready:
  450. cse.numReady += updateVal
  451. case connectivity.Connecting:
  452. cse.numConnecting += updateVal
  453. case connectivity.TransientFailure:
  454. cse.numTransientFailure += updateVal
  455. }
  456. }
  457. // Evaluate.
  458. if cse.numReady > 0 {
  459. cse.csMgr.updateState(connectivity.Ready)
  460. return
  461. }
  462. if cse.numConnecting > 0 {
  463. cse.csMgr.updateState(connectivity.Connecting)
  464. return
  465. }
  466. cse.csMgr.updateState(connectivity.TransientFailure)
  467. }
  468. // connectivityStateManager keeps the connectivity.State of ClientConn.
  469. // This struct will eventually be exported so the balancers can access it.
  470. type connectivityStateManager struct {
  471. mu sync.Mutex
  472. state connectivity.State
  473. notifyChan chan struct{}
  474. }
  475. // updateState updates the connectivity.State of ClientConn.
  476. // If there's a change it notifies goroutines waiting on state change to
  477. // happen.
  478. func (csm *connectivityStateManager) updateState(state connectivity.State) {
  479. csm.mu.Lock()
  480. defer csm.mu.Unlock()
  481. if csm.state == connectivity.Shutdown {
  482. return
  483. }
  484. if csm.state == state {
  485. return
  486. }
  487. csm.state = state
  488. if csm.notifyChan != nil {
  489. // There are other goroutines waiting on this channel.
  490. close(csm.notifyChan)
  491. csm.notifyChan = nil
  492. }
  493. }
  494. func (csm *connectivityStateManager) getState() connectivity.State {
  495. csm.mu.Lock()
  496. defer csm.mu.Unlock()
  497. return csm.state
  498. }
  499. func (csm *connectivityStateManager) getNotifyChan() <-chan struct{} {
  500. csm.mu.Lock()
  501. defer csm.mu.Unlock()
  502. if csm.notifyChan == nil {
  503. csm.notifyChan = make(chan struct{})
  504. }
  505. return csm.notifyChan
  506. }
  507. // ClientConn represents a client connection to an RPC server.
  508. type ClientConn struct {
  509. ctx context.Context
  510. cancel context.CancelFunc
  511. target string
  512. authority string
  513. dopts dialOptions
  514. csMgr *connectivityStateManager
  515. csEvltr *connectivityStateEvaluator // This will eventually be part of balancer.
  516. balancer balancer.Balancer
  517. // TODO(bar) move the mutex and picker into a struct that does blocking pick().
  518. pmu sync.Mutex
  519. picker balancer.Picker
  520. mu sync.RWMutex
  521. sc ServiceConfig
  522. conns map[*addrConn]struct{}
  523. // Keepalive parameter can be updated if a GoAway is received.
  524. mkp keepalive.ClientParameters
  525. }
  526. // WaitForStateChange waits until the connectivity.State of ClientConn changes from sourceState or
  527. // ctx expires. A true value is returned in former case and false in latter.
  528. // This is an EXPERIMENTAL API.
  529. func (cc *ClientConn) WaitForStateChange(ctx context.Context, sourceState connectivity.State) bool {
  530. ch := cc.csMgr.getNotifyChan()
  531. if cc.csMgr.getState() != sourceState {
  532. return true
  533. }
  534. select {
  535. case <-ctx.Done():
  536. return false
  537. case <-ch:
  538. return true
  539. }
  540. }
  541. // GetState returns the connectivity.State of ClientConn.
  542. // This is an EXPERIMENTAL API.
  543. func (cc *ClientConn) GetState() connectivity.State {
  544. return cc.csMgr.getState()
  545. }
  546. func (cc *ClientConn) scWatcher() {
  547. for {
  548. select {
  549. case sc, ok := <-cc.dopts.scChan:
  550. if !ok {
  551. return
  552. }
  553. cc.mu.Lock()
  554. // TODO: load balance policy runtime change is ignored.
  555. // We may revist this decision in the future.
  556. cc.sc = sc
  557. cc.mu.Unlock()
  558. case <-cc.ctx.Done():
  559. return
  560. }
  561. }
  562. }
  563. // newAddrConn creates an addrConn for addrs and adds it to cc.conns.
  564. func (cc *ClientConn) newAddrConn(addrs []resolver.Address) (*addrConn, error) {
  565. ac := &addrConn{
  566. cc: cc,
  567. addrs: addrs,
  568. dopts: cc.dopts,
  569. }
  570. ac.ctx, ac.cancel = context.WithCancel(cc.ctx)
  571. ac.csEvltr = cc.csEvltr
  572. // Track ac in cc. This needs to be done before any getTransport(...) is called.
  573. cc.mu.Lock()
  574. if cc.conns == nil {
  575. cc.mu.Unlock()
  576. return nil, ErrClientConnClosing
  577. }
  578. cc.conns[ac] = struct{}{}
  579. cc.mu.Unlock()
  580. return ac, nil
  581. }
  582. // removeAddrConn removes the addrConn in the subConn from clientConn.
  583. // It also tears down the ac with the given error.
  584. func (cc *ClientConn) removeAddrConn(ac *addrConn, err error) {
  585. cc.mu.Lock()
  586. if cc.conns == nil {
  587. cc.mu.Unlock()
  588. return
  589. }
  590. delete(cc.conns, ac)
  591. cc.mu.Unlock()
  592. ac.tearDown(err)
  593. }
  594. // connect starts to creating transport and also starts the transport monitor
  595. // goroutine for this ac.
  596. // TODO(bar) Move this to the addrConn section.
  597. // This was part of resetAddrConn, keep it here to make the diff look clean.
  598. func (ac *addrConn) connect(block bool) error {
  599. ac.mu.Lock()
  600. if ac.state == connectivity.Shutdown {
  601. ac.mu.Unlock()
  602. return errConnClosing
  603. }
  604. ac.mu.Unlock()
  605. if EnableTracing {
  606. ac.events = trace.NewEventLog("grpc.ClientConn", ac.addrs[0].Addr)
  607. }
  608. if !ac.dopts.insecure {
  609. if ac.dopts.copts.TransportCredentials == nil {
  610. return errNoTransportSecurity
  611. }
  612. } else {
  613. if ac.dopts.copts.TransportCredentials != nil {
  614. return errCredentialsConflict
  615. }
  616. for _, cd := range ac.dopts.copts.PerRPCCredentials {
  617. if cd.RequireTransportSecurity() {
  618. return errTransportCredentialsMissing
  619. }
  620. }
  621. }
  622. if block {
  623. if err := ac.resetTransport(false); err != nil {
  624. if err != errConnClosing {
  625. ac.tearDown(err)
  626. }
  627. if e, ok := err.(transport.ConnectionError); ok && !e.Temporary() {
  628. return e.Origin()
  629. }
  630. return err
  631. }
  632. // Start to monitor the error status of transport.
  633. go ac.transportMonitor()
  634. } else {
  635. // Start a goroutine connecting to the server asynchronously.
  636. go func() {
  637. if err := ac.resetTransport(false); err != nil {
  638. grpclog.Warningf("Failed to dial %s: %v; please retry.", ac.addrs[0].Addr, err)
  639. if err != errConnClosing {
  640. // Keep this ac in cc.conns, to get the reason it's torn down.
  641. ac.tearDown(err)
  642. }
  643. return
  644. }
  645. ac.transportMonitor()
  646. }()
  647. }
  648. return nil
  649. }
  650. // tryUpdateAddrs tries to update ac.addrs with the new addresses list.
  651. //
  652. // It checks whether current connected address of ac is in the new addrs list.
  653. // - If true, it updates ac.addrs and returns true. The ac will keep using
  654. // the existing connection.
  655. // - If false, it does nothing and returns false.
  656. func (ac *addrConn) tryUpdateAddrs(addrs []resolver.Address) bool {
  657. ac.mu.Lock()
  658. defer ac.mu.Unlock()
  659. grpclog.Infof("addrConn: tryUpdateAddrs curAddr: %v, addrs: %v", ac.curAddr, addrs)
  660. if ac.state == connectivity.Shutdown {
  661. ac.addrs = addrs
  662. return true
  663. }
  664. var curAddrFound bool
  665. for _, a := range addrs {
  666. if reflect.DeepEqual(ac.curAddr, a) {
  667. curAddrFound = true
  668. break
  669. }
  670. }
  671. grpclog.Infof("addrConn: tryUpdateAddrs curAddrFound: %v", curAddrFound)
  672. if curAddrFound {
  673. ac.addrs = addrs
  674. }
  675. return curAddrFound
  676. }
  677. // GetMethodConfig gets the method config of the input method.
  678. // If there's an exact match for input method (i.e. /service/method), we return
  679. // the corresponding MethodConfig.
  680. // If there isn't an exact match for the input method, we look for the default config
  681. // under the service (i.e /service/). If there is a default MethodConfig for
  682. // the serivce, we return it.
  683. // Otherwise, we return an empty MethodConfig.
  684. func (cc *ClientConn) GetMethodConfig(method string) MethodConfig {
  685. // TODO: Avoid the locking here.
  686. cc.mu.RLock()
  687. defer cc.mu.RUnlock()
  688. m, ok := cc.sc.Methods[method]
  689. if !ok {
  690. i := strings.LastIndex(method, "/")
  691. m, _ = cc.sc.Methods[method[:i+1]]
  692. }
  693. return m
  694. }
  695. func (cc *ClientConn) getTransport(ctx context.Context, opts BalancerGetOptions) (transport.ClientTransport, func(balancer.DoneInfo), error) {
  696. var (
  697. ac *addrConn
  698. put func(balancer.DoneInfo)
  699. )
  700. if cc.balancer == nil {
  701. // If balancer is nil, there should be only one addrConn available.
  702. cc.mu.RLock()
  703. if cc.conns == nil {
  704. cc.mu.RUnlock()
  705. return nil, nil, toRPCErr(ErrClientConnClosing)
  706. }
  707. for ac = range cc.conns {
  708. // Break after the first iteration to get the first addrConn.
  709. break
  710. }
  711. cc.mu.RUnlock()
  712. } else {
  713. cc.pmu.Lock()
  714. // TODO(bar) call pick on struct blockPicker instead of the real picker.
  715. p := cc.picker
  716. cc.pmu.Unlock()
  717. var (
  718. err error
  719. sc balancer.SubConn
  720. )
  721. sc, put, err = p.Pick(ctx, balancer.PickOptions{})
  722. if err != nil {
  723. return nil, nil, toRPCErr(err)
  724. }
  725. if acbw, ok := sc.(*acBalancerWrapper); ok {
  726. ac = acbw.getAddrConn()
  727. } else if put != nil {
  728. updateRPCInfoInContext(ctx, rpcInfo{bytesSent: false, bytesReceived: false})
  729. put(balancer.DoneInfo{Err: errors.New("SubConn returned by pick cannot be recognized")})
  730. }
  731. }
  732. if ac == nil {
  733. return nil, nil, errConnClosing
  734. }
  735. t, err := ac.wait(ctx, cc.balancer != nil, !opts.BlockingWait)
  736. if err != nil {
  737. if put != nil {
  738. updateRPCInfoInContext(ctx, rpcInfo{bytesSent: false, bytesReceived: false})
  739. put(balancer.DoneInfo{Err: err})
  740. }
  741. return nil, nil, err
  742. }
  743. return t, put, nil
  744. }
  745. // Close tears down the ClientConn and all underlying connections.
  746. func (cc *ClientConn) Close() error {
  747. cc.cancel()
  748. cc.mu.Lock()
  749. if cc.conns == nil {
  750. cc.mu.Unlock()
  751. return ErrClientConnClosing
  752. }
  753. conns := cc.conns
  754. cc.conns = nil
  755. cc.csMgr.updateState(connectivity.Shutdown)
  756. cc.mu.Unlock()
  757. if cc.balancer != nil {
  758. cc.balancer.Close()
  759. }
  760. for ac := range conns {
  761. ac.tearDown(ErrClientConnClosing)
  762. }
  763. return nil
  764. }
  765. // addrConn is a network connection to a given address.
  766. type addrConn struct {
  767. ctx context.Context
  768. cancel context.CancelFunc
  769. cc *ClientConn
  770. curAddr resolver.Address
  771. addrs []resolver.Address
  772. dopts dialOptions
  773. events trace.EventLog
  774. acbw balancer.SubConn
  775. csEvltr *connectivityStateEvaluator
  776. mu sync.Mutex
  777. state connectivity.State
  778. // ready is closed and becomes nil when a new transport is up or failed
  779. // due to timeout.
  780. ready chan struct{}
  781. transport transport.ClientTransport
  782. // The reason this addrConn is torn down.
  783. tearDownErr error
  784. }
  785. // adjustParams updates parameters used to create transports upon
  786. // receiving a GoAway.
  787. func (ac *addrConn) adjustParams(r transport.GoAwayReason) {
  788. switch r {
  789. case transport.TooManyPings:
  790. v := 2 * ac.dopts.copts.KeepaliveParams.Time
  791. ac.cc.mu.Lock()
  792. if v > ac.cc.mkp.Time {
  793. ac.cc.mkp.Time = v
  794. }
  795. ac.cc.mu.Unlock()
  796. }
  797. }
  798. // printf records an event in ac's event log, unless ac has been closed.
  799. // REQUIRES ac.mu is held.
  800. func (ac *addrConn) printf(format string, a ...interface{}) {
  801. if ac.events != nil {
  802. ac.events.Printf(format, a...)
  803. }
  804. }
  805. // errorf records an error in ac's event log, unless ac has been closed.
  806. // REQUIRES ac.mu is held.
  807. func (ac *addrConn) errorf(format string, a ...interface{}) {
  808. if ac.events != nil {
  809. ac.events.Errorf(format, a...)
  810. }
  811. }
  812. // resetTransport recreates a transport to the address for ac.
  813. // For the old transport:
  814. // - if drain is true, it will be gracefully closed.
  815. // - otherwise, it will be closed.
  816. // TODO(bar) make sure all state transitions are valid.
  817. func (ac *addrConn) resetTransport(drain bool) error {
  818. ac.mu.Lock()
  819. if ac.state == connectivity.Shutdown {
  820. ac.mu.Unlock()
  821. return errConnClosing
  822. }
  823. oldState := ac.state
  824. ac.state = connectivity.Connecting
  825. ac.csEvltr.recordTransition(oldState, ac.state)
  826. if ac.cc.balancer != nil {
  827. ac.cc.balancer.HandleSubConnStateChange(ac.acbw, ac.state)
  828. }
  829. // TODO(bar) don't call balancer functions to handle subconn state change if ac.acbw is nil.
  830. if ac.ready != nil {
  831. close(ac.ready)
  832. ac.ready = nil
  833. }
  834. t := ac.transport
  835. ac.transport = nil
  836. ac.mu.Unlock()
  837. if t != nil && !drain {
  838. t.Close()
  839. }
  840. ac.cc.mu.RLock()
  841. ac.dopts.copts.KeepaliveParams = ac.cc.mkp
  842. ac.cc.mu.RUnlock()
  843. for retries := 0; ; retries++ {
  844. sleepTime := ac.dopts.bs.backoff(retries)
  845. timeout := minConnectTimeout
  846. ac.mu.Lock()
  847. if timeout < time.Duration(int(sleepTime)/len(ac.addrs)) {
  848. timeout = time.Duration(int(sleepTime) / len(ac.addrs))
  849. }
  850. connectTime := time.Now()
  851. if ac.state == connectivity.Shutdown {
  852. ac.mu.Unlock()
  853. return errConnClosing
  854. }
  855. ac.printf("connecting")
  856. oldState := ac.state
  857. ac.state = connectivity.Connecting
  858. ac.csEvltr.recordTransition(oldState, ac.state)
  859. // TODO(bar) remove condition once we always have a balancer.
  860. if ac.cc.balancer != nil {
  861. ac.cc.balancer.HandleSubConnStateChange(ac.acbw, ac.state)
  862. }
  863. // copy ac.addrs in case of race
  864. addrsIter := make([]resolver.Address, len(ac.addrs))
  865. copy(addrsIter, ac.addrs)
  866. ac.mu.Unlock()
  867. for _, addr := range addrsIter {
  868. ac.mu.Lock()
  869. if ac.state == connectivity.Shutdown {
  870. // ac.tearDown(...) has been invoked.
  871. ac.mu.Unlock()
  872. return errConnClosing
  873. }
  874. ac.mu.Unlock()
  875. ctx, cancel := context.WithTimeout(ac.ctx, timeout)
  876. sinfo := transport.TargetInfo{
  877. Addr: addr.Addr,
  878. Metadata: addr.Metadata,
  879. }
  880. newTransport, err := transport.NewClientTransport(ctx, sinfo, ac.dopts.copts)
  881. // Don't call cancel in success path due to a race in Go 1.6:
  882. // https://github.com/golang/go/issues/15078.
  883. if err != nil {
  884. cancel()
  885. if e, ok := err.(transport.ConnectionError); ok && !e.Temporary() {
  886. return err
  887. }
  888. grpclog.Warningf("grpc: addrConn.resetTransport failed to create client transport: %v; Reconnecting to %v", err, addr)
  889. ac.mu.Lock()
  890. if ac.state == connectivity.Shutdown {
  891. // ac.tearDown(...) has been invoked.
  892. ac.mu.Unlock()
  893. return errConnClosing
  894. }
  895. ac.mu.Unlock()
  896. continue
  897. }
  898. ac.mu.Lock()
  899. ac.printf("ready")
  900. if ac.state == connectivity.Shutdown {
  901. // ac.tearDown(...) has been invoked.
  902. ac.mu.Unlock()
  903. newTransport.Close()
  904. return errConnClosing
  905. }
  906. oldState = ac.state
  907. ac.state = connectivity.Ready
  908. ac.csEvltr.recordTransition(oldState, ac.state)
  909. if ac.cc.balancer != nil {
  910. ac.cc.balancer.HandleSubConnStateChange(ac.acbw, ac.state)
  911. }
  912. ac.transport = newTransport
  913. ac.curAddr = addr
  914. if ac.ready != nil {
  915. close(ac.ready)
  916. ac.ready = nil
  917. }
  918. ac.mu.Unlock()
  919. return nil
  920. }
  921. ac.mu.Lock()
  922. oldState = ac.state
  923. ac.state = connectivity.TransientFailure
  924. ac.csEvltr.recordTransition(oldState, ac.state)
  925. if ac.cc.balancer != nil {
  926. ac.cc.balancer.HandleSubConnStateChange(ac.acbw, ac.state)
  927. }
  928. if ac.ready != nil {
  929. close(ac.ready)
  930. ac.ready = nil
  931. }
  932. ac.mu.Unlock()
  933. timer := time.NewTimer(sleepTime - time.Since(connectTime))
  934. select {
  935. case <-timer.C:
  936. case <-ac.ctx.Done():
  937. timer.Stop()
  938. return ac.ctx.Err()
  939. }
  940. timer.Stop()
  941. }
  942. }
  943. // Run in a goroutine to track the error in transport and create the
  944. // new transport if an error happens. It returns when the channel is closing.
  945. func (ac *addrConn) transportMonitor() {
  946. for {
  947. ac.mu.Lock()
  948. t := ac.transport
  949. ac.mu.Unlock()
  950. select {
  951. // This is needed to detect the teardown when
  952. // the addrConn is idle (i.e., no RPC in flight).
  953. case <-ac.ctx.Done():
  954. select {
  955. case <-t.Error():
  956. t.Close()
  957. default:
  958. }
  959. return
  960. case <-t.GoAway():
  961. ac.adjustParams(t.GetGoAwayReason())
  962. // If GoAway happens without any network I/O error, the underlying transport
  963. // will be gracefully closed, and a new transport will be created.
  964. // (The transport will be closed when all the pending RPCs finished or failed.)
  965. // If GoAway and some network I/O error happen concurrently, the underlying transport
  966. // will be closed, and a new transport will be created.
  967. var drain bool
  968. select {
  969. case <-t.Error():
  970. default:
  971. drain = true
  972. }
  973. if err := ac.resetTransport(drain); err != nil {
  974. grpclog.Infof("get error from resetTransport %v, transportMonitor returning", err)
  975. if err != errConnClosing {
  976. // Keep this ac in cc.conns, to get the reason it's torn down.
  977. ac.tearDown(err)
  978. }
  979. return
  980. }
  981. case <-t.Error():
  982. select {
  983. case <-ac.ctx.Done():
  984. t.Close()
  985. return
  986. case <-t.GoAway():
  987. ac.adjustParams(t.GetGoAwayReason())
  988. default:
  989. }
  990. if err := ac.resetTransport(false); err != nil {
  991. grpclog.Infof("get error from resetTransport %v, transportMonitor returning", err)
  992. ac.mu.Lock()
  993. ac.printf("transport exiting: %v", err)
  994. ac.mu.Unlock()
  995. grpclog.Warningf("grpc: addrConn.transportMonitor exits due to: %v", err)
  996. if err != errConnClosing {
  997. // Keep this ac in cc.conns, to get the reason it's torn down.
  998. ac.tearDown(err)
  999. }
  1000. return
  1001. }
  1002. }
  1003. }
  1004. }
  1005. // wait blocks until i) the new transport is up or ii) ctx is done or iii) ac is closed or
  1006. // iv) transport is in connectivity.TransientFailure and there is a balancer/failfast is true.
  1007. func (ac *addrConn) wait(ctx context.Context, hasBalancer, failfast bool) (transport.ClientTransport, error) {
  1008. for {
  1009. ac.mu.Lock()
  1010. switch {
  1011. case ac.state == connectivity.Shutdown:
  1012. if failfast || !hasBalancer {
  1013. // RPC is failfast or balancer is nil. This RPC should fail with ac.tearDownErr.
  1014. err := ac.tearDownErr
  1015. ac.mu.Unlock()
  1016. return nil, err
  1017. }
  1018. ac.mu.Unlock()
  1019. return nil, errConnClosing
  1020. case ac.state == connectivity.Ready:
  1021. ct := ac.transport
  1022. ac.mu.Unlock()
  1023. return ct, nil
  1024. case ac.state == connectivity.TransientFailure:
  1025. if failfast || hasBalancer {
  1026. ac.mu.Unlock()
  1027. return nil, errConnUnavailable
  1028. }
  1029. }
  1030. ready := ac.ready
  1031. if ready == nil {
  1032. ready = make(chan struct{})
  1033. ac.ready = ready
  1034. }
  1035. ac.mu.Unlock()
  1036. select {
  1037. case <-ctx.Done():
  1038. return nil, toRPCErr(ctx.Err())
  1039. // Wait until the new transport is ready or failed.
  1040. case <-ready:
  1041. }
  1042. }
  1043. }
  1044. // tearDown starts to tear down the addrConn.
  1045. // TODO(zhaoq): Make this synchronous to avoid unbounded memory consumption in
  1046. // some edge cases (e.g., the caller opens and closes many addrConn's in a
  1047. // tight loop.
  1048. // tearDown doesn't remove ac from ac.cc.conns.
  1049. func (ac *addrConn) tearDown(err error) {
  1050. ac.cancel()
  1051. ac.mu.Lock()
  1052. ac.curAddr = resolver.Address{}
  1053. defer ac.mu.Unlock()
  1054. if err == errConnDrain && ac.transport != nil {
  1055. // GracefulClose(...) may be executed multiple times when
  1056. // i) receiving multiple GoAway frames from the server; or
  1057. // ii) there are concurrent name resolver/Balancer triggered
  1058. // address removal and GoAway.
  1059. ac.transport.GracefulClose()
  1060. }
  1061. if ac.state == connectivity.Shutdown {
  1062. return
  1063. }
  1064. oldState := ac.state
  1065. ac.state = connectivity.Shutdown
  1066. ac.tearDownErr = err
  1067. ac.csEvltr.recordTransition(oldState, ac.state)
  1068. if ac.cc.balancer != nil {
  1069. ac.cc.balancer.HandleSubConnStateChange(ac.acbw, ac.state)
  1070. }
  1071. if ac.events != nil {
  1072. ac.events.Finish()
  1073. ac.events = nil
  1074. }
  1075. if ac.ready != nil {
  1076. close(ac.ready)
  1077. ac.ready = nil
  1078. }
  1079. if ac.transport != nil && err != errConnDrain {
  1080. ac.transport.Close()
  1081. }
  1082. return
  1083. }
  1084. func (ac *addrConn) getState() connectivity.State {
  1085. ac.mu.Lock()
  1086. defer ac.mu.Unlock()
  1087. return ac.state
  1088. }