| title | Client Option | |||
|---|---|---|---|---|
| date | 2022-06-20 | |||
| weight | 1 | |||
| keywords |
|
|||
| description | Kitex Client Option instructions. |
Add some options when creating a client:
client, err := echo.NewClient("targetService", client.WithXXXX...)func WithClientBasicInfo(ebi *rpcinfo.EndpointBasicInfo) OptionSet the service infos for client, including ServiceName and customized Tags, customized Tag such as Cluster, IDC, Env, and it is no need to set Method field of EndpointBasicInfo. It is strongly recommended to configure this option.
func WithHostPorts(hostports ...string) OptionManually specifie one or more targets overrides the results discovered by the service and directly connects to the access.
func WithTransportProtocol(tp transport.Protocol) OptionSet the transport protocol, configure the transport protocol on the message protocol. Thrift/KitexProtobuf can configure TTHeader, TTHeaderFramed, and Framed. In addition, Framed is not strictly a transmission protocol. In order to distinguish it for PurePayload, it is also configured as a transmission protocol. PurePayload means that there is no transmission protocol; if it is configured as GRPC, it means that the GRPC protocol is used. , the transmission protocol of GRPC is HTTP2, but for the convenience of users' understanding, it is directly used as the configuration of the transmission protocol. Note that configuring GRPC needs to use Protobuf to define Service. If GRPC is not configured, KitexProtobuf protocol is used by default.
When WithTransportProtocol is not set, the default protocol is PurePayload
func WithShortConnection() OptionEnable short connections. More
func WithLongConnection(cfg connpool.IdleConfig) OptionEnable long connections. More
⚠️ DeprecatedReason see Connection Multiplexing.
func WithMuxConnection(connNum int) OptionEnable mux connections. Server side also need to turn on this option, or it won't work. More
func WithMiddleware(mw endpoint.Middleware) OptionAdd a middleware that executes after service level circuit breaker and timeout middleware. More
func WithInstanceMW(mw endpoint.Middleware) OptionAdd a middleware that executes after service discovery and load balance. If instance level circuit breaker exists, then it will execute after that. (If proxy is used, it will not be called, such as mesh mode). More
func WithMiddlewareBuilder(mwb endpoint.MiddlewareBuilder) OptionAdd middleware depends on the context passed in by the framework that contains runtime configuration information (the context of non-RPC calls), so that the middleware can take advantage of the framework's information when initializing.
func WithStreamOptions(opts ...client.StreamOption) OptionKitex >= v0.13.0 adds this option.
Aggregate streaming-related options such as StreamMiddleware, StreamSendMiddleware, StreamRecvMiddleware, RecvTimeout. Works for streaming methods. See StreamX Middleware.
func WithCircuitBreaker(s *circuitbreak.CBSuite) OptionSet the circuit breaker, which includes service circuit break and instance circuit break by default, using the example:
var opts []client.Option
cbs := circuitbreak.NewCBSuite(circuitbreak.RPCInfo2Key)
opts = append(opts, client.WithCloseCallbacks(func() error {
// Circuit Breaker Close method is injected into CloseCallbacks to release circuit breaker related resources when the client is destroyed
return cs.cbs.Close()
}))
opts = append(opts, client.WithCircuitBreaker(cbs))
// Dynamically updates the circuit breaker configuration
cbs.UpdateServiceCBConfig(key, config)
cbs.UpdateInstanceCBConfig(key, config)For more details, please visit Circuit Breaker.
func WithFailureRetry(p *retry.FailurePolicy) OptionSet timeout retry rules, you can configure the maximum number of retries, the maximum time spent accumulated, the threshold of the retry circuit fault rate, the DDL abort and backoff policy. More
func WithBackupRequest(p *retry.BackupPolicy) OptionSet the policy for Backup Request, which can configure the number of requests, circuit breaker abort, and link abort. More
func WithRPCTimeout(d time.Duration) OptionSet RPC timeout. More
func WithConnectTimeout(d time.Duration) OptionSet connect timeout. More
func WithTimeoutProvider(p rpcinfo.TimeoutProvider) OptionAdd a TimeoutProvider to set the RPC timeout, connection timeout, etc. policies as a whole. If You use Both WithRPCTimeout or WithConnectTimeout, the settings here will be overridden.
func WithDestService(svr string) OptionSpecify the service name of the target side of the call.
func WithTag(key, val string) OptionAdd some meta information to the client, such as idc, cluster, etc., for scenarios such as auxiliary service discovery.
func WithStatsLevel(level stats.Level) OptiongSet the stats level for client. More
These options only works for scenarios where the transport protocol uses gRPC, with some parameter adjustments to gRPC transfers.
func WithGRPCConnPoolSize(s uint32) OptionWithGRPCConnPoolSize sets the value for the client connection pool size. In general, you should not adjust the size of the connection pool, otherwise it may cause performance degradation. You should adjust the size according to the actual situation.
func WithGRPCWriteBufferSize(s uint32) OptionWithGRPCWriteBufferSize 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 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. It corresponds to the WriteBufferSize ServerOption of gRPC.
func WithGRPCReadBufferSize(s uint32) OptionWithGRPCReadBufferSize lets you set the size of read buffer, this determines how much data can be read at most for one read syscall. The default value for this buffer is 32KB. Zero will disable read buffer for a connection so data framer can access the underlying conn directly. It corresponds to the ReadBufferSize ServerOption of gRPC.
func WithGRPCInitialWindowSize(s uint32) OptionWithGRPCInitialWindowSize returns a Option that sets window size for stream. The lower bound for window size is 64K and any value smaller than that will be ignored. It corresponds to the InitialWindowSize ServerOption of gRPC.
func WithGRPCInitialConnWindowSize(s uint32) OptionWithGRPCInitialConnWindowSize returns an Option that sets window size for a connection. The lower bound for window size is 64K and any value smaller than that will be ignored. It corresponds to the InitialConnWindowSize ServerOption of gRPC.
func WithGRPCMaxHeaderListSize(s uint32) OptionWithGRPCMaxHeaderListSize returns a ServerOption that sets the max (uncompressed) size of header list that the server is prepared to accept. It corresponds to the MaxHeaderListSize ServerOption of gRPC.
func WithGRPCKeepaliveParams(kp grpc.ClientKeepalive) OptionWithGRPCKeepaliveParams returns a DialOption that specifies keepalive parameters for the client transport. It corresponds to the WithKeepaliveParams DialOption of gRPC.
func WithGRPCTLSConfig(tlsConfig *tls.Config) OptionWithGRPCTLSConfig sets the TLS config to the connection options for Kitex gRPC client.
func WithSuite(suite Suite) OptionSet up a specific configuration, customize according to the scene, configure multiple options and middlewares combinations and encapsulations in the Suite. More
func WithProxy(p proxy.ForwardProxy) OptionFor proxy scenarios (such as Mesh Egress), do some configuration processing, return proxy address, configure proxy. After ForwardProxy, the framework does not perform service discovery, circuit breakers, and InstanceMWs.
func WithRetryContainer(rc *retry.Container) OptionManually set up RetryContainer. Used to retry the strategy in combination with circuit breakers. At present, three rapid implementation schemes are available: NewRetryContainer, NewRetryContainerWithCB and NewRetryContainerWithCBStat.
- NewRetryContainerWithCB (recommended)
If you have already configured the circuit breaker, it is recommended to reuse the circuit breaker with RetryContainer to avoid additional statistics, you can use the NewRetryContainerWithCB, such as the example in the example below, enable the circuit breaker scenario, and at the same time pass the circuit breaker to RetryContainer:
cbs := circuitbreak.NewCBSuite(circuitbreak.RPCInfo2Key)
retryC := retry.NewRetryContainerWithCB(cbs.ServiceControl(), cbs.ServicePanel())
var opts []client.Option
opts = append(opts, client.WithRetryContainer(retryC))
// enable service circuit breaker
opts = append(opts, client.WithMiddleware(cbs.ServiceCBMW()))- NewRetryContainer
- Specifies the default RetryContainer for the retry policy, which has a built-in circuit breaker.
- NewRetryContainerWithCBStat
To customize the built-in circuit breaker ServiceCBKeyFunc settings, you can use the NewRetryContainerWithCBStat method:
cbs := circuitbreak.NewCBSuite(YourGenServiceCBKeyFunc)
retry.NewRetryContainerWithCBStat(cbs.ServiceControl(), cbs.ServicePanel())func WithWarmingUp(wuo *warmup.ClientOption) OptionSet warming up option. Kitex supports client warm-up, which allows you to pre-initialize the relevant components of service discovery and connection pooling when creating the client, avoiding large delays on the first request.
Warm up service discovery:
cli, err := myservice.NewClient(psm, client.WithWarmingUp(&warmup.ClientOption{
ResolverOption: &warmup.ResolverOption{
Dests: []*rpcinfo.EndpointBasicInfo{
&rpcinfo.EndpointBasicInfo{
ServiceName: psm,
Method: method,
Tags: map[string]string{
"cluster": "default",
},
},
},
},
}))Warm up connection pool:
cli, err := myservice.NewClient(psm, client.WithWarmingUp(&warmup.ClientOption{
PoolOption: &warmup.PoolOption{
ConnNum: 2,
},
}))func WithCloseCallbacks(callback func() error) OptionSet close callback function.
func WithErrorHandler(f func(error) error) OptionSet the error handler function, which is executed after the server handler is executed and before the middleware executes.
func WithGeneric(g generic.Generic) OptionSpecifie the generalization call type, which needs to be used in conjunction with the generalization Client/Server. More
func WithACLRules(rules ...acl.RejectFunc) OptionSet ACL permission access control, which is executed before service discovery. More
func WithConnReporterEnabled() OptionEnable connection pool reporter. More
func WithHTTPConnection() OptionSpecifie client use RPC over http.
func WithTracer(c stats.Tracer) OptionAdd an additional Tracer. More
func WithResolver(r discovery.Resolver) OptionSpecifie a resolver to do service discovery. More
func WithHTTPResolver(r http.Resolver) OptionSet HTTP resolver. More
func WithLoadBalancer(lb loadbalance.Loadbalancer, opts ...*lbcache.Options) OptionSet load balancer. More
func WithBoundHandler(h remote.BoundHandler) OptionAdd a new IO Bound handler. More
func WithCodec(c remote.Codec) OptionSpecifie a Codec for scenarios that require custom protocol. More
func WithPayloadCodec(c remote.PayloadCodec) OptionSpecifie a PayloadCodec. More
func WithMetaHandler(h remote.MetaHandler) OptionAdd a meta handler for customizing transparent information in conjunction with the transport protocol, such as service name, invocation method, machine room, cluster, env, tracerInfo. More
func WithFirstMetaHandler(h remote.MetaHandler) OptionAdd a meta handler at the first position.
func WithTransHandlerFactory(f remote.ClientTransHandlerFactory) OptionSet transHandlerFactory. More
func WithDiagnosisService(ds diagnosis.Service) OptionSet diagnosis service. More
func WithDialer(d remote.Dialer) OptionSet dialer.
func WithConnPool(pool remote.ConnPool) OptionSet connection pool.