clientconn.go 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908
  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. "context"
  21. "errors"
  22. "fmt"
  23. "math"
  24. "net/url"
  25. "strings"
  26. "sync"
  27. "sync/atomic"
  28. "time"
  29. "google.golang.org/grpc/balancer"
  30. "google.golang.org/grpc/balancer/base"
  31. "google.golang.org/grpc/codes"
  32. "google.golang.org/grpc/connectivity"
  33. "google.golang.org/grpc/credentials"
  34. "google.golang.org/grpc/internal/backoff"
  35. "google.golang.org/grpc/internal/channelz"
  36. "google.golang.org/grpc/internal/grpcsync"
  37. iresolver "google.golang.org/grpc/internal/resolver"
  38. "google.golang.org/grpc/internal/transport"
  39. "google.golang.org/grpc/keepalive"
  40. "google.golang.org/grpc/resolver"
  41. "google.golang.org/grpc/serviceconfig"
  42. "google.golang.org/grpc/status"
  43. _ "google.golang.org/grpc/balancer/roundrobin" // To register roundrobin.
  44. _ "google.golang.org/grpc/internal/resolver/dns" // To register dns resolver.
  45. _ "google.golang.org/grpc/internal/resolver/passthrough" // To register passthrough resolver.
  46. _ "google.golang.org/grpc/internal/resolver/unix" // To register unix resolver.
  47. )
  48. const (
  49. // minimum time to give a connection to complete
  50. minConnectTimeout = 20 * time.Second
  51. // must match grpclbName in grpclb/grpclb.go
  52. grpclbName = "grpclb"
  53. )
  54. var (
  55. // ErrClientConnClosing indicates that the operation is illegal because
  56. // the ClientConn is closing.
  57. //
  58. // Deprecated: this error should not be relied upon by users; use the status
  59. // code of Canceled instead.
  60. ErrClientConnClosing = status.Error(codes.Canceled, "grpc: the client connection is closing")
  61. // errConnDrain indicates that the connection starts to be drained and does not accept any new RPCs.
  62. errConnDrain = errors.New("grpc: the connection is drained")
  63. // errConnClosing indicates that the connection is closing.
  64. errConnClosing = errors.New("grpc: the connection is closing")
  65. // errConnIdling indicates the the connection is being closed as the channel
  66. // is moving to an idle mode due to inactivity.
  67. errConnIdling = errors.New("grpc: the connection is closing due to channel idleness")
  68. // invalidDefaultServiceConfigErrPrefix is used to prefix the json parsing error for the default
  69. // service config.
  70. invalidDefaultServiceConfigErrPrefix = "grpc: the provided default service config is invalid"
  71. )
  72. // The following errors are returned from Dial and DialContext
  73. var (
  74. // errNoTransportSecurity indicates that there is no transport security
  75. // being set for ClientConn. Users should either set one or explicitly
  76. // call WithInsecure DialOption to disable security.
  77. errNoTransportSecurity = errors.New("grpc: no transport security set (use grpc.WithTransportCredentials(insecure.NewCredentials()) explicitly or set credentials)")
  78. // errTransportCredsAndBundle indicates that creds bundle is used together
  79. // with other individual Transport Credentials.
  80. errTransportCredsAndBundle = errors.New("grpc: credentials.Bundle may not be used with individual TransportCredentials")
  81. // errNoTransportCredsInBundle indicated that the configured creds bundle
  82. // returned a transport credentials which was nil.
  83. errNoTransportCredsInBundle = errors.New("grpc: credentials.Bundle must return non-nil transport credentials")
  84. // errTransportCredentialsMissing indicates that users want to transmit
  85. // security information (e.g., OAuth2 token) which requires secure
  86. // connection on an insecure connection.
  87. errTransportCredentialsMissing = errors.New("grpc: the credentials require transport level security (use grpc.WithTransportCredentials() to set)")
  88. )
  89. const (
  90. defaultClientMaxReceiveMessageSize = 1024 * 1024 * 4
  91. defaultClientMaxSendMessageSize = math.MaxInt32
  92. // http2IOBufSize specifies the buffer size for sending frames.
  93. defaultWriteBufSize = 32 * 1024
  94. defaultReadBufSize = 32 * 1024
  95. )
  96. // Dial creates a client connection to the given target.
  97. func Dial(target string, opts ...DialOption) (*ClientConn, error) {
  98. return DialContext(context.Background(), target, opts...)
  99. }
  100. type defaultConfigSelector struct {
  101. sc *ServiceConfig
  102. }
  103. func (dcs *defaultConfigSelector) SelectConfig(rpcInfo iresolver.RPCInfo) (*iresolver.RPCConfig, error) {
  104. return &iresolver.RPCConfig{
  105. Context: rpcInfo.Context,
  106. MethodConfig: getMethodConfig(dcs.sc, rpcInfo.Method),
  107. }, nil
  108. }
  109. // DialContext creates a client connection to the given target. By default, it's
  110. // a non-blocking dial (the function won't wait for connections to be
  111. // established, and connecting happens in the background). To make it a blocking
  112. // dial, use WithBlock() dial option.
  113. //
  114. // In the non-blocking case, the ctx does not act against the connection. It
  115. // only controls the setup steps.
  116. //
  117. // In the blocking case, ctx can be used to cancel or expire the pending
  118. // connection. Once this function returns, the cancellation and expiration of
  119. // ctx will be noop. Users should call ClientConn.Close to terminate all the
  120. // pending operations after this function returns.
  121. //
  122. // The target name syntax is defined in
  123. // https://github.com/grpc/grpc/blob/master/doc/naming.md.
  124. // e.g. to use dns resolver, a "dns:///" prefix should be applied to the target.
  125. func DialContext(ctx context.Context, target string, opts ...DialOption) (conn *ClientConn, err error) {
  126. cc := &ClientConn{
  127. target: target,
  128. csMgr: &connectivityStateManager{},
  129. conns: make(map[*addrConn]struct{}),
  130. dopts: defaultDialOptions(),
  131. czData: new(channelzData),
  132. }
  133. // We start the channel off in idle mode, but kick it out of idle at the end
  134. // of this method, instead of waiting for the first RPC. Other gRPC
  135. // implementations do wait for the first RPC to kick the channel out of
  136. // idle. But doing so would be a major behavior change for our users who are
  137. // used to seeing the channel active after Dial.
  138. //
  139. // Taking this approach of kicking it out of idle at the end of this method
  140. // allows us to share the code between channel creation and exiting idle
  141. // mode. This will also make it easy for us to switch to starting the
  142. // channel off in idle, if at all we ever get to do that.
  143. cc.idlenessState = ccIdlenessStateIdle
  144. cc.retryThrottler.Store((*retryThrottler)(nil))
  145. cc.safeConfigSelector.UpdateConfigSelector(&defaultConfigSelector{nil})
  146. cc.ctx, cc.cancel = context.WithCancel(context.Background())
  147. cc.exitIdleCond = sync.NewCond(&cc.mu)
  148. disableGlobalOpts := false
  149. for _, opt := range opts {
  150. if _, ok := opt.(*disableGlobalDialOptions); ok {
  151. disableGlobalOpts = true
  152. break
  153. }
  154. }
  155. if !disableGlobalOpts {
  156. for _, opt := range globalDialOptions {
  157. opt.apply(&cc.dopts)
  158. }
  159. }
  160. for _, opt := range opts {
  161. opt.apply(&cc.dopts)
  162. }
  163. chainUnaryClientInterceptors(cc)
  164. chainStreamClientInterceptors(cc)
  165. defer func() {
  166. if err != nil {
  167. cc.Close()
  168. }
  169. }()
  170. // Register ClientConn with channelz.
  171. cc.channelzRegistration(target)
  172. if err := cc.validateTransportCredentials(); err != nil {
  173. return nil, err
  174. }
  175. if cc.dopts.defaultServiceConfigRawJSON != nil {
  176. scpr := parseServiceConfig(*cc.dopts.defaultServiceConfigRawJSON)
  177. if scpr.Err != nil {
  178. return nil, fmt.Errorf("%s: %v", invalidDefaultServiceConfigErrPrefix, scpr.Err)
  179. }
  180. cc.dopts.defaultServiceConfig, _ = scpr.Config.(*ServiceConfig)
  181. }
  182. cc.mkp = cc.dopts.copts.KeepaliveParams
  183. if cc.dopts.copts.UserAgent != "" {
  184. cc.dopts.copts.UserAgent += " " + grpcUA
  185. } else {
  186. cc.dopts.copts.UserAgent = grpcUA
  187. }
  188. if cc.dopts.timeout > 0 {
  189. var cancel context.CancelFunc
  190. ctx, cancel = context.WithTimeout(ctx, cc.dopts.timeout)
  191. defer cancel()
  192. }
  193. defer func() {
  194. select {
  195. case <-ctx.Done():
  196. switch {
  197. case ctx.Err() == err:
  198. conn = nil
  199. case err == nil || !cc.dopts.returnLastError:
  200. conn, err = nil, ctx.Err()
  201. default:
  202. conn, err = nil, fmt.Errorf("%v: %v", ctx.Err(), err)
  203. }
  204. default:
  205. }
  206. }()
  207. if cc.dopts.bs == nil {
  208. cc.dopts.bs = backoff.DefaultExponential
  209. }
  210. // Determine the resolver to use.
  211. if err := cc.parseTargetAndFindResolver(); err != nil {
  212. return nil, err
  213. }
  214. if err = cc.determineAuthority(); err != nil {
  215. return nil, err
  216. }
  217. if cc.dopts.scChan != nil {
  218. // Blocking wait for the initial service config.
  219. select {
  220. case sc, ok := <-cc.dopts.scChan:
  221. if ok {
  222. cc.sc = &sc
  223. cc.safeConfigSelector.UpdateConfigSelector(&defaultConfigSelector{&sc})
  224. }
  225. case <-ctx.Done():
  226. return nil, ctx.Err()
  227. }
  228. }
  229. if cc.dopts.scChan != nil {
  230. go cc.scWatcher()
  231. }
  232. // This creates the name resolver, load balancer, blocking picker etc.
  233. if err := cc.exitIdleMode(); err != nil {
  234. return nil, err
  235. }
  236. // Configure idleness support with configured idle timeout or default idle
  237. // timeout duration. Idleness can be explicitly disabled by the user, by
  238. // setting the dial option to 0.
  239. cc.idlenessMgr = newIdlenessManager(cc, cc.dopts.idleTimeout)
  240. // Return early for non-blocking dials.
  241. if !cc.dopts.block {
  242. return cc, nil
  243. }
  244. // A blocking dial blocks until the clientConn is ready.
  245. for {
  246. s := cc.GetState()
  247. if s == connectivity.Idle {
  248. cc.Connect()
  249. }
  250. if s == connectivity.Ready {
  251. return cc, nil
  252. } else if cc.dopts.copts.FailOnNonTempDialError && s == connectivity.TransientFailure {
  253. if err = cc.connectionError(); err != nil {
  254. terr, ok := err.(interface {
  255. Temporary() bool
  256. })
  257. if ok && !terr.Temporary() {
  258. return nil, err
  259. }
  260. }
  261. }
  262. if !cc.WaitForStateChange(ctx, s) {
  263. // ctx got timeout or canceled.
  264. if err = cc.connectionError(); err != nil && cc.dopts.returnLastError {
  265. return nil, err
  266. }
  267. return nil, ctx.Err()
  268. }
  269. }
  270. }
  271. // addTraceEvent is a helper method to add a trace event on the channel. If the
  272. // channel is a nested one, the same event is also added on the parent channel.
  273. func (cc *ClientConn) addTraceEvent(msg string) {
  274. ted := &channelz.TraceEventDesc{
  275. Desc: fmt.Sprintf("Channel %s", msg),
  276. Severity: channelz.CtInfo,
  277. }
  278. if cc.dopts.channelzParentID != nil {
  279. ted.Parent = &channelz.TraceEventDesc{
  280. Desc: fmt.Sprintf("Nested channel(id:%d) %s", cc.channelzID.Int(), msg),
  281. Severity: channelz.CtInfo,
  282. }
  283. }
  284. channelz.AddTraceEvent(logger, cc.channelzID, 0, ted)
  285. }
  286. // exitIdleMode moves the channel out of idle mode by recreating the name
  287. // resolver and load balancer.
  288. func (cc *ClientConn) exitIdleMode() error {
  289. cc.mu.Lock()
  290. if cc.conns == nil {
  291. cc.mu.Unlock()
  292. return errConnClosing
  293. }
  294. if cc.idlenessState != ccIdlenessStateIdle {
  295. cc.mu.Unlock()
  296. logger.Info("ClientConn asked to exit idle mode when not in idle mode")
  297. return nil
  298. }
  299. defer func() {
  300. // When Close() and exitIdleMode() race against each other, one of the
  301. // following two can happen:
  302. // - Close() wins the race and runs first. exitIdleMode() runs after, and
  303. // sees that the ClientConn is already closed and hence returns early.
  304. // - exitIdleMode() wins the race and runs first and recreates the balancer
  305. // and releases the lock before recreating the resolver. If Close() runs
  306. // in this window, it will wait for exitIdleMode to complete.
  307. //
  308. // We achieve this synchronization using the below condition variable.
  309. cc.mu.Lock()
  310. cc.idlenessState = ccIdlenessStateActive
  311. cc.exitIdleCond.Signal()
  312. cc.mu.Unlock()
  313. }()
  314. cc.idlenessState = ccIdlenessStateExitingIdle
  315. exitedIdle := false
  316. if cc.blockingpicker == nil {
  317. cc.blockingpicker = newPickerWrapper()
  318. } else {
  319. cc.blockingpicker.exitIdleMode()
  320. exitedIdle = true
  321. }
  322. var credsClone credentials.TransportCredentials
  323. if creds := cc.dopts.copts.TransportCredentials; creds != nil {
  324. credsClone = creds.Clone()
  325. }
  326. if cc.balancerWrapper == nil {
  327. cc.balancerWrapper = newCCBalancerWrapper(cc, balancer.BuildOptions{
  328. DialCreds: credsClone,
  329. CredsBundle: cc.dopts.copts.CredsBundle,
  330. Dialer: cc.dopts.copts.Dialer,
  331. Authority: cc.authority,
  332. CustomUserAgent: cc.dopts.copts.UserAgent,
  333. ChannelzParentID: cc.channelzID,
  334. Target: cc.parsedTarget,
  335. })
  336. } else {
  337. cc.balancerWrapper.exitIdleMode()
  338. }
  339. cc.firstResolveEvent = grpcsync.NewEvent()
  340. cc.mu.Unlock()
  341. // This needs to be called without cc.mu because this builds a new resolver
  342. // which might update state or report error inline which needs to be handled
  343. // by cc.updateResolverState() which also grabs cc.mu.
  344. if err := cc.initResolverWrapper(credsClone); err != nil {
  345. return err
  346. }
  347. if exitedIdle {
  348. cc.addTraceEvent("exiting idle mode")
  349. }
  350. return nil
  351. }
  352. // enterIdleMode puts the channel in idle mode, and as part of it shuts down the
  353. // name resolver, load balancer and any subchannels.
  354. func (cc *ClientConn) enterIdleMode() error {
  355. cc.mu.Lock()
  356. if cc.conns == nil {
  357. cc.mu.Unlock()
  358. return ErrClientConnClosing
  359. }
  360. if cc.idlenessState != ccIdlenessStateActive {
  361. logger.Error("ClientConn asked to enter idle mode when not active")
  362. return nil
  363. }
  364. // cc.conns == nil is a proxy for the ClientConn being closed. So, instead
  365. // of setting it to nil here, we recreate the map. This also means that we
  366. // don't have to do this when exiting idle mode.
  367. conns := cc.conns
  368. cc.conns = make(map[*addrConn]struct{})
  369. // TODO: Currently, we close the resolver wrapper upon entering idle mode
  370. // and create a new one upon exiting idle mode. This means that the
  371. // `cc.resolverWrapper` field would be overwritten everytime we exit idle
  372. // mode. While this means that we need to hold `cc.mu` when accessing
  373. // `cc.resolverWrapper`, it makes the code simpler in the wrapper. We should
  374. // try to do the same for the balancer and picker wrappers too.
  375. cc.resolverWrapper.close()
  376. cc.blockingpicker.enterIdleMode()
  377. cc.balancerWrapper.enterIdleMode()
  378. cc.csMgr.updateState(connectivity.Idle)
  379. cc.idlenessState = ccIdlenessStateIdle
  380. cc.mu.Unlock()
  381. go func() {
  382. cc.addTraceEvent("entering idle mode")
  383. for ac := range conns {
  384. ac.tearDown(errConnIdling)
  385. }
  386. }()
  387. return nil
  388. }
  389. // validateTransportCredentials performs a series of checks on the configured
  390. // transport credentials. It returns a non-nil error if any of these conditions
  391. // are met:
  392. // - no transport creds and no creds bundle is configured
  393. // - both transport creds and creds bundle are configured
  394. // - creds bundle is configured, but it lacks a transport credentials
  395. // - insecure transport creds configured alongside call creds that require
  396. // transport level security
  397. //
  398. // If none of the above conditions are met, the configured credentials are
  399. // deemed valid and a nil error is returned.
  400. func (cc *ClientConn) validateTransportCredentials() error {
  401. if cc.dopts.copts.TransportCredentials == nil && cc.dopts.copts.CredsBundle == nil {
  402. return errNoTransportSecurity
  403. }
  404. if cc.dopts.copts.TransportCredentials != nil && cc.dopts.copts.CredsBundle != nil {
  405. return errTransportCredsAndBundle
  406. }
  407. if cc.dopts.copts.CredsBundle != nil && cc.dopts.copts.CredsBundle.TransportCredentials() == nil {
  408. return errNoTransportCredsInBundle
  409. }
  410. transportCreds := cc.dopts.copts.TransportCredentials
  411. if transportCreds == nil {
  412. transportCreds = cc.dopts.copts.CredsBundle.TransportCredentials()
  413. }
  414. if transportCreds.Info().SecurityProtocol == "insecure" {
  415. for _, cd := range cc.dopts.copts.PerRPCCredentials {
  416. if cd.RequireTransportSecurity() {
  417. return errTransportCredentialsMissing
  418. }
  419. }
  420. }
  421. return nil
  422. }
  423. // channelzRegistration registers the newly created ClientConn with channelz and
  424. // stores the returned identifier in `cc.channelzID` and `cc.csMgr.channelzID`.
  425. // A channelz trace event is emitted for ClientConn creation. If the newly
  426. // created ClientConn is a nested one, i.e a valid parent ClientConn ID is
  427. // specified via a dial option, the trace event is also added to the parent.
  428. //
  429. // Doesn't grab cc.mu as this method is expected to be called only at Dial time.
  430. func (cc *ClientConn) channelzRegistration(target string) {
  431. cc.channelzID = channelz.RegisterChannel(&channelzChannel{cc}, cc.dopts.channelzParentID, target)
  432. cc.addTraceEvent("created")
  433. cc.csMgr.channelzID = cc.channelzID
  434. }
  435. // chainUnaryClientInterceptors chains all unary client interceptors into one.
  436. func chainUnaryClientInterceptors(cc *ClientConn) {
  437. interceptors := cc.dopts.chainUnaryInts
  438. // Prepend dopts.unaryInt to the chaining interceptors if it exists, since unaryInt will
  439. // be executed before any other chained interceptors.
  440. if cc.dopts.unaryInt != nil {
  441. interceptors = append([]UnaryClientInterceptor{cc.dopts.unaryInt}, interceptors...)
  442. }
  443. var chainedInt UnaryClientInterceptor
  444. if len(interceptors) == 0 {
  445. chainedInt = nil
  446. } else if len(interceptors) == 1 {
  447. chainedInt = interceptors[0]
  448. } else {
  449. chainedInt = func(ctx context.Context, method string, req, reply interface{}, cc *ClientConn, invoker UnaryInvoker, opts ...CallOption) error {
  450. return interceptors[0](ctx, method, req, reply, cc, getChainUnaryInvoker(interceptors, 0, invoker), opts...)
  451. }
  452. }
  453. cc.dopts.unaryInt = chainedInt
  454. }
  455. // getChainUnaryInvoker recursively generate the chained unary invoker.
  456. func getChainUnaryInvoker(interceptors []UnaryClientInterceptor, curr int, finalInvoker UnaryInvoker) UnaryInvoker {
  457. if curr == len(interceptors)-1 {
  458. return finalInvoker
  459. }
  460. return func(ctx context.Context, method string, req, reply interface{}, cc *ClientConn, opts ...CallOption) error {
  461. return interceptors[curr+1](ctx, method, req, reply, cc, getChainUnaryInvoker(interceptors, curr+1, finalInvoker), opts...)
  462. }
  463. }
  464. // chainStreamClientInterceptors chains all stream client interceptors into one.
  465. func chainStreamClientInterceptors(cc *ClientConn) {
  466. interceptors := cc.dopts.chainStreamInts
  467. // Prepend dopts.streamInt to the chaining interceptors if it exists, since streamInt will
  468. // be executed before any other chained interceptors.
  469. if cc.dopts.streamInt != nil {
  470. interceptors = append([]StreamClientInterceptor{cc.dopts.streamInt}, interceptors...)
  471. }
  472. var chainedInt StreamClientInterceptor
  473. if len(interceptors) == 0 {
  474. chainedInt = nil
  475. } else if len(interceptors) == 1 {
  476. chainedInt = interceptors[0]
  477. } else {
  478. chainedInt = func(ctx context.Context, desc *StreamDesc, cc *ClientConn, method string, streamer Streamer, opts ...CallOption) (ClientStream, error) {
  479. return interceptors[0](ctx, desc, cc, method, getChainStreamer(interceptors, 0, streamer), opts...)
  480. }
  481. }
  482. cc.dopts.streamInt = chainedInt
  483. }
  484. // getChainStreamer recursively generate the chained client stream constructor.
  485. func getChainStreamer(interceptors []StreamClientInterceptor, curr int, finalStreamer Streamer) Streamer {
  486. if curr == len(interceptors)-1 {
  487. return finalStreamer
  488. }
  489. return func(ctx context.Context, desc *StreamDesc, cc *ClientConn, method string, opts ...CallOption) (ClientStream, error) {
  490. return interceptors[curr+1](ctx, desc, cc, method, getChainStreamer(interceptors, curr+1, finalStreamer), opts...)
  491. }
  492. }
  493. // connectivityStateManager keeps the connectivity.State of ClientConn.
  494. // This struct will eventually be exported so the balancers can access it.
  495. type connectivityStateManager struct {
  496. mu sync.Mutex
  497. state connectivity.State
  498. notifyChan chan struct{}
  499. channelzID *channelz.Identifier
  500. }
  501. // updateState updates the connectivity.State of ClientConn.
  502. // If there's a change it notifies goroutines waiting on state change to
  503. // happen.
  504. func (csm *connectivityStateManager) updateState(state connectivity.State) {
  505. csm.mu.Lock()
  506. defer csm.mu.Unlock()
  507. if csm.state == connectivity.Shutdown {
  508. return
  509. }
  510. if csm.state == state {
  511. return
  512. }
  513. csm.state = state
  514. channelz.Infof(logger, csm.channelzID, "Channel Connectivity change to %v", state)
  515. if csm.notifyChan != nil {
  516. // There are other goroutines waiting on this channel.
  517. close(csm.notifyChan)
  518. csm.notifyChan = nil
  519. }
  520. }
  521. func (csm *connectivityStateManager) getState() connectivity.State {
  522. csm.mu.Lock()
  523. defer csm.mu.Unlock()
  524. return csm.state
  525. }
  526. func (csm *connectivityStateManager) getNotifyChan() <-chan struct{} {
  527. csm.mu.Lock()
  528. defer csm.mu.Unlock()
  529. if csm.notifyChan == nil {
  530. csm.notifyChan = make(chan struct{})
  531. }
  532. return csm.notifyChan
  533. }
  534. // ClientConnInterface defines the functions clients need to perform unary and
  535. // streaming RPCs. It is implemented by *ClientConn, and is only intended to
  536. // be referenced by generated code.
  537. type ClientConnInterface interface {
  538. // Invoke performs a unary RPC and returns after the response is received
  539. // into reply.
  540. Invoke(ctx context.Context, method string, args interface{}, reply interface{}, opts ...CallOption) error
  541. // NewStream begins a streaming RPC.
  542. NewStream(ctx context.Context, desc *StreamDesc, method string, opts ...CallOption) (ClientStream, error)
  543. }
  544. // Assert *ClientConn implements ClientConnInterface.
  545. var _ ClientConnInterface = (*ClientConn)(nil)
  546. // ClientConn represents a virtual connection to a conceptual endpoint, to
  547. // perform RPCs.
  548. //
  549. // A ClientConn is free to have zero or more actual connections to the endpoint
  550. // based on configuration, load, etc. It is also free to determine which actual
  551. // endpoints to use and may change it every RPC, permitting client-side load
  552. // balancing.
  553. //
  554. // A ClientConn encapsulates a range of functionality including name
  555. // resolution, TCP connection establishment (with retries and backoff) and TLS
  556. // handshakes. It also handles errors on established connections by
  557. // re-resolving the name and reconnecting.
  558. type ClientConn struct {
  559. ctx context.Context // Initialized using the background context at dial time.
  560. cancel context.CancelFunc // Cancelled on close.
  561. // The following are initialized at dial time, and are read-only after that.
  562. target string // User's dial target.
  563. parsedTarget resolver.Target // See parseTargetAndFindResolver().
  564. authority string // See determineAuthority().
  565. dopts dialOptions // Default and user specified dial options.
  566. channelzID *channelz.Identifier // Channelz identifier for the channel.
  567. resolverBuilder resolver.Builder // See parseTargetAndFindResolver().
  568. balancerWrapper *ccBalancerWrapper // Uses gracefulswitch.balancer underneath.
  569. idlenessMgr idlenessManager
  570. // The following provide their own synchronization, and therefore don't
  571. // require cc.mu to be held to access them.
  572. csMgr *connectivityStateManager
  573. blockingpicker *pickerWrapper
  574. safeConfigSelector iresolver.SafeConfigSelector
  575. czData *channelzData
  576. retryThrottler atomic.Value // Updated from service config.
  577. // firstResolveEvent is used to track whether the name resolver sent us at
  578. // least one update. RPCs block on this event.
  579. firstResolveEvent *grpcsync.Event
  580. // mu protects the following fields.
  581. // TODO: split mu so the same mutex isn't used for everything.
  582. mu sync.RWMutex
  583. resolverWrapper *ccResolverWrapper // Initialized in Dial; cleared in Close.
  584. sc *ServiceConfig // Latest service config received from the resolver.
  585. conns map[*addrConn]struct{} // Set to nil on close.
  586. mkp keepalive.ClientParameters // May be updated upon receipt of a GoAway.
  587. idlenessState ccIdlenessState // Tracks idleness state of the channel.
  588. exitIdleCond *sync.Cond // Signalled when channel exits idle.
  589. lceMu sync.Mutex // protects lastConnectionError
  590. lastConnectionError error
  591. }
  592. // ccIdlenessState tracks the idleness state of the channel.
  593. //
  594. // Channels start off in `active` and move to `idle` after a period of
  595. // inactivity. When moving back to `active` upon an incoming RPC, they
  596. // transition through `exiting_idle`. This state is useful for synchronization
  597. // with Close().
  598. //
  599. // This state tracking is mostly for self-protection. The idlenessManager is
  600. // expected to keep track of the state as well, and is expected not to call into
  601. // the ClientConn unnecessarily.
  602. type ccIdlenessState int8
  603. const (
  604. ccIdlenessStateActive ccIdlenessState = iota
  605. ccIdlenessStateIdle
  606. ccIdlenessStateExitingIdle
  607. )
  608. // WaitForStateChange waits until the connectivity.State of ClientConn changes from sourceState or
  609. // ctx expires. A true value is returned in former case and false in latter.
  610. //
  611. // # Experimental
  612. //
  613. // Notice: This API is EXPERIMENTAL and may be changed or removed in a
  614. // later release.
  615. func (cc *ClientConn) WaitForStateChange(ctx context.Context, sourceState connectivity.State) bool {
  616. ch := cc.csMgr.getNotifyChan()
  617. if cc.csMgr.getState() != sourceState {
  618. return true
  619. }
  620. select {
  621. case <-ctx.Done():
  622. return false
  623. case <-ch:
  624. return true
  625. }
  626. }
  627. // GetState returns the connectivity.State of ClientConn.
  628. //
  629. // # Experimental
  630. //
  631. // Notice: This API is EXPERIMENTAL and may be changed or removed in a later
  632. // release.
  633. func (cc *ClientConn) GetState() connectivity.State {
  634. return cc.csMgr.getState()
  635. }
  636. // Connect causes all subchannels in the ClientConn to attempt to connect if
  637. // the channel is idle. Does not wait for the connection attempts to begin
  638. // before returning.
  639. //
  640. // # Experimental
  641. //
  642. // Notice: This API is EXPERIMENTAL and may be changed or removed in a later
  643. // release.
  644. func (cc *ClientConn) Connect() {
  645. cc.exitIdleMode()
  646. // If the ClientConn was not in idle mode, we need to call ExitIdle on the
  647. // LB policy so that connections can be created.
  648. cc.balancerWrapper.exitIdleMode()
  649. }
  650. func (cc *ClientConn) scWatcher() {
  651. for {
  652. select {
  653. case sc, ok := <-cc.dopts.scChan:
  654. if !ok {
  655. return
  656. }
  657. cc.mu.Lock()
  658. // TODO: load balance policy runtime change is ignored.
  659. // We may revisit this decision in the future.
  660. cc.sc = &sc
  661. cc.safeConfigSelector.UpdateConfigSelector(&defaultConfigSelector{&sc})
  662. cc.mu.Unlock()
  663. case <-cc.ctx.Done():
  664. return
  665. }
  666. }
  667. }
  668. // waitForResolvedAddrs blocks until the resolver has provided addresses or the
  669. // context expires. Returns nil unless the context expires first; otherwise
  670. // returns a status error based on the context.
  671. func (cc *ClientConn) waitForResolvedAddrs(ctx context.Context) error {
  672. // This is on the RPC path, so we use a fast path to avoid the
  673. // more-expensive "select" below after the resolver has returned once.
  674. if cc.firstResolveEvent.HasFired() {
  675. return nil
  676. }
  677. select {
  678. case <-cc.firstResolveEvent.Done():
  679. return nil
  680. case <-ctx.Done():
  681. return status.FromContextError(ctx.Err()).Err()
  682. case <-cc.ctx.Done():
  683. return ErrClientConnClosing
  684. }
  685. }
  686. var emptyServiceConfig *ServiceConfig
  687. func init() {
  688. cfg := parseServiceConfig("{}")
  689. if cfg.Err != nil {
  690. panic(fmt.Sprintf("impossible error parsing empty service config: %v", cfg.Err))
  691. }
  692. emptyServiceConfig = cfg.Config.(*ServiceConfig)
  693. }
  694. func (cc *ClientConn) maybeApplyDefaultServiceConfig(addrs []resolver.Address) {
  695. if cc.sc != nil {
  696. cc.applyServiceConfigAndBalancer(cc.sc, nil, addrs)
  697. return
  698. }
  699. if cc.dopts.defaultServiceConfig != nil {
  700. cc.applyServiceConfigAndBalancer(cc.dopts.defaultServiceConfig, &defaultConfigSelector{cc.dopts.defaultServiceConfig}, addrs)
  701. } else {
  702. cc.applyServiceConfigAndBalancer(emptyServiceConfig, &defaultConfigSelector{emptyServiceConfig}, addrs)
  703. }
  704. }
  705. func (cc *ClientConn) updateResolverState(s resolver.State, err error) error {
  706. defer cc.firstResolveEvent.Fire()
  707. cc.mu.Lock()
  708. // Check if the ClientConn is already closed. Some fields (e.g.
  709. // balancerWrapper) are set to nil when closing the ClientConn, and could
  710. // cause nil pointer panic if we don't have this check.
  711. if cc.conns == nil {
  712. cc.mu.Unlock()
  713. return nil
  714. }
  715. if err != nil {
  716. // May need to apply the initial service config in case the resolver
  717. // doesn't support service configs, or doesn't provide a service config
  718. // with the new addresses.
  719. cc.maybeApplyDefaultServiceConfig(nil)
  720. cc.balancerWrapper.resolverError(err)
  721. // No addresses are valid with err set; return early.
  722. cc.mu.Unlock()
  723. return balancer.ErrBadResolverState
  724. }
  725. var ret error
  726. if cc.dopts.disableServiceConfig {
  727. channelz.Infof(logger, cc.channelzID, "ignoring service config from resolver (%v) and applying the default because service config is disabled", s.ServiceConfig)
  728. cc.maybeApplyDefaultServiceConfig(s.Addresses)
  729. } else if s.ServiceConfig == nil {
  730. cc.maybeApplyDefaultServiceConfig(s.Addresses)
  731. // TODO: do we need to apply a failing LB policy if there is no
  732. // default, per the error handling design?
  733. } else {
  734. if sc, ok := s.ServiceConfig.Config.(*ServiceConfig); s.ServiceConfig.Err == nil && ok {
  735. configSelector := iresolver.GetConfigSelector(s)
  736. if configSelector != nil {
  737. if len(s.ServiceConfig.Config.(*ServiceConfig).Methods) != 0 {
  738. channelz.Infof(logger, cc.channelzID, "method configs in service config will be ignored due to presence of config selector")
  739. }
  740. } else {
  741. configSelector = &defaultConfigSelector{sc}
  742. }
  743. cc.applyServiceConfigAndBalancer(sc, configSelector, s.Addresses)
  744. } else {
  745. ret = balancer.ErrBadResolverState
  746. if cc.sc == nil {
  747. // Apply the failing LB only if we haven't received valid service config
  748. // from the name resolver in the past.
  749. cc.applyFailingLB(s.ServiceConfig)
  750. cc.mu.Unlock()
  751. return ret
  752. }
  753. }
  754. }
  755. var balCfg serviceconfig.LoadBalancingConfig
  756. if cc.sc != nil && cc.sc.lbConfig != nil {
  757. balCfg = cc.sc.lbConfig.cfg
  758. }
  759. bw := cc.balancerWrapper
  760. cc.mu.Unlock()
  761. uccsErr := bw.updateClientConnState(&balancer.ClientConnState{ResolverState: s, BalancerConfig: balCfg})
  762. if ret == nil {
  763. ret = uccsErr // prefer ErrBadResolver state since any other error is
  764. // currently meaningless to the caller.
  765. }
  766. return ret
  767. }
  768. // applyFailingLB is akin to configuring an LB policy on the channel which
  769. // always fails RPCs. Here, an actual LB policy is not configured, but an always
  770. // erroring picker is configured, which returns errors with information about
  771. // what was invalid in the received service config. A config selector with no
  772. // service config is configured, and the connectivity state of the channel is
  773. // set to TransientFailure.
  774. //
  775. // Caller must hold cc.mu.
  776. func (cc *ClientConn) applyFailingLB(sc *serviceconfig.ParseResult) {
  777. var err error
  778. if sc.Err != nil {
  779. err = status.Errorf(codes.Unavailable, "error parsing service config: %v", sc.Err)
  780. } else {
  781. err = status.Errorf(codes.Unavailable, "illegal service config type: %T", sc.Config)
  782. }
  783. cc.safeConfigSelector.UpdateConfigSelector(&defaultConfigSelector{nil})
  784. cc.blockingpicker.updatePicker(base.NewErrPicker(err))
  785. cc.csMgr.updateState(connectivity.TransientFailure)
  786. }
  787. func (cc *ClientConn) handleSubConnStateChange(sc balancer.SubConn, s connectivity.State, err error) {
  788. cc.balancerWrapper.updateSubConnState(sc, s, err)
  789. }
  790. // newAddrConn creates an addrConn for addrs and adds it to cc.conns.
  791. //
  792. // Caller needs to make sure len(addrs) > 0.
  793. func (cc *ClientConn) newAddrConn(addrs []resolver.Address, opts balancer.NewSubConnOptions) (*addrConn, error) {
  794. ac := &addrConn{
  795. state: connectivity.Idle,
  796. cc: cc,
  797. addrs: addrs,
  798. scopts: opts,
  799. dopts: cc.dopts,
  800. czData: new(channelzData),
  801. resetBackoff: make(chan struct{}),
  802. stateChan: make(chan struct{}),
  803. }
  804. ac.ctx, ac.cancel = context.WithCancel(cc.ctx)
  805. // Track ac in cc. This needs to be done before any getTransport(...) is called.
  806. cc.mu.Lock()
  807. defer cc.mu.Unlock()
  808. if cc.conns == nil {
  809. return nil, ErrClientConnClosing
  810. }
  811. var err error
  812. ac.channelzID, err = channelz.RegisterSubChannel(ac, cc.channelzID, "")
  813. if err != nil {
  814. return nil, err
  815. }
  816. channelz.AddTraceEvent(logger, ac.channelzID, 0, &channelz.TraceEventDesc{
  817. Desc: "Subchannel created",
  818. Severity: channelz.CtInfo,
  819. Parent: &channelz.TraceEventDesc{
  820. Desc: fmt.Sprintf("Subchannel(id:%d) created", ac.channelzID.Int()),
  821. Severity: channelz.CtInfo,
  822. },
  823. })
  824. cc.conns[ac] = struct{}{}
  825. return ac, nil
  826. }
  827. // removeAddrConn removes the addrConn in the subConn from clientConn.
  828. // It also tears down the ac with the given error.
  829. func (cc *ClientConn) removeAddrConn(ac *addrConn, err error) {
  830. cc.mu.Lock()
  831. if cc.conns == nil {
  832. cc.mu.Unlock()
  833. return
  834. }
  835. delete(cc.conns, ac)
  836. cc.mu.Unlock()
  837. ac.tearDown(err)
  838. }
  839. func (cc *ClientConn) channelzMetric() *channelz.ChannelInternalMetric {
  840. return &channelz.ChannelInternalMetric{
  841. State: cc.GetState(),
  842. Target: cc.target,
  843. CallsStarted: atomic.LoadInt64(&cc.czData.callsStarted),
  844. CallsSucceeded: atomic.LoadInt64(&cc.czData.callsSucceeded),
  845. CallsFailed: atomic.LoadInt64(&cc.czData.callsFailed),
  846. LastCallStartedTimestamp: time.Unix(0, atomic.LoadInt64(&cc.czData.lastCallStartedTime)),
  847. }
  848. }
  849. // Target returns the target string of the ClientConn.
  850. //
  851. // # Experimental
  852. //
  853. // Notice: This API is EXPERIMENTAL and may be changed or removed in a
  854. // later release.
  855. func (cc *ClientConn) Target() string {
  856. return cc.target
  857. }
  858. func (cc *ClientConn) incrCallsStarted() {
  859. atomic.AddInt64(&cc.czData.callsStarted, 1)
  860. atomic.StoreInt64(&cc.czData.lastCallStartedTime, time.Now().UnixNano())
  861. }
  862. func (cc *ClientConn) incrCallsSucceeded() {
  863. atomic.AddInt64(&cc.czData.callsSucceeded, 1)
  864. }
  865. func (cc *ClientConn) incrCallsFailed() {
  866. atomic.AddInt64(&cc.czData.callsFailed, 1)
  867. }
  868. // connect starts creating a transport.
  869. // It does nothing if the ac is not IDLE.
  870. // TODO(bar) Move this to the addrConn section.
  871. func (ac *addrConn) connect() error {
  872. ac.mu.Lock()
  873. if ac.state == connectivity.Shutdown {
  874. if logger.V(2) {
  875. logger.Infof("connect called on shutdown addrConn; ignoring.")
  876. }
  877. ac.mu.Unlock()
  878. return errConnClosing
  879. }
  880. if ac.state != connectivity.Idle {
  881. if logger.V(2) {
  882. logger.Infof("connect called on addrConn in non-idle state (%v); ignoring.", ac.state)
  883. }
  884. ac.mu.Unlock()
  885. return nil
  886. }
  887. ac.mu.Unlock()
  888. ac.resetTransport()
  889. return nil
  890. }
  891. func equalAddresses(a, b []resolver.Address) bool {
  892. if len(a) != len(b) {
  893. return false
  894. }
  895. for i, v := range a {
  896. if !v.Equal(b[i]) {
  897. return false
  898. }
  899. }
  900. return true
  901. }
  902. // updateAddrs updates ac.addrs with the new addresses list and handles active
  903. // connections or connection attempts.
  904. func (ac *addrConn) updateAddrs(addrs []resolver.Address) {
  905. ac.mu.Lock()
  906. channelz.Infof(logger, ac.channelzID, "addrConn: updateAddrs curAddr: %v, addrs: %v", ac.curAddr, addrs)
  907. if equalAddresses(ac.addrs, addrs) {
  908. ac.mu.Unlock()
  909. return
  910. }
  911. ac.addrs = addrs
  912. if ac.state == connectivity.Shutdown ||
  913. ac.state == connectivity.TransientFailure ||
  914. ac.state == connectivity.Idle {
  915. // We were not connecting, so do nothing but update the addresses.
  916. ac.mu.Unlock()
  917. return
  918. }
  919. if ac.state == connectivity.Ready {
  920. // Try to find the connected address.
  921. for _, a := range addrs {
  922. a.ServerName = ac.cc.getServerName(a)
  923. if a.Equal(ac.curAddr) {
  924. // We are connected to a valid address, so do nothing but
  925. // update the addresses.
  926. ac.mu.Unlock()
  927. return
  928. }
  929. }
  930. }
  931. // We are either connected to the wrong address or currently connecting.
  932. // Stop the current iteration and restart.
  933. ac.cancel()
  934. ac.ctx, ac.cancel = context.WithCancel(ac.cc.ctx)
  935. // We have to defer here because GracefulClose => Close => onClose, which
  936. // requires locking ac.mu.
  937. if ac.transport != nil {
  938. defer ac.transport.GracefulClose()
  939. ac.transport = nil
  940. }
  941. if len(addrs) == 0 {
  942. ac.updateConnectivityState(connectivity.Idle, nil)
  943. }
  944. ac.mu.Unlock()
  945. // Since we were connecting/connected, we should start a new connection
  946. // attempt.
  947. go ac.resetTransport()
  948. }
  949. // getServerName determines the serverName to be used in the connection
  950. // handshake. The default value for the serverName is the authority on the
  951. // ClientConn, which either comes from the user's dial target or through an
  952. // authority override specified using the WithAuthority dial option. Name
  953. // resolvers can specify a per-address override for the serverName through the
  954. // resolver.Address.ServerName field which is used only if the WithAuthority
  955. // dial option was not used. The rationale is that per-address authority
  956. // overrides specified by the name resolver can represent a security risk, while
  957. // an override specified by the user is more dependable since they probably know
  958. // what they are doing.
  959. func (cc *ClientConn) getServerName(addr resolver.Address) string {
  960. if cc.dopts.authority != "" {
  961. return cc.dopts.authority
  962. }
  963. if addr.ServerName != "" {
  964. return addr.ServerName
  965. }
  966. return cc.authority
  967. }
  968. func getMethodConfig(sc *ServiceConfig, method string) MethodConfig {
  969. if sc == nil {
  970. return MethodConfig{}
  971. }
  972. if m, ok := sc.Methods[method]; ok {
  973. return m
  974. }
  975. i := strings.LastIndex(method, "/")
  976. if m, ok := sc.Methods[method[:i+1]]; ok {
  977. return m
  978. }
  979. return sc.Methods[""]
  980. }
  981. // GetMethodConfig gets the method config of the input method.
  982. // If there's an exact match for input method (i.e. /service/method), we return
  983. // the corresponding MethodConfig.
  984. // If there isn't an exact match for the input method, we look for the service's default
  985. // config under the service (i.e /service/) and then for the default for all services (empty string).
  986. //
  987. // If there is a default MethodConfig for the service, we return it.
  988. // Otherwise, we return an empty MethodConfig.
  989. func (cc *ClientConn) GetMethodConfig(method string) MethodConfig {
  990. // TODO: Avoid the locking here.
  991. cc.mu.RLock()
  992. defer cc.mu.RUnlock()
  993. return getMethodConfig(cc.sc, method)
  994. }
  995. func (cc *ClientConn) healthCheckConfig() *healthCheckConfig {
  996. cc.mu.RLock()
  997. defer cc.mu.RUnlock()
  998. if cc.sc == nil {
  999. return nil
  1000. }
  1001. return cc.sc.healthCheckConfig
  1002. }
  1003. func (cc *ClientConn) getTransport(ctx context.Context, failfast bool, method string) (transport.ClientTransport, balancer.PickResult, error) {
  1004. return cc.blockingpicker.pick(ctx, failfast, balancer.PickInfo{
  1005. Ctx: ctx,
  1006. FullMethodName: method,
  1007. })
  1008. }
  1009. func (cc *ClientConn) applyServiceConfigAndBalancer(sc *ServiceConfig, configSelector iresolver.ConfigSelector, addrs []resolver.Address) {
  1010. if sc == nil {
  1011. // should never reach here.
  1012. return
  1013. }
  1014. cc.sc = sc
  1015. if configSelector != nil {
  1016. cc.safeConfigSelector.UpdateConfigSelector(configSelector)
  1017. }
  1018. if cc.sc.retryThrottling != nil {
  1019. newThrottler := &retryThrottler{
  1020. tokens: cc.sc.retryThrottling.MaxTokens,
  1021. max: cc.sc.retryThrottling.MaxTokens,
  1022. thresh: cc.sc.retryThrottling.MaxTokens / 2,
  1023. ratio: cc.sc.retryThrottling.TokenRatio,
  1024. }
  1025. cc.retryThrottler.Store(newThrottler)
  1026. } else {
  1027. cc.retryThrottler.Store((*retryThrottler)(nil))
  1028. }
  1029. var newBalancerName string
  1030. if cc.sc != nil && cc.sc.lbConfig != nil {
  1031. newBalancerName = cc.sc.lbConfig.name
  1032. } else {
  1033. var isGRPCLB bool
  1034. for _, a := range addrs {
  1035. if a.Type == resolver.GRPCLB {
  1036. isGRPCLB = true
  1037. break
  1038. }
  1039. }
  1040. if isGRPCLB {
  1041. newBalancerName = grpclbName
  1042. } else if cc.sc != nil && cc.sc.LB != nil {
  1043. newBalancerName = *cc.sc.LB
  1044. } else {
  1045. newBalancerName = PickFirstBalancerName
  1046. }
  1047. }
  1048. cc.balancerWrapper.switchTo(newBalancerName)
  1049. }
  1050. func (cc *ClientConn) resolveNow(o resolver.ResolveNowOptions) {
  1051. cc.mu.RLock()
  1052. r := cc.resolverWrapper
  1053. cc.mu.RUnlock()
  1054. if r == nil {
  1055. return
  1056. }
  1057. go r.resolveNow(o)
  1058. }
  1059. // ResetConnectBackoff wakes up all subchannels in transient failure and causes
  1060. // them to attempt another connection immediately. It also resets the backoff
  1061. // times used for subsequent attempts regardless of the current state.
  1062. //
  1063. // In general, this function should not be used. Typical service or network
  1064. // outages result in a reasonable client reconnection strategy by default.
  1065. // However, if a previously unavailable network becomes available, this may be
  1066. // used to trigger an immediate reconnect.
  1067. //
  1068. // # Experimental
  1069. //
  1070. // Notice: This API is EXPERIMENTAL and may be changed or removed in a
  1071. // later release.
  1072. func (cc *ClientConn) ResetConnectBackoff() {
  1073. cc.mu.Lock()
  1074. conns := cc.conns
  1075. cc.mu.Unlock()
  1076. for ac := range conns {
  1077. ac.resetConnectBackoff()
  1078. }
  1079. }
  1080. // Close tears down the ClientConn and all underlying connections.
  1081. func (cc *ClientConn) Close() error {
  1082. defer cc.cancel()
  1083. cc.mu.Lock()
  1084. if cc.conns == nil {
  1085. cc.mu.Unlock()
  1086. return ErrClientConnClosing
  1087. }
  1088. for cc.idlenessState == ccIdlenessStateExitingIdle {
  1089. cc.exitIdleCond.Wait()
  1090. }
  1091. conns := cc.conns
  1092. cc.conns = nil
  1093. cc.csMgr.updateState(connectivity.Shutdown)
  1094. pWrapper := cc.blockingpicker
  1095. rWrapper := cc.resolverWrapper
  1096. bWrapper := cc.balancerWrapper
  1097. idlenessMgr := cc.idlenessMgr
  1098. cc.mu.Unlock()
  1099. // The order of closing matters here since the balancer wrapper assumes the
  1100. // picker is closed before it is closed.
  1101. if pWrapper != nil {
  1102. pWrapper.close()
  1103. }
  1104. if bWrapper != nil {
  1105. bWrapper.close()
  1106. }
  1107. if rWrapper != nil {
  1108. rWrapper.close()
  1109. }
  1110. if idlenessMgr != nil {
  1111. idlenessMgr.close()
  1112. }
  1113. for ac := range conns {
  1114. ac.tearDown(ErrClientConnClosing)
  1115. }
  1116. cc.addTraceEvent("deleted")
  1117. // TraceEvent needs to be called before RemoveEntry, as TraceEvent may add
  1118. // trace reference to the entity being deleted, and thus prevent it from being
  1119. // deleted right away.
  1120. channelz.RemoveEntry(cc.channelzID)
  1121. return nil
  1122. }
  1123. // addrConn is a network connection to a given address.
  1124. type addrConn struct {
  1125. ctx context.Context
  1126. cancel context.CancelFunc
  1127. cc *ClientConn
  1128. dopts dialOptions
  1129. acbw balancer.SubConn
  1130. scopts balancer.NewSubConnOptions
  1131. // transport is set when there's a viable transport (note: ac state may not be READY as LB channel
  1132. // health checking may require server to report healthy to set ac to READY), and is reset
  1133. // to nil when the current transport should no longer be used to create a stream (e.g. after GoAway
  1134. // is received, transport is closed, ac has been torn down).
  1135. transport transport.ClientTransport // The current transport.
  1136. mu sync.Mutex
  1137. curAddr resolver.Address // The current address.
  1138. addrs []resolver.Address // All addresses that the resolver resolved to.
  1139. // Use updateConnectivityState for updating addrConn's connectivity state.
  1140. state connectivity.State
  1141. stateChan chan struct{} // closed and recreated on every state change.
  1142. backoffIdx int // Needs to be stateful for resetConnectBackoff.
  1143. resetBackoff chan struct{}
  1144. channelzID *channelz.Identifier
  1145. czData *channelzData
  1146. }
  1147. // Note: this requires a lock on ac.mu.
  1148. func (ac *addrConn) updateConnectivityState(s connectivity.State, lastErr error) {
  1149. if ac.state == s {
  1150. return
  1151. }
  1152. // When changing states, reset the state change channel.
  1153. close(ac.stateChan)
  1154. ac.stateChan = make(chan struct{})
  1155. ac.state = s
  1156. if lastErr == nil {
  1157. channelz.Infof(logger, ac.channelzID, "Subchannel Connectivity change to %v", s)
  1158. } else {
  1159. channelz.Infof(logger, ac.channelzID, "Subchannel Connectivity change to %v, last error: %s", s, lastErr)
  1160. }
  1161. ac.cc.handleSubConnStateChange(ac.acbw, s, lastErr)
  1162. }
  1163. // adjustParams updates parameters used to create transports upon
  1164. // receiving a GoAway.
  1165. func (ac *addrConn) adjustParams(r transport.GoAwayReason) {
  1166. switch r {
  1167. case transport.GoAwayTooManyPings:
  1168. v := 2 * ac.dopts.copts.KeepaliveParams.Time
  1169. ac.cc.mu.Lock()
  1170. if v > ac.cc.mkp.Time {
  1171. ac.cc.mkp.Time = v
  1172. }
  1173. ac.cc.mu.Unlock()
  1174. }
  1175. }
  1176. func (ac *addrConn) resetTransport() {
  1177. ac.mu.Lock()
  1178. acCtx := ac.ctx
  1179. if acCtx.Err() != nil {
  1180. ac.mu.Unlock()
  1181. return
  1182. }
  1183. addrs := ac.addrs
  1184. backoffFor := ac.dopts.bs.Backoff(ac.backoffIdx)
  1185. // This will be the duration that dial gets to finish.
  1186. dialDuration := minConnectTimeout
  1187. if ac.dopts.minConnectTimeout != nil {
  1188. dialDuration = ac.dopts.minConnectTimeout()
  1189. }
  1190. if dialDuration < backoffFor {
  1191. // Give dial more time as we keep failing to connect.
  1192. dialDuration = backoffFor
  1193. }
  1194. // We can potentially spend all the time trying the first address, and
  1195. // if the server accepts the connection and then hangs, the following
  1196. // addresses will never be tried.
  1197. //
  1198. // The spec doesn't mention what should be done for multiple addresses.
  1199. // https://github.com/grpc/grpc/blob/master/doc/connection-backoff.md#proposed-backoff-algorithm
  1200. connectDeadline := time.Now().Add(dialDuration)
  1201. ac.updateConnectivityState(connectivity.Connecting, nil)
  1202. ac.mu.Unlock()
  1203. if err := ac.tryAllAddrs(acCtx, addrs, connectDeadline); err != nil {
  1204. ac.cc.resolveNow(resolver.ResolveNowOptions{})
  1205. // After exhausting all addresses, the addrConn enters
  1206. // TRANSIENT_FAILURE.
  1207. if acCtx.Err() != nil {
  1208. return
  1209. }
  1210. ac.mu.Lock()
  1211. ac.updateConnectivityState(connectivity.TransientFailure, err)
  1212. // Backoff.
  1213. b := ac.resetBackoff
  1214. ac.mu.Unlock()
  1215. timer := time.NewTimer(backoffFor)
  1216. select {
  1217. case <-timer.C:
  1218. ac.mu.Lock()
  1219. ac.backoffIdx++
  1220. ac.mu.Unlock()
  1221. case <-b:
  1222. timer.Stop()
  1223. case <-acCtx.Done():
  1224. timer.Stop()
  1225. return
  1226. }
  1227. ac.mu.Lock()
  1228. if acCtx.Err() == nil {
  1229. ac.updateConnectivityState(connectivity.Idle, err)
  1230. }
  1231. ac.mu.Unlock()
  1232. return
  1233. }
  1234. // Success; reset backoff.
  1235. ac.mu.Lock()
  1236. ac.backoffIdx = 0
  1237. ac.mu.Unlock()
  1238. }
  1239. // tryAllAddrs tries to creates a connection to the addresses, and stop when at
  1240. // the first successful one. It returns an error if no address was successfully
  1241. // connected, or updates ac appropriately with the new transport.
  1242. func (ac *addrConn) tryAllAddrs(ctx context.Context, addrs []resolver.Address, connectDeadline time.Time) error {
  1243. var firstConnErr error
  1244. for _, addr := range addrs {
  1245. if ctx.Err() != nil {
  1246. return errConnClosing
  1247. }
  1248. ac.mu.Lock()
  1249. ac.cc.mu.RLock()
  1250. ac.dopts.copts.KeepaliveParams = ac.cc.mkp
  1251. ac.cc.mu.RUnlock()
  1252. copts := ac.dopts.copts
  1253. if ac.scopts.CredsBundle != nil {
  1254. copts.CredsBundle = ac.scopts.CredsBundle
  1255. }
  1256. ac.mu.Unlock()
  1257. channelz.Infof(logger, ac.channelzID, "Subchannel picks a new address %q to connect", addr.Addr)
  1258. err := ac.createTransport(ctx, addr, copts, connectDeadline)
  1259. if err == nil {
  1260. return nil
  1261. }
  1262. if firstConnErr == nil {
  1263. firstConnErr = err
  1264. }
  1265. ac.cc.updateConnectionError(err)
  1266. }
  1267. // Couldn't connect to any address.
  1268. return firstConnErr
  1269. }
  1270. // createTransport creates a connection to addr. It returns an error if the
  1271. // address was not successfully connected, or updates ac appropriately with the
  1272. // new transport.
  1273. func (ac *addrConn) createTransport(ctx context.Context, addr resolver.Address, copts transport.ConnectOptions, connectDeadline time.Time) error {
  1274. addr.ServerName = ac.cc.getServerName(addr)
  1275. hctx, hcancel := context.WithCancel(ctx)
  1276. onClose := func(r transport.GoAwayReason) {
  1277. ac.mu.Lock()
  1278. defer ac.mu.Unlock()
  1279. // adjust params based on GoAwayReason
  1280. ac.adjustParams(r)
  1281. if ctx.Err() != nil {
  1282. // Already shut down or connection attempt canceled. tearDown() or
  1283. // updateAddrs() already cleared the transport and canceled hctx
  1284. // via ac.ctx, and we expected this connection to be closed, so do
  1285. // nothing here.
  1286. return
  1287. }
  1288. hcancel()
  1289. if ac.transport == nil {
  1290. // We're still connecting to this address, which could error. Do
  1291. // not update the connectivity state or resolve; these will happen
  1292. // at the end of the tryAllAddrs connection loop in the event of an
  1293. // error.
  1294. return
  1295. }
  1296. ac.transport = nil
  1297. // Refresh the name resolver on any connection loss.
  1298. ac.cc.resolveNow(resolver.ResolveNowOptions{})
  1299. // Always go idle and wait for the LB policy to initiate a new
  1300. // connection attempt.
  1301. ac.updateConnectivityState(connectivity.Idle, nil)
  1302. }
  1303. connectCtx, cancel := context.WithDeadline(ctx, connectDeadline)
  1304. defer cancel()
  1305. copts.ChannelzParentID = ac.channelzID
  1306. newTr, err := transport.NewClientTransport(connectCtx, ac.cc.ctx, addr, copts, onClose)
  1307. if err != nil {
  1308. if logger.V(2) {
  1309. logger.Infof("Creating new client transport to %q: %v", addr, err)
  1310. }
  1311. // newTr is either nil, or closed.
  1312. hcancel()
  1313. channelz.Warningf(logger, ac.channelzID, "grpc: addrConn.createTransport failed to connect to %s. Err: %v", addr, err)
  1314. return err
  1315. }
  1316. ac.mu.Lock()
  1317. defer ac.mu.Unlock()
  1318. if ctx.Err() != nil {
  1319. // This can happen if the subConn was removed while in `Connecting`
  1320. // state. tearDown() would have set the state to `Shutdown`, but
  1321. // would not have closed the transport since ac.transport would not
  1322. // have been set at that point.
  1323. //
  1324. // We run this in a goroutine because newTr.Close() calls onClose()
  1325. // inline, which requires locking ac.mu.
  1326. //
  1327. // The error we pass to Close() is immaterial since there are no open
  1328. // streams at this point, so no trailers with error details will be sent
  1329. // out. We just need to pass a non-nil error.
  1330. //
  1331. // This can also happen when updateAddrs is called during a connection
  1332. // attempt.
  1333. go newTr.Close(transport.ErrConnClosing)
  1334. return nil
  1335. }
  1336. if hctx.Err() != nil {
  1337. // onClose was already called for this connection, but the connection
  1338. // was successfully established first. Consider it a success and set
  1339. // the new state to Idle.
  1340. ac.updateConnectivityState(connectivity.Idle, nil)
  1341. return nil
  1342. }
  1343. ac.curAddr = addr
  1344. ac.transport = newTr
  1345. ac.startHealthCheck(hctx) // Will set state to READY if appropriate.
  1346. return nil
  1347. }
  1348. // startHealthCheck starts the health checking stream (RPC) to watch the health
  1349. // stats of this connection if health checking is requested and configured.
  1350. //
  1351. // LB channel health checking is enabled when all requirements below are met:
  1352. // 1. it is not disabled by the user with the WithDisableHealthCheck DialOption
  1353. // 2. internal.HealthCheckFunc is set by importing the grpc/health package
  1354. // 3. a service config with non-empty healthCheckConfig field is provided
  1355. // 4. the load balancer requests it
  1356. //
  1357. // It sets addrConn to READY if the health checking stream is not started.
  1358. //
  1359. // Caller must hold ac.mu.
  1360. func (ac *addrConn) startHealthCheck(ctx context.Context) {
  1361. var healthcheckManagingState bool
  1362. defer func() {
  1363. if !healthcheckManagingState {
  1364. ac.updateConnectivityState(connectivity.Ready, nil)
  1365. }
  1366. }()
  1367. if ac.cc.dopts.disableHealthCheck {
  1368. return
  1369. }
  1370. healthCheckConfig := ac.cc.healthCheckConfig()
  1371. if healthCheckConfig == nil {
  1372. return
  1373. }
  1374. if !ac.scopts.HealthCheckEnabled {
  1375. return
  1376. }
  1377. healthCheckFunc := ac.cc.dopts.healthCheckFunc
  1378. if healthCheckFunc == nil {
  1379. // The health package is not imported to set health check function.
  1380. //
  1381. // TODO: add a link to the health check doc in the error message.
  1382. channelz.Error(logger, ac.channelzID, "Health check is requested but health check function is not set.")
  1383. return
  1384. }
  1385. healthcheckManagingState = true
  1386. // Set up the health check helper functions.
  1387. currentTr := ac.transport
  1388. newStream := func(method string) (interface{}, error) {
  1389. ac.mu.Lock()
  1390. if ac.transport != currentTr {
  1391. ac.mu.Unlock()
  1392. return nil, status.Error(codes.Canceled, "the provided transport is no longer valid to use")
  1393. }
  1394. ac.mu.Unlock()
  1395. return newNonRetryClientStream(ctx, &StreamDesc{ServerStreams: true}, method, currentTr, ac)
  1396. }
  1397. setConnectivityState := func(s connectivity.State, lastErr error) {
  1398. ac.mu.Lock()
  1399. defer ac.mu.Unlock()
  1400. if ac.transport != currentTr {
  1401. return
  1402. }
  1403. ac.updateConnectivityState(s, lastErr)
  1404. }
  1405. // Start the health checking stream.
  1406. go func() {
  1407. err := ac.cc.dopts.healthCheckFunc(ctx, newStream, setConnectivityState, healthCheckConfig.ServiceName)
  1408. if err != nil {
  1409. if status.Code(err) == codes.Unimplemented {
  1410. channelz.Error(logger, ac.channelzID, "Subchannel health check is unimplemented at server side, thus health check is disabled")
  1411. } else {
  1412. channelz.Errorf(logger, ac.channelzID, "Health checking failed: %v", err)
  1413. }
  1414. }
  1415. }()
  1416. }
  1417. func (ac *addrConn) resetConnectBackoff() {
  1418. ac.mu.Lock()
  1419. close(ac.resetBackoff)
  1420. ac.backoffIdx = 0
  1421. ac.resetBackoff = make(chan struct{})
  1422. ac.mu.Unlock()
  1423. }
  1424. // getReadyTransport returns the transport if ac's state is READY or nil if not.
  1425. func (ac *addrConn) getReadyTransport() transport.ClientTransport {
  1426. ac.mu.Lock()
  1427. defer ac.mu.Unlock()
  1428. if ac.state == connectivity.Ready {
  1429. return ac.transport
  1430. }
  1431. return nil
  1432. }
  1433. // getTransport waits until the addrconn is ready and returns the transport.
  1434. // If the context expires first, returns an appropriate status. If the
  1435. // addrConn is stopped first, returns an Unavailable status error.
  1436. func (ac *addrConn) getTransport(ctx context.Context) (transport.ClientTransport, error) {
  1437. for ctx.Err() == nil {
  1438. ac.mu.Lock()
  1439. t, state, sc := ac.transport, ac.state, ac.stateChan
  1440. ac.mu.Unlock()
  1441. if state == connectivity.Ready {
  1442. return t, nil
  1443. }
  1444. if state == connectivity.Shutdown {
  1445. return nil, status.Errorf(codes.Unavailable, "SubConn shutting down")
  1446. }
  1447. select {
  1448. case <-ctx.Done():
  1449. case <-sc:
  1450. }
  1451. }
  1452. return nil, status.FromContextError(ctx.Err()).Err()
  1453. }
  1454. // tearDown starts to tear down the addrConn.
  1455. //
  1456. // Note that tearDown doesn't remove ac from ac.cc.conns, so the addrConn struct
  1457. // will leak. In most cases, call cc.removeAddrConn() instead.
  1458. func (ac *addrConn) tearDown(err error) {
  1459. ac.mu.Lock()
  1460. if ac.state == connectivity.Shutdown {
  1461. ac.mu.Unlock()
  1462. return
  1463. }
  1464. curTr := ac.transport
  1465. ac.transport = nil
  1466. // We have to set the state to Shutdown before anything else to prevent races
  1467. // between setting the state and logic that waits on context cancellation / etc.
  1468. ac.updateConnectivityState(connectivity.Shutdown, nil)
  1469. ac.cancel()
  1470. ac.curAddr = resolver.Address{}
  1471. if err == errConnDrain && curTr != nil {
  1472. // GracefulClose(...) may be executed multiple times when
  1473. // i) receiving multiple GoAway frames from the server; or
  1474. // ii) there are concurrent name resolver/Balancer triggered
  1475. // address removal and GoAway.
  1476. // We have to unlock and re-lock here because GracefulClose => Close => onClose, which requires locking ac.mu.
  1477. ac.mu.Unlock()
  1478. curTr.GracefulClose()
  1479. ac.mu.Lock()
  1480. }
  1481. channelz.AddTraceEvent(logger, ac.channelzID, 0, &channelz.TraceEventDesc{
  1482. Desc: "Subchannel deleted",
  1483. Severity: channelz.CtInfo,
  1484. Parent: &channelz.TraceEventDesc{
  1485. Desc: fmt.Sprintf("Subchannel(id:%d) deleted", ac.channelzID.Int()),
  1486. Severity: channelz.CtInfo,
  1487. },
  1488. })
  1489. // TraceEvent needs to be called before RemoveEntry, as TraceEvent may add
  1490. // trace reference to the entity being deleted, and thus prevent it from
  1491. // being deleted right away.
  1492. channelz.RemoveEntry(ac.channelzID)
  1493. ac.mu.Unlock()
  1494. }
  1495. func (ac *addrConn) getState() connectivity.State {
  1496. ac.mu.Lock()
  1497. defer ac.mu.Unlock()
  1498. return ac.state
  1499. }
  1500. func (ac *addrConn) ChannelzMetric() *channelz.ChannelInternalMetric {
  1501. ac.mu.Lock()
  1502. addr := ac.curAddr.Addr
  1503. ac.mu.Unlock()
  1504. return &channelz.ChannelInternalMetric{
  1505. State: ac.getState(),
  1506. Target: addr,
  1507. CallsStarted: atomic.LoadInt64(&ac.czData.callsStarted),
  1508. CallsSucceeded: atomic.LoadInt64(&ac.czData.callsSucceeded),
  1509. CallsFailed: atomic.LoadInt64(&ac.czData.callsFailed),
  1510. LastCallStartedTimestamp: time.Unix(0, atomic.LoadInt64(&ac.czData.lastCallStartedTime)),
  1511. }
  1512. }
  1513. func (ac *addrConn) incrCallsStarted() {
  1514. atomic.AddInt64(&ac.czData.callsStarted, 1)
  1515. atomic.StoreInt64(&ac.czData.lastCallStartedTime, time.Now().UnixNano())
  1516. }
  1517. func (ac *addrConn) incrCallsSucceeded() {
  1518. atomic.AddInt64(&ac.czData.callsSucceeded, 1)
  1519. }
  1520. func (ac *addrConn) incrCallsFailed() {
  1521. atomic.AddInt64(&ac.czData.callsFailed, 1)
  1522. }
  1523. type retryThrottler struct {
  1524. max float64
  1525. thresh float64
  1526. ratio float64
  1527. mu sync.Mutex
  1528. tokens float64 // TODO(dfawley): replace with atomic and remove lock.
  1529. }
  1530. // throttle subtracts a retry token from the pool and returns whether a retry
  1531. // should be throttled (disallowed) based upon the retry throttling policy in
  1532. // the service config.
  1533. func (rt *retryThrottler) throttle() bool {
  1534. if rt == nil {
  1535. return false
  1536. }
  1537. rt.mu.Lock()
  1538. defer rt.mu.Unlock()
  1539. rt.tokens--
  1540. if rt.tokens < 0 {
  1541. rt.tokens = 0
  1542. }
  1543. return rt.tokens <= rt.thresh
  1544. }
  1545. func (rt *retryThrottler) successfulRPC() {
  1546. if rt == nil {
  1547. return
  1548. }
  1549. rt.mu.Lock()
  1550. defer rt.mu.Unlock()
  1551. rt.tokens += rt.ratio
  1552. if rt.tokens > rt.max {
  1553. rt.tokens = rt.max
  1554. }
  1555. }
  1556. type channelzChannel struct {
  1557. cc *ClientConn
  1558. }
  1559. func (c *channelzChannel) ChannelzMetric() *channelz.ChannelInternalMetric {
  1560. return c.cc.channelzMetric()
  1561. }
  1562. // ErrClientConnTimeout indicates that the ClientConn cannot establish the
  1563. // underlying connections within the specified timeout.
  1564. //
  1565. // Deprecated: This error is never returned by grpc and should not be
  1566. // referenced by users.
  1567. var ErrClientConnTimeout = errors.New("grpc: timed out when dialing")
  1568. // getResolver finds the scheme in the cc's resolvers or the global registry.
  1569. // scheme should always be lowercase (typically by virtue of url.Parse()
  1570. // performing proper RFC3986 behavior).
  1571. func (cc *ClientConn) getResolver(scheme string) resolver.Builder {
  1572. for _, rb := range cc.dopts.resolvers {
  1573. if scheme == rb.Scheme() {
  1574. return rb
  1575. }
  1576. }
  1577. return resolver.Get(scheme)
  1578. }
  1579. func (cc *ClientConn) updateConnectionError(err error) {
  1580. cc.lceMu.Lock()
  1581. cc.lastConnectionError = err
  1582. cc.lceMu.Unlock()
  1583. }
  1584. func (cc *ClientConn) connectionError() error {
  1585. cc.lceMu.Lock()
  1586. defer cc.lceMu.Unlock()
  1587. return cc.lastConnectionError
  1588. }
  1589. // parseTargetAndFindResolver parses the user's dial target and stores the
  1590. // parsed target in `cc.parsedTarget`.
  1591. //
  1592. // The resolver to use is determined based on the scheme in the parsed target
  1593. // and the same is stored in `cc.resolverBuilder`.
  1594. //
  1595. // Doesn't grab cc.mu as this method is expected to be called only at Dial time.
  1596. func (cc *ClientConn) parseTargetAndFindResolver() error {
  1597. channelz.Infof(logger, cc.channelzID, "original dial target is: %q", cc.target)
  1598. var rb resolver.Builder
  1599. parsedTarget, err := parseTarget(cc.target)
  1600. if err != nil {
  1601. channelz.Infof(logger, cc.channelzID, "dial target %q parse failed: %v", cc.target, err)
  1602. } else {
  1603. channelz.Infof(logger, cc.channelzID, "parsed dial target is: %+v", parsedTarget)
  1604. rb = cc.getResolver(parsedTarget.URL.Scheme)
  1605. if rb != nil {
  1606. cc.parsedTarget = parsedTarget
  1607. cc.resolverBuilder = rb
  1608. return nil
  1609. }
  1610. }
  1611. // We are here because the user's dial target did not contain a scheme or
  1612. // specified an unregistered scheme. We should fallback to the default
  1613. // scheme, except when a custom dialer is specified in which case, we should
  1614. // always use passthrough scheme.
  1615. defScheme := resolver.GetDefaultScheme()
  1616. channelz.Infof(logger, cc.channelzID, "fallback to scheme %q", defScheme)
  1617. canonicalTarget := defScheme + ":///" + cc.target
  1618. parsedTarget, err = parseTarget(canonicalTarget)
  1619. if err != nil {
  1620. channelz.Infof(logger, cc.channelzID, "dial target %q parse failed: %v", canonicalTarget, err)
  1621. return err
  1622. }
  1623. channelz.Infof(logger, cc.channelzID, "parsed dial target is: %+v", parsedTarget)
  1624. rb = cc.getResolver(parsedTarget.URL.Scheme)
  1625. if rb == nil {
  1626. return fmt.Errorf("could not get resolver for default scheme: %q", parsedTarget.URL.Scheme)
  1627. }
  1628. cc.parsedTarget = parsedTarget
  1629. cc.resolverBuilder = rb
  1630. return nil
  1631. }
  1632. // parseTarget uses RFC 3986 semantics to parse the given target into a
  1633. // resolver.Target struct containing scheme, authority and url. Query
  1634. // params are stripped from the endpoint.
  1635. func parseTarget(target string) (resolver.Target, error) {
  1636. u, err := url.Parse(target)
  1637. if err != nil {
  1638. return resolver.Target{}, err
  1639. }
  1640. return resolver.Target{
  1641. Scheme: u.Scheme,
  1642. Authority: u.Host,
  1643. URL: *u,
  1644. }, nil
  1645. }
  1646. // Determine channel authority. The order of precedence is as follows:
  1647. // - user specified authority override using `WithAuthority` dial option
  1648. // - creds' notion of server name for the authentication handshake
  1649. // - endpoint from dial target of the form "scheme://[authority]/endpoint"
  1650. //
  1651. // Stores the determined authority in `cc.authority`.
  1652. //
  1653. // Returns a non-nil error if the authority returned by the transport
  1654. // credentials do not match the authority configured through the dial option.
  1655. //
  1656. // Doesn't grab cc.mu as this method is expected to be called only at Dial time.
  1657. func (cc *ClientConn) determineAuthority() error {
  1658. dopts := cc.dopts
  1659. // Historically, we had two options for users to specify the serverName or
  1660. // authority for a channel. One was through the transport credentials
  1661. // (either in its constructor, or through the OverrideServerName() method).
  1662. // The other option (for cases where WithInsecure() dial option was used)
  1663. // was to use the WithAuthority() dial option.
  1664. //
  1665. // A few things have changed since:
  1666. // - `insecure` package with an implementation of the `TransportCredentials`
  1667. // interface for the insecure case
  1668. // - WithAuthority() dial option support for secure credentials
  1669. authorityFromCreds := ""
  1670. if creds := dopts.copts.TransportCredentials; creds != nil && creds.Info().ServerName != "" {
  1671. authorityFromCreds = creds.Info().ServerName
  1672. }
  1673. authorityFromDialOption := dopts.authority
  1674. if (authorityFromCreds != "" && authorityFromDialOption != "") && authorityFromCreds != authorityFromDialOption {
  1675. return fmt.Errorf("ClientConn's authority from transport creds %q and dial option %q don't match", authorityFromCreds, authorityFromDialOption)
  1676. }
  1677. endpoint := cc.parsedTarget.Endpoint()
  1678. target := cc.target
  1679. switch {
  1680. case authorityFromDialOption != "":
  1681. cc.authority = authorityFromDialOption
  1682. case authorityFromCreds != "":
  1683. cc.authority = authorityFromCreds
  1684. case strings.HasPrefix(target, "unix:") || strings.HasPrefix(target, "unix-abstract:"):
  1685. // TODO: remove when the unix resolver implements optional interface to
  1686. // return channel authority.
  1687. cc.authority = "localhost"
  1688. case strings.HasPrefix(endpoint, ":"):
  1689. cc.authority = "localhost" + endpoint
  1690. default:
  1691. // TODO: Define an optional interface on the resolver builder to return
  1692. // the channel authority given the user's dial target. For resolvers
  1693. // which don't implement this interface, we will use the endpoint from
  1694. // "scheme://authority/endpoint" as the default authority.
  1695. cc.authority = endpoint
  1696. }
  1697. channelz.Infof(logger, cc.channelzID, "Channel authority set to %q", cc.authority)
  1698. return nil
  1699. }
  1700. // initResolverWrapper creates a ccResolverWrapper, which builds the name
  1701. // resolver. This method grabs the lock to assign the newly built resolver
  1702. // wrapper to the cc.resolverWrapper field.
  1703. func (cc *ClientConn) initResolverWrapper(creds credentials.TransportCredentials) error {
  1704. rw, err := newCCResolverWrapper(cc, ccResolverWrapperOpts{
  1705. target: cc.parsedTarget,
  1706. builder: cc.resolverBuilder,
  1707. bOpts: resolver.BuildOptions{
  1708. DisableServiceConfig: cc.dopts.disableServiceConfig,
  1709. DialCreds: creds,
  1710. CredsBundle: cc.dopts.copts.CredsBundle,
  1711. Dialer: cc.dopts.copts.Dialer,
  1712. },
  1713. channelzID: cc.channelzID,
  1714. })
  1715. if err != nil {
  1716. return fmt.Errorf("failed to build resolver: %v", err)
  1717. }
  1718. // Resolver implementations may report state update or error inline when
  1719. // built (or right after), and this is handled in cc.updateResolverState.
  1720. // Also, an error from the resolver might lead to a re-resolution request
  1721. // from the balancer, which is handled in resolveNow() where
  1722. // `cc.resolverWrapper` is accessed. Hence, we need to hold the lock here.
  1723. cc.mu.Lock()
  1724. cc.resolverWrapper = rw
  1725. cc.mu.Unlock()
  1726. return nil
  1727. }