dialoptions.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. /*
  2. *
  3. * Copyright 2018 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. "fmt"
  22. "net"
  23. "time"
  24. "google.golang.org/grpc/backoff"
  25. "google.golang.org/grpc/balancer"
  26. "google.golang.org/grpc/credentials"
  27. "google.golang.org/grpc/internal"
  28. internalbackoff "google.golang.org/grpc/internal/backoff"
  29. "google.golang.org/grpc/internal/envconfig"
  30. "google.golang.org/grpc/internal/transport"
  31. "google.golang.org/grpc/keepalive"
  32. "google.golang.org/grpc/resolver"
  33. "google.golang.org/grpc/stats"
  34. )
  35. // dialOptions configure a Dial call. dialOptions are set by the DialOption
  36. // values passed to Dial.
  37. type dialOptions struct {
  38. unaryInt UnaryClientInterceptor
  39. streamInt StreamClientInterceptor
  40. chainUnaryInts []UnaryClientInterceptor
  41. chainStreamInts []StreamClientInterceptor
  42. cp Compressor
  43. dc Decompressor
  44. bs internalbackoff.Strategy
  45. block bool
  46. returnLastError bool
  47. insecure bool
  48. timeout time.Duration
  49. scChan <-chan ServiceConfig
  50. authority string
  51. copts transport.ConnectOptions
  52. callOptions []CallOption
  53. // This is used by WithBalancerName dial option.
  54. balancerBuilder balancer.Builder
  55. channelzParentID int64
  56. disableServiceConfig bool
  57. disableRetry bool
  58. disableHealthCheck bool
  59. healthCheckFunc internal.HealthChecker
  60. minConnectTimeout func() time.Duration
  61. defaultServiceConfig *ServiceConfig // defaultServiceConfig is parsed from defaultServiceConfigRawJSON.
  62. defaultServiceConfigRawJSON *string
  63. // This is used by ccResolverWrapper to backoff between successive calls to
  64. // resolver.ResolveNow(). The user will have no need to configure this, but
  65. // we need to be able to configure this in tests.
  66. resolveNowBackoff func(int) time.Duration
  67. resolvers []resolver.Builder
  68. withProxy bool
  69. }
  70. // DialOption configures how we set up the connection.
  71. type DialOption interface {
  72. apply(*dialOptions)
  73. }
  74. // EmptyDialOption does not alter the dial configuration. It can be embedded in
  75. // another structure to build custom dial options.
  76. //
  77. // This API is EXPERIMENTAL.
  78. type EmptyDialOption struct{}
  79. func (EmptyDialOption) apply(*dialOptions) {}
  80. // funcDialOption wraps a function that modifies dialOptions into an
  81. // implementation of the DialOption interface.
  82. type funcDialOption struct {
  83. f func(*dialOptions)
  84. }
  85. func (fdo *funcDialOption) apply(do *dialOptions) {
  86. fdo.f(do)
  87. }
  88. func newFuncDialOption(f func(*dialOptions)) *funcDialOption {
  89. return &funcDialOption{
  90. f: f,
  91. }
  92. }
  93. // WithWriteBufferSize determines how much data can be batched before doing a
  94. // write on the wire. The corresponding memory allocation for this buffer will
  95. // be twice the size to keep syscalls low. The default value for this buffer is
  96. // 32KB.
  97. //
  98. // Zero will disable the write buffer such that each write will be on underlying
  99. // connection. Note: A Send call may not directly translate to a write.
  100. func WithWriteBufferSize(s int) DialOption {
  101. return newFuncDialOption(func(o *dialOptions) {
  102. o.copts.WriteBufferSize = s
  103. })
  104. }
  105. // WithReadBufferSize lets you set the size of read buffer, this determines how
  106. // much data can be read at most for each read syscall.
  107. //
  108. // The default value for this buffer is 32KB. Zero will disable read buffer for
  109. // a connection so data framer can access the underlying conn directly.
  110. func WithReadBufferSize(s int) DialOption {
  111. return newFuncDialOption(func(o *dialOptions) {
  112. o.copts.ReadBufferSize = s
  113. })
  114. }
  115. // WithInitialWindowSize returns a DialOption which sets the value for initial
  116. // window size on a stream. The lower bound for window size is 64K and any value
  117. // smaller than that will be ignored.
  118. func WithInitialWindowSize(s int32) DialOption {
  119. return newFuncDialOption(func(o *dialOptions) {
  120. o.copts.InitialWindowSize = s
  121. })
  122. }
  123. // WithInitialConnWindowSize returns a DialOption which sets the value for
  124. // initial window size on a connection. The lower bound for window size is 64K
  125. // and any value smaller than that will be ignored.
  126. func WithInitialConnWindowSize(s int32) DialOption {
  127. return newFuncDialOption(func(o *dialOptions) {
  128. o.copts.InitialConnWindowSize = s
  129. })
  130. }
  131. // WithMaxMsgSize returns a DialOption which sets the maximum message size the
  132. // client can receive.
  133. //
  134. // Deprecated: use WithDefaultCallOptions(MaxCallRecvMsgSize(s)) instead. Will
  135. // be supported throughout 1.x.
  136. func WithMaxMsgSize(s int) DialOption {
  137. return WithDefaultCallOptions(MaxCallRecvMsgSize(s))
  138. }
  139. // WithDefaultCallOptions returns a DialOption which sets the default
  140. // CallOptions for calls over the connection.
  141. func WithDefaultCallOptions(cos ...CallOption) DialOption {
  142. return newFuncDialOption(func(o *dialOptions) {
  143. o.callOptions = append(o.callOptions, cos...)
  144. })
  145. }
  146. // WithCodec returns a DialOption which sets a codec for message marshaling and
  147. // unmarshaling.
  148. //
  149. // Deprecated: use WithDefaultCallOptions(ForceCodec(_)) instead. Will be
  150. // supported throughout 1.x.
  151. func WithCodec(c Codec) DialOption {
  152. return WithDefaultCallOptions(CallCustomCodec(c))
  153. }
  154. // WithCompressor returns a DialOption which sets a Compressor to use for
  155. // message compression. It has lower priority than the compressor set by the
  156. // UseCompressor CallOption.
  157. //
  158. // Deprecated: use UseCompressor instead. Will be supported throughout 1.x.
  159. func WithCompressor(cp Compressor) DialOption {
  160. return newFuncDialOption(func(o *dialOptions) {
  161. o.cp = cp
  162. })
  163. }
  164. // WithDecompressor returns a DialOption which sets a Decompressor to use for
  165. // incoming message decompression. If incoming response messages are encoded
  166. // using the decompressor's Type(), it will be used. Otherwise, the message
  167. // encoding will be used to look up the compressor registered via
  168. // encoding.RegisterCompressor, which will then be used to decompress the
  169. // message. If no compressor is registered for the encoding, an Unimplemented
  170. // status error will be returned.
  171. //
  172. // Deprecated: use encoding.RegisterCompressor instead. Will be supported
  173. // throughout 1.x.
  174. func WithDecompressor(dc Decompressor) DialOption {
  175. return newFuncDialOption(func(o *dialOptions) {
  176. o.dc = dc
  177. })
  178. }
  179. // WithBalancerName sets the balancer that the ClientConn will be initialized
  180. // with. Balancer registered with balancerName will be used. This function
  181. // panics if no balancer was registered by balancerName.
  182. //
  183. // The balancer cannot be overridden by balancer option specified by service
  184. // config.
  185. //
  186. // Deprecated: use WithDefaultServiceConfig and WithDisableServiceConfig
  187. // instead. Will be removed in a future 1.x release.
  188. func WithBalancerName(balancerName string) DialOption {
  189. builder := balancer.Get(balancerName)
  190. if builder == nil {
  191. panic(fmt.Sprintf("grpc.WithBalancerName: no balancer is registered for name %v", balancerName))
  192. }
  193. return newFuncDialOption(func(o *dialOptions) {
  194. o.balancerBuilder = builder
  195. })
  196. }
  197. // WithServiceConfig returns a DialOption which has a channel to read the
  198. // service configuration.
  199. //
  200. // Deprecated: service config should be received through name resolver or via
  201. // WithDefaultServiceConfig, as specified at
  202. // https://github.com/grpc/grpc/blob/master/doc/service_config.md. Will be
  203. // removed in a future 1.x release.
  204. func WithServiceConfig(c <-chan ServiceConfig) DialOption {
  205. return newFuncDialOption(func(o *dialOptions) {
  206. o.scChan = c
  207. })
  208. }
  209. // WithConnectParams configures the dialer to use the provided ConnectParams.
  210. //
  211. // The backoff configuration specified as part of the ConnectParams overrides
  212. // all defaults specified in
  213. // https://github.com/grpc/grpc/blob/master/doc/connection-backoff.md. Consider
  214. // using the backoff.DefaultConfig as a base, in cases where you want to
  215. // override only a subset of the backoff configuration.
  216. //
  217. // This API is EXPERIMENTAL.
  218. func WithConnectParams(p ConnectParams) DialOption {
  219. return newFuncDialOption(func(o *dialOptions) {
  220. o.bs = internalbackoff.Exponential{Config: p.Backoff}
  221. o.minConnectTimeout = func() time.Duration {
  222. return p.MinConnectTimeout
  223. }
  224. })
  225. }
  226. // WithBackoffMaxDelay configures the dialer to use the provided maximum delay
  227. // when backing off after failed connection attempts.
  228. //
  229. // Deprecated: use WithConnectParams instead. Will be supported throughout 1.x.
  230. func WithBackoffMaxDelay(md time.Duration) DialOption {
  231. return WithBackoffConfig(BackoffConfig{MaxDelay: md})
  232. }
  233. // WithBackoffConfig configures the dialer to use the provided backoff
  234. // parameters after connection failures.
  235. //
  236. // Deprecated: use WithConnectParams instead. Will be supported throughout 1.x.
  237. func WithBackoffConfig(b BackoffConfig) DialOption {
  238. bc := backoff.DefaultConfig
  239. bc.MaxDelay = b.MaxDelay
  240. return withBackoff(internalbackoff.Exponential{Config: bc})
  241. }
  242. // withBackoff sets the backoff strategy used for connectRetryNum after a failed
  243. // connection attempt.
  244. //
  245. // This can be exported if arbitrary backoff strategies are allowed by gRPC.
  246. func withBackoff(bs internalbackoff.Strategy) DialOption {
  247. return newFuncDialOption(func(o *dialOptions) {
  248. o.bs = bs
  249. })
  250. }
  251. // WithBlock returns a DialOption which makes caller of Dial blocks until the
  252. // underlying connection is up. Without this, Dial returns immediately and
  253. // connecting the server happens in background.
  254. func WithBlock() DialOption {
  255. return newFuncDialOption(func(o *dialOptions) {
  256. o.block = true
  257. })
  258. }
  259. // WithReturnConnectionError returns a DialOption which makes the client connection
  260. // return a string containing both the last connection error that occurred and
  261. // the context.DeadlineExceeded error.
  262. // Implies WithBlock()
  263. //
  264. // This API is EXPERIMENTAL.
  265. func WithReturnConnectionError() DialOption {
  266. return newFuncDialOption(func(o *dialOptions) {
  267. o.block = true
  268. o.returnLastError = true
  269. })
  270. }
  271. // WithInsecure returns a DialOption which disables transport security for this
  272. // ClientConn. Note that transport security is required unless WithInsecure is
  273. // set.
  274. func WithInsecure() DialOption {
  275. return newFuncDialOption(func(o *dialOptions) {
  276. o.insecure = true
  277. })
  278. }
  279. // WithNoProxy returns a DialOption which disables the use of proxies for this
  280. // ClientConn. This is ignored if WithDialer or WithContextDialer are used.
  281. //
  282. // This API is EXPERIMENTAL.
  283. func WithNoProxy() DialOption {
  284. return newFuncDialOption(func(o *dialOptions) {
  285. o.withProxy = false
  286. })
  287. }
  288. // WithTransportCredentials returns a DialOption which configures a connection
  289. // level security credentials (e.g., TLS/SSL). This should not be used together
  290. // with WithCredentialsBundle.
  291. func WithTransportCredentials(creds credentials.TransportCredentials) DialOption {
  292. return newFuncDialOption(func(o *dialOptions) {
  293. o.copts.TransportCredentials = creds
  294. })
  295. }
  296. // WithPerRPCCredentials returns a DialOption which sets credentials and places
  297. // auth state on each outbound RPC.
  298. func WithPerRPCCredentials(creds credentials.PerRPCCredentials) DialOption {
  299. return newFuncDialOption(func(o *dialOptions) {
  300. o.copts.PerRPCCredentials = append(o.copts.PerRPCCredentials, creds)
  301. })
  302. }
  303. // WithCredentialsBundle returns a DialOption to set a credentials bundle for
  304. // the ClientConn.WithCreds. This should not be used together with
  305. // WithTransportCredentials.
  306. //
  307. // This API is experimental.
  308. func WithCredentialsBundle(b credentials.Bundle) DialOption {
  309. return newFuncDialOption(func(o *dialOptions) {
  310. o.copts.CredsBundle = b
  311. })
  312. }
  313. // WithTimeout returns a DialOption that configures a timeout for dialing a
  314. // ClientConn initially. This is valid if and only if WithBlock() is present.
  315. //
  316. // Deprecated: use DialContext instead of Dial and context.WithTimeout
  317. // instead. Will be supported throughout 1.x.
  318. func WithTimeout(d time.Duration) DialOption {
  319. return newFuncDialOption(func(o *dialOptions) {
  320. o.timeout = d
  321. })
  322. }
  323. // WithContextDialer returns a DialOption that sets a dialer to create
  324. // connections. If FailOnNonTempDialError() is set to true, and an error is
  325. // returned by f, gRPC checks the error's Temporary() method to decide if it
  326. // should try to reconnect to the network address.
  327. func WithContextDialer(f func(context.Context, string) (net.Conn, error)) DialOption {
  328. return newFuncDialOption(func(o *dialOptions) {
  329. o.copts.Dialer = f
  330. })
  331. }
  332. func init() {
  333. internal.WithHealthCheckFunc = withHealthCheckFunc
  334. }
  335. // WithDialer returns a DialOption that specifies a function to use for dialing
  336. // network addresses. If FailOnNonTempDialError() is set to true, and an error
  337. // is returned by f, gRPC checks the error's Temporary() method to decide if it
  338. // should try to reconnect to the network address.
  339. //
  340. // Deprecated: use WithContextDialer instead. Will be supported throughout
  341. // 1.x.
  342. func WithDialer(f func(string, time.Duration) (net.Conn, error)) DialOption {
  343. return WithContextDialer(
  344. func(ctx context.Context, addr string) (net.Conn, error) {
  345. if deadline, ok := ctx.Deadline(); ok {
  346. return f(addr, time.Until(deadline))
  347. }
  348. return f(addr, 0)
  349. })
  350. }
  351. // WithStatsHandler returns a DialOption that specifies the stats handler for
  352. // all the RPCs and underlying network connections in this ClientConn.
  353. func WithStatsHandler(h stats.Handler) DialOption {
  354. return newFuncDialOption(func(o *dialOptions) {
  355. o.copts.StatsHandler = h
  356. })
  357. }
  358. // FailOnNonTempDialError returns a DialOption that specifies if gRPC fails on
  359. // non-temporary dial errors. If f is true, and dialer returns a non-temporary
  360. // error, gRPC will fail the connection to the network address and won't try to
  361. // reconnect. The default value of FailOnNonTempDialError is false.
  362. //
  363. // FailOnNonTempDialError only affects the initial dial, and does not do
  364. // anything useful unless you are also using WithBlock().
  365. //
  366. // This is an EXPERIMENTAL API.
  367. func FailOnNonTempDialError(f bool) DialOption {
  368. return newFuncDialOption(func(o *dialOptions) {
  369. o.copts.FailOnNonTempDialError = f
  370. })
  371. }
  372. // WithUserAgent returns a DialOption that specifies a user agent string for all
  373. // the RPCs.
  374. func WithUserAgent(s string) DialOption {
  375. return newFuncDialOption(func(o *dialOptions) {
  376. o.copts.UserAgent = s
  377. })
  378. }
  379. // WithKeepaliveParams returns a DialOption that specifies keepalive parameters
  380. // for the client transport.
  381. func WithKeepaliveParams(kp keepalive.ClientParameters) DialOption {
  382. if kp.Time < internal.KeepaliveMinPingTime {
  383. logger.Warningf("Adjusting keepalive ping interval to minimum period of %v", internal.KeepaliveMinPingTime)
  384. kp.Time = internal.KeepaliveMinPingTime
  385. }
  386. return newFuncDialOption(func(o *dialOptions) {
  387. o.copts.KeepaliveParams = kp
  388. })
  389. }
  390. // WithUnaryInterceptor returns a DialOption that specifies the interceptor for
  391. // unary RPCs.
  392. func WithUnaryInterceptor(f UnaryClientInterceptor) DialOption {
  393. return newFuncDialOption(func(o *dialOptions) {
  394. o.unaryInt = f
  395. })
  396. }
  397. // WithChainUnaryInterceptor returns a DialOption that specifies the chained
  398. // interceptor for unary RPCs. The first interceptor will be the outer most,
  399. // while the last interceptor will be the inner most wrapper around the real call.
  400. // All interceptors added by this method will be chained, and the interceptor
  401. // defined by WithUnaryInterceptor will always be prepended to the chain.
  402. func WithChainUnaryInterceptor(interceptors ...UnaryClientInterceptor) DialOption {
  403. return newFuncDialOption(func(o *dialOptions) {
  404. o.chainUnaryInts = append(o.chainUnaryInts, interceptors...)
  405. })
  406. }
  407. // WithStreamInterceptor returns a DialOption that specifies the interceptor for
  408. // streaming RPCs.
  409. func WithStreamInterceptor(f StreamClientInterceptor) DialOption {
  410. return newFuncDialOption(func(o *dialOptions) {
  411. o.streamInt = f
  412. })
  413. }
  414. // WithChainStreamInterceptor returns a DialOption that specifies the chained
  415. // interceptor for streaming RPCs. The first interceptor will be the outer most,
  416. // while the last interceptor will be the inner most wrapper around the real call.
  417. // All interceptors added by this method will be chained, and the interceptor
  418. // defined by WithStreamInterceptor will always be prepended to the chain.
  419. func WithChainStreamInterceptor(interceptors ...StreamClientInterceptor) DialOption {
  420. return newFuncDialOption(func(o *dialOptions) {
  421. o.chainStreamInts = append(o.chainStreamInts, interceptors...)
  422. })
  423. }
  424. // WithAuthority returns a DialOption that specifies the value to be used as the
  425. // :authority pseudo-header. This value only works with WithInsecure and has no
  426. // effect if TransportCredentials are present.
  427. func WithAuthority(a string) DialOption {
  428. return newFuncDialOption(func(o *dialOptions) {
  429. o.authority = a
  430. })
  431. }
  432. // WithChannelzParentID returns a DialOption that specifies the channelz ID of
  433. // current ClientConn's parent. This function is used in nested channel creation
  434. // (e.g. grpclb dial).
  435. //
  436. // This API is EXPERIMENTAL.
  437. func WithChannelzParentID(id int64) DialOption {
  438. return newFuncDialOption(func(o *dialOptions) {
  439. o.channelzParentID = id
  440. })
  441. }
  442. // WithDisableServiceConfig returns a DialOption that causes gRPC to ignore any
  443. // service config provided by the resolver and provides a hint to the resolver
  444. // to not fetch service configs.
  445. //
  446. // Note that this dial option only disables service config from resolver. If
  447. // default service config is provided, gRPC will use the default service config.
  448. func WithDisableServiceConfig() DialOption {
  449. return newFuncDialOption(func(o *dialOptions) {
  450. o.disableServiceConfig = true
  451. })
  452. }
  453. // WithDefaultServiceConfig returns a DialOption that configures the default
  454. // service config, which will be used in cases where:
  455. //
  456. // 1. WithDisableServiceConfig is also used.
  457. // 2. Resolver does not return a service config or if the resolver returns an
  458. // invalid service config.
  459. //
  460. // This API is EXPERIMENTAL.
  461. func WithDefaultServiceConfig(s string) DialOption {
  462. return newFuncDialOption(func(o *dialOptions) {
  463. o.defaultServiceConfigRawJSON = &s
  464. })
  465. }
  466. // WithDisableRetry returns a DialOption that disables retries, even if the
  467. // service config enables them. This does not impact transparent retries, which
  468. // will happen automatically if no data is written to the wire or if the RPC is
  469. // unprocessed by the remote server.
  470. //
  471. // Retry support is currently disabled by default, but will be enabled by
  472. // default in the future. Until then, it may be enabled by setting the
  473. // environment variable "GRPC_GO_RETRY" to "on".
  474. //
  475. // This API is EXPERIMENTAL.
  476. func WithDisableRetry() DialOption {
  477. return newFuncDialOption(func(o *dialOptions) {
  478. o.disableRetry = true
  479. })
  480. }
  481. // WithMaxHeaderListSize returns a DialOption that specifies the maximum
  482. // (uncompressed) size of header list that the client is prepared to accept.
  483. func WithMaxHeaderListSize(s uint32) DialOption {
  484. return newFuncDialOption(func(o *dialOptions) {
  485. o.copts.MaxHeaderListSize = &s
  486. })
  487. }
  488. // WithDisableHealthCheck disables the LB channel health checking for all
  489. // SubConns of this ClientConn.
  490. //
  491. // This API is EXPERIMENTAL.
  492. func WithDisableHealthCheck() DialOption {
  493. return newFuncDialOption(func(o *dialOptions) {
  494. o.disableHealthCheck = true
  495. })
  496. }
  497. // withHealthCheckFunc replaces the default health check function with the
  498. // provided one. It makes tests easier to change the health check function.
  499. //
  500. // For testing purpose only.
  501. func withHealthCheckFunc(f internal.HealthChecker) DialOption {
  502. return newFuncDialOption(func(o *dialOptions) {
  503. o.healthCheckFunc = f
  504. })
  505. }
  506. func defaultDialOptions() dialOptions {
  507. return dialOptions{
  508. disableRetry: !envconfig.Retry,
  509. healthCheckFunc: internal.HealthCheckFunc,
  510. copts: transport.ConnectOptions{
  511. WriteBufferSize: defaultWriteBufSize,
  512. ReadBufferSize: defaultReadBufSize,
  513. },
  514. resolveNowBackoff: internalbackoff.DefaultExponential.Backoff,
  515. withProxy: true,
  516. }
  517. }
  518. // withGetMinConnectDeadline specifies the function that clientconn uses to
  519. // get minConnectDeadline. This can be used to make connection attempts happen
  520. // faster/slower.
  521. //
  522. // For testing purpose only.
  523. func withMinConnectDeadline(f func() time.Duration) DialOption {
  524. return newFuncDialOption(func(o *dialOptions) {
  525. o.minConnectTimeout = f
  526. })
  527. }
  528. // withResolveNowBackoff specifies the function that clientconn uses to backoff
  529. // between successive calls to resolver.ResolveNow().
  530. //
  531. // For testing purpose only.
  532. func withResolveNowBackoff(f func(int) time.Duration) DialOption {
  533. return newFuncDialOption(func(o *dialOptions) {
  534. o.resolveNowBackoff = f
  535. })
  536. }
  537. // WithResolvers allows a list of resolver implementations to be registered
  538. // locally with the ClientConn without needing to be globally registered via
  539. // resolver.Register. They will be matched against the scheme used for the
  540. // current Dial only, and will take precedence over the global registry.
  541. //
  542. // This API is EXPERIMENTAL.
  543. func WithResolvers(rs ...resolver.Builder) DialOption {
  544. return newFuncDialOption(func(o *dialOptions) {
  545. o.resolvers = append(o.resolvers, rs...)
  546. })
  547. }