Source File
dialoptions.go
Belonging Package
google.golang.org/grpc
/*
*
* Copyright 2018 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package grpc
import (
internalbackoff
)
func () {
internal.AddGlobalDialOptions = func( ...DialOption) {
extraDialOptions = append(extraDialOptions, ...)
}
internal.ClearGlobalDialOptions = func() {
extraDialOptions = nil
}
internal.WithBinaryLogger = withBinaryLogger
internal.JoinDialOptions = newJoinDialOption
}
// dialOptions configure a Dial call. dialOptions are set by the DialOption
// values passed to Dial.
type dialOptions struct {
unaryInt UnaryClientInterceptor
streamInt StreamClientInterceptor
chainUnaryInts []UnaryClientInterceptor
chainStreamInts []StreamClientInterceptor
cp Compressor
dc Decompressor
bs internalbackoff.Strategy
block bool
returnLastError bool
timeout time.Duration
scChan <-chan ServiceConfig
authority string
binaryLogger binarylog.Logger
copts transport.ConnectOptions
callOptions []CallOption
channelzParentID *channelz.Identifier
disableServiceConfig bool
disableRetry bool
disableHealthCheck bool
healthCheckFunc internal.HealthChecker
minConnectTimeout func() time.Duration
defaultServiceConfig *ServiceConfig // defaultServiceConfig is parsed from defaultServiceConfigRawJSON.
defaultServiceConfigRawJSON *string
resolvers []resolver.Builder
}
// DialOption configures how we set up the connection.
type DialOption interface {
apply(*dialOptions)
}
var extraDialOptions []DialOption
// EmptyDialOption does not alter the dial configuration. It can be embedded in
// another structure to build custom dial options.
//
// # Experimental
//
// Notice: This type is EXPERIMENTAL and may be changed or removed in a
// later release.
type EmptyDialOption struct{}
func (EmptyDialOption) (*dialOptions) {}
// funcDialOption wraps a function that modifies dialOptions into an
// implementation of the DialOption interface.
type funcDialOption struct {
f func(*dialOptions)
}
func ( *funcDialOption) ( *dialOptions) {
.f()
}
func ( func(*dialOptions)) *funcDialOption {
return &funcDialOption{
f: ,
}
}
type joinDialOption struct {
opts []DialOption
}
func ( *joinDialOption) ( *dialOptions) {
for , := range .opts {
.apply()
}
}
func ( ...DialOption) DialOption {
return &joinDialOption{opts: }
}
// WithWriteBufferSize determines how much data can be batched before doing a
// write on the wire. The corresponding memory allocation for this buffer will
// be twice the size to keep syscalls low. The default value for this buffer is
// 32KB.
//
// Zero or negative values will disable the write buffer such that each write
// will be on underlying connection. Note: A Send call may not directly
// translate to a write.
func ( int) DialOption {
return newFuncDialOption(func( *dialOptions) {
.copts.WriteBufferSize =
})
}
// WithReadBufferSize lets you set the size of read buffer, this determines how
// much data can be read at most for each read syscall.
//
// The default value for this buffer is 32KB. Zero or negative values will
// disable read buffer for a connection so data framer can access the
// underlying conn directly.
func ( int) DialOption {
return newFuncDialOption(func( *dialOptions) {
.copts.ReadBufferSize =
})
}
// WithInitialWindowSize returns a DialOption which sets the value for initial
// window size on a stream. The lower bound for window size is 64K and any value
// smaller than that will be ignored.
func ( int32) DialOption {
return newFuncDialOption(func( *dialOptions) {
.copts.InitialWindowSize =
})
}
// WithInitialConnWindowSize returns a DialOption which sets the value for
// initial window size on a connection. The lower bound for window size is 64K
// and any value smaller than that will be ignored.
func ( int32) DialOption {
return newFuncDialOption(func( *dialOptions) {
.copts.InitialConnWindowSize =
})
}
// WithMaxMsgSize returns a DialOption which sets the maximum message size the
// client can receive.
//
// Deprecated: use WithDefaultCallOptions(MaxCallRecvMsgSize(s)) instead. Will
// be supported throughout 1.x.
func ( int) DialOption {
return WithDefaultCallOptions(MaxCallRecvMsgSize())
}
// WithDefaultCallOptions returns a DialOption which sets the default
// CallOptions for calls over the connection.
func ( ...CallOption) DialOption {
return newFuncDialOption(func( *dialOptions) {
.callOptions = append(.callOptions, ...)
})
}
// WithCodec returns a DialOption which sets a codec for message marshaling and
// unmarshaling.
//
// Deprecated: use WithDefaultCallOptions(ForceCodec(_)) instead. Will be
// supported throughout 1.x.
func ( Codec) DialOption {
return WithDefaultCallOptions(CallCustomCodec())
}
// WithCompressor returns a DialOption which sets a Compressor to use for
// message compression. It has lower priority than the compressor set by the
// UseCompressor CallOption.
//
// Deprecated: use UseCompressor instead. Will be supported throughout 1.x.
func ( Compressor) DialOption {
return newFuncDialOption(func( *dialOptions) {
.cp =
})
}
// WithDecompressor returns a DialOption which sets a Decompressor to use for
// incoming message decompression. If incoming response messages are encoded
// using the decompressor's Type(), it will be used. Otherwise, the message
// encoding will be used to look up the compressor registered via
// encoding.RegisterCompressor, which will then be used to decompress the
// message. If no compressor is registered for the encoding, an Unimplemented
// status error will be returned.
//
// Deprecated: use encoding.RegisterCompressor instead. Will be supported
// throughout 1.x.
func ( Decompressor) DialOption {
return newFuncDialOption(func( *dialOptions) {
.dc =
})
}
// WithServiceConfig returns a DialOption which has a channel to read the
// service configuration.
//
// Deprecated: service config should be received through name resolver or via
// WithDefaultServiceConfig, as specified at
// https://github.com/grpc/grpc/blob/master/doc/service_config.md. Will be
// removed in a future 1.x release.
func ( <-chan ServiceConfig) DialOption {
return newFuncDialOption(func( *dialOptions) {
.scChan =
})
}
// WithConnectParams configures the ClientConn to use the provided ConnectParams
// for creating and maintaining connections to servers.
//
// The backoff configuration specified as part of the ConnectParams overrides
// all defaults specified in
// https://github.com/grpc/grpc/blob/master/doc/connection-backoff.md. Consider
// using the backoff.DefaultConfig as a base, in cases where you want to
// override only a subset of the backoff configuration.
func ( ConnectParams) DialOption {
return newFuncDialOption(func( *dialOptions) {
.bs = internalbackoff.Exponential{Config: .Backoff}
.minConnectTimeout = func() time.Duration {
return .MinConnectTimeout
}
})
}
// WithBackoffMaxDelay configures the dialer to use the provided maximum delay
// when backing off after failed connection attempts.
//
// Deprecated: use WithConnectParams instead. Will be supported throughout 1.x.
func ( time.Duration) DialOption {
return WithBackoffConfig(BackoffConfig{MaxDelay: })
}
// WithBackoffConfig configures the dialer to use the provided backoff
// parameters after connection failures.
//
// Deprecated: use WithConnectParams instead. Will be supported throughout 1.x.
func ( BackoffConfig) DialOption {
:= backoff.DefaultConfig
.MaxDelay = .MaxDelay
return withBackoff(internalbackoff.Exponential{Config: })
}
// withBackoff sets the backoff strategy used for connectRetryNum after a failed
// connection attempt.
//
// This can be exported if arbitrary backoff strategies are allowed by gRPC.
func ( internalbackoff.Strategy) DialOption {
return newFuncDialOption(func( *dialOptions) {
.bs =
})
}
// WithBlock returns a DialOption which makes callers of Dial block until the
// underlying connection is up. Without this, Dial returns immediately and
// connecting the server happens in background.
func () DialOption {
return newFuncDialOption(func( *dialOptions) {
.block = true
})
}
// WithReturnConnectionError returns a DialOption which makes the client connection
// return a string containing both the last connection error that occurred and
// the context.DeadlineExceeded error.
// Implies WithBlock()
//
// # Experimental
//
// Notice: This API is EXPERIMENTAL and may be changed or removed in a
// later release.
func () DialOption {
return newFuncDialOption(func( *dialOptions) {
.block = true
.returnLastError = true
})
}
// WithInsecure returns a DialOption which disables transport security for this
// ClientConn. Under the hood, it uses insecure.NewCredentials().
//
// Note that using this DialOption with per-RPC credentials (through
// WithCredentialsBundle or WithPerRPCCredentials) which require transport
// security is incompatible and will cause grpc.Dial() to fail.
//
// Deprecated: use WithTransportCredentials and insecure.NewCredentials()
// instead. Will be supported throughout 1.x.
func () DialOption {
return newFuncDialOption(func( *dialOptions) {
.copts.TransportCredentials = insecure.NewCredentials()
})
}
// WithNoProxy returns a DialOption which disables the use of proxies for this
// ClientConn. This is ignored if WithDialer or WithContextDialer are used.
//
// # Experimental
//
// Notice: This API is EXPERIMENTAL and may be changed or removed in a
// later release.
func () DialOption {
return newFuncDialOption(func( *dialOptions) {
.copts.UseProxy = false
})
}
// WithTransportCredentials returns a DialOption which configures a connection
// level security credentials (e.g., TLS/SSL). This should not be used together
// with WithCredentialsBundle.
func ( credentials.TransportCredentials) DialOption {
return newFuncDialOption(func( *dialOptions) {
.copts.TransportCredentials =
})
}
// WithPerRPCCredentials returns a DialOption which sets credentials and places
// auth state on each outbound RPC.
func ( credentials.PerRPCCredentials) DialOption {
return newFuncDialOption(func( *dialOptions) {
.copts.PerRPCCredentials = append(.copts.PerRPCCredentials, )
})
}
// WithCredentialsBundle returns a DialOption to set a credentials bundle for
// the ClientConn.WithCreds. This should not be used together with
// WithTransportCredentials.
//
// # Experimental
//
// Notice: This API is EXPERIMENTAL and may be changed or removed in a
// later release.
func ( credentials.Bundle) DialOption {
return newFuncDialOption(func( *dialOptions) {
.copts.CredsBundle =
})
}
// WithTimeout returns a DialOption that configures a timeout for dialing a
// ClientConn initially. This is valid if and only if WithBlock() is present.
//
// Deprecated: use DialContext instead of Dial and context.WithTimeout
// instead. Will be supported throughout 1.x.
func ( time.Duration) DialOption {
return newFuncDialOption(func( *dialOptions) {
.timeout =
})
}
// WithContextDialer returns a DialOption that sets a dialer to create
// connections. If FailOnNonTempDialError() is set to true, and an error is
// returned by f, gRPC checks the error's Temporary() method to decide if it
// should try to reconnect to the network address.
func ( func(context.Context, string) (net.Conn, error)) DialOption {
return newFuncDialOption(func( *dialOptions) {
.copts.Dialer =
})
}
func () {
internal.WithHealthCheckFunc = withHealthCheckFunc
}
// WithDialer returns a DialOption that specifies a function to use for dialing
// network addresses. If FailOnNonTempDialError() is set to true, and an error
// is returned by f, gRPC checks the error's Temporary() method to decide if it
// should try to reconnect to the network address.
//
// Deprecated: use WithContextDialer instead. Will be supported throughout
// 1.x.
func ( func(string, time.Duration) (net.Conn, error)) DialOption {
return WithContextDialer(
func( context.Context, string) (net.Conn, error) {
if , := .Deadline(); {
return (, time.Until())
}
return (, 0)
})
}
// WithStatsHandler returns a DialOption that specifies the stats handler for
// all the RPCs and underlying network connections in this ClientConn.
func ( stats.Handler) DialOption {
return newFuncDialOption(func( *dialOptions) {
if == nil {
logger.Error("ignoring nil parameter in grpc.WithStatsHandler ClientOption")
// Do not allow a nil stats handler, which would otherwise cause
// panics.
return
}
.copts.StatsHandlers = append(.copts.StatsHandlers, )
})
}
// withBinaryLogger returns a DialOption that specifies the binary logger for
// this ClientConn.
func ( binarylog.Logger) DialOption {
return newFuncDialOption(func( *dialOptions) {
.binaryLogger =
})
}
// FailOnNonTempDialError returns a DialOption that specifies if gRPC fails on
// non-temporary dial errors. If f is true, and dialer returns a non-temporary
// error, gRPC will fail the connection to the network address and won't try to
// reconnect. The default value of FailOnNonTempDialError is false.
//
// FailOnNonTempDialError only affects the initial dial, and does not do
// anything useful unless you are also using WithBlock().
//
// # Experimental
//
// Notice: This API is EXPERIMENTAL and may be changed or removed in a
// later release.
func ( bool) DialOption {
return newFuncDialOption(func( *dialOptions) {
.copts.FailOnNonTempDialError =
})
}
// WithUserAgent returns a DialOption that specifies a user agent string for all
// the RPCs.
func ( string) DialOption {
return newFuncDialOption(func( *dialOptions) {
.copts.UserAgent =
})
}
// WithKeepaliveParams returns a DialOption that specifies keepalive parameters
// for the client transport.
func ( keepalive.ClientParameters) DialOption {
if .Time < internal.KeepaliveMinPingTime {
logger.Warningf("Adjusting keepalive ping interval to minimum period of %v", internal.KeepaliveMinPingTime)
.Time = internal.KeepaliveMinPingTime
}
return newFuncDialOption(func( *dialOptions) {
.copts.KeepaliveParams =
})
}
// WithUnaryInterceptor returns a DialOption that specifies the interceptor for
// unary RPCs.
func ( UnaryClientInterceptor) DialOption {
return newFuncDialOption(func( *dialOptions) {
.unaryInt =
})
}
// WithChainUnaryInterceptor returns a DialOption that specifies the chained
// interceptor for unary RPCs. The first interceptor will be the outer most,
// while the last interceptor will be the inner most wrapper around the real call.
// All interceptors added by this method will be chained, and the interceptor
// defined by WithUnaryInterceptor will always be prepended to the chain.
func ( ...UnaryClientInterceptor) DialOption {
return newFuncDialOption(func( *dialOptions) {
.chainUnaryInts = append(.chainUnaryInts, ...)
})
}
// WithStreamInterceptor returns a DialOption that specifies the interceptor for
// streaming RPCs.
func ( StreamClientInterceptor) DialOption {
return newFuncDialOption(func( *dialOptions) {
.streamInt =
})
}
// WithChainStreamInterceptor returns a DialOption that specifies the chained
// interceptor for streaming RPCs. The first interceptor will be the outer most,
// while the last interceptor will be the inner most wrapper around the real call.
// All interceptors added by this method will be chained, and the interceptor
// defined by WithStreamInterceptor will always be prepended to the chain.
func ( ...StreamClientInterceptor) DialOption {
return newFuncDialOption(func( *dialOptions) {
.chainStreamInts = append(.chainStreamInts, ...)
})
}
// WithAuthority returns a DialOption that specifies the value to be used as the
// :authority pseudo-header and as the server name in authentication handshake.
func ( string) DialOption {
return newFuncDialOption(func( *dialOptions) {
.authority =
})
}
// WithChannelzParentID returns a DialOption that specifies the channelz ID of
// current ClientConn's parent. This function is used in nested channel creation
// (e.g. grpclb dial).
//
// # Experimental
//
// Notice: This API is EXPERIMENTAL and may be changed or removed in a
// later release.
func ( *channelz.Identifier) DialOption {
return newFuncDialOption(func( *dialOptions) {
.channelzParentID =
})
}
// WithDisableServiceConfig returns a DialOption that causes gRPC to ignore any
// service config provided by the resolver and provides a hint to the resolver
// to not fetch service configs.
//
// Note that this dial option only disables service config from resolver. If
// default service config is provided, gRPC will use the default service config.
func () DialOption {
return newFuncDialOption(func( *dialOptions) {
.disableServiceConfig = true
})
}
// WithDefaultServiceConfig returns a DialOption that configures the default
// service config, which will be used in cases where:
//
// 1. WithDisableServiceConfig is also used, or
//
// 2. The name resolver does not provide a service config or provides an
// invalid service config.
//
// The parameter s is the JSON representation of the default service config.
// For more information about service configs, see:
// https://github.com/grpc/grpc/blob/master/doc/service_config.md
// For a simple example of usage, see:
// examples/features/load_balancing/client/main.go
func ( string) DialOption {
return newFuncDialOption(func( *dialOptions) {
.defaultServiceConfigRawJSON = &
})
}
// WithDisableRetry returns a DialOption that disables retries, even if the
// service config enables them. This does not impact transparent retries, which
// will happen automatically if no data is written to the wire or if the RPC is
// unprocessed by the remote server.
func () DialOption {
return newFuncDialOption(func( *dialOptions) {
.disableRetry = true
})
}
// WithMaxHeaderListSize returns a DialOption that specifies the maximum
// (uncompressed) size of header list that the client is prepared to accept.
func ( uint32) DialOption {
return newFuncDialOption(func( *dialOptions) {
.copts.MaxHeaderListSize = &
})
}
// WithDisableHealthCheck disables the LB channel health checking for all
// SubConns of this ClientConn.
//
// # Experimental
//
// Notice: This API is EXPERIMENTAL and may be changed or removed in a
// later release.
func () DialOption {
return newFuncDialOption(func( *dialOptions) {
.disableHealthCheck = true
})
}
// withHealthCheckFunc replaces the default health check function with the
// provided one. It makes tests easier to change the health check function.
//
// For testing purpose only.
func ( internal.HealthChecker) DialOption {
return newFuncDialOption(func( *dialOptions) {
.healthCheckFunc =
})
}
func () dialOptions {
return dialOptions{
healthCheckFunc: internal.HealthCheckFunc,
copts: transport.ConnectOptions{
WriteBufferSize: defaultWriteBufSize,
ReadBufferSize: defaultReadBufSize,
UseProxy: true,
},
}
}
// withGetMinConnectDeadline specifies the function that clientconn uses to
// get minConnectDeadline. This can be used to make connection attempts happen
// faster/slower.
//
// For testing purpose only.
func ( func() time.Duration) DialOption {
return newFuncDialOption(func( *dialOptions) {
.minConnectTimeout =
})
}
// WithResolvers allows a list of resolver implementations to be registered
// locally with the ClientConn without needing to be globally registered via
// resolver.Register. They will be matched against the scheme used for the
// current Dial only, and will take precedence over the global registry.
//
// # Experimental
//
// Notice: This API is EXPERIMENTAL and may be changed or removed in a
// later release.
func ( ...resolver.Builder) DialOption {
return newFuncDialOption(func( *dialOptions) {
.resolvers = append(.resolvers, ...)
})
}
The pages are generated with Golds v0.4.9. (GOOS=linux GOARCH=amd64)