Kitex provides Short Connection, Long Connection Pool and Connection Multiplexing for different business scenarios. Kitex uses Long Connection Pool by default after v0.0.2, but adjusting the Pool Config according to your need is suggested.
Every request needs to create a connection, the performance is bad, so it is not suggested normally.
Enable Short Connection:
xxxCli := xxxservice.NewClient("destServiceName", client.WithShortConnection())
Kitex enable Long Connection Pool after >= v0.0.2, default config params are as below:
connpool2.IdleConfig{
MaxIdlePerAddress: 10,
MaxIdleGlobal: 100,
MaxIdleTimeout: time.Minute,
MinIdlePerAddress: 2,
}
Adjusting the Pool Config according to your need is suggested, config as below:
xxxCli := xxxservice.NewClient("destServiceName", client.WithLongConnection(connpool.IdleConfig{10, 1000, time.Minute}))
Parameter description:
MaxIdlePerAddress
: the maximum number of idle connections per downstream instanceMaxIdleGlobal
: the global maximum number of idle connectionsMaxIdleTimeout
: the idle duration of the connection. A connection that has been idle for more than MaxIdleTimeout will be closed (minimum value is 3s, the default value is 30s)MinIdlePerAddress
(Kitex >= v0.4.3)MaxIdleTimeout
.MinIdlePerAddress
should be less than 5.Each downstream address corresponds to a connection pool, the connection pool is a ring composed of connections, and the size of the ring is MaxIdlePerAddress
.
When getting a connection of downstream address, proceed as follows:
MaxIdlePerAddress
When the connection is ready to be returned after used, proceed as follows:
MaxIdleGlobal
, and if yes, close it directlyThe setting of parameters is suggested as follows:
MaxIdlePerAddress
: the minimum value is 1, otherwise long connections would degenerate to short connectionsMaxIdlePerAddress = qps_per_dest_host*avg_response_time_sec
MinIdlePerAddress
: Assuming that there are periodic requests and the period is greater than MaxIdleTimeout
, setting this parameter can avoid creating a new connection every time.MaxIdlePerAddress
and can be set according to the average latency of requests and the throughput.MinIdlePerAddress
is set to 5 and the response time of each request is 100ms. 50 requests can be processed per second (50QPS) without creating a new connection.MaxIdleGlobal
: should be larger than the total number of downstream targets number * MaxIdlePerAddress
MaxIdleTimeout
: since the server will clean up inactive connections within 10min, the client also needs to clean up long-idle connections in time to avoid using invalid connections. This value cannot exceed 10min when the downstream is also a Kitex serviceThe client invokes the Server only need one connection normally when enabling Connection Multiplexing. Connection Multiplexing not only reduces the number of connections but also performs better than Connection Pool.
Special Note:
Server Side Enable:
option: WithMuxTransport
svr := xxxservice.NewServer(handler, server.WithMuxTransport())
Client Side Enable:
option: WithMuxConnection
1-2 connection is enough normally, it is no need to config more.
xxxCli := NewClient("destServiceName", client.WithMuxConnection(1))
Connection pooling defines the Reporter
interface for connection pool status monitoring, such as the reuse rate of long connections.
Users should implement the interface themselves and inject it by SetReporter
.
// Reporter report status of the connection pool.
type Reporter interface {
ConnSucceed(poolType ConnectionPoolType, serviceName string, addr net.Addr)
ConnFailed(poolType ConnectionPoolType, serviceName string, addr net.Addr)
ReuseSucceed(poolType ConnectionPoolType, serviceName string, addr net.Addr)
}
// SetReporter set the common reporter of a connection pool, that can only be set once.
func SetReporter(r Reporter)