dialoptions.go 22 KB

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