当前位置:网站首页>Detailed explanation of connection pool parameter settings (view while adjusting)

Detailed explanation of connection pool parameter settings (view while adjusting)

2022-06-21 10:50:00 kevwan

Do you feel the same ?

When we are developing the server code , Do you often have the following questions ?

  • I wonder MySQL How many connections are there in the connection pool ?
  • How long does the life cycle of each connection last ?
  • When the connection is disconnected abnormally, the server actively disconnects , The client actively disconnected ?
  • When there is no request for a long time , Whether the underlying library has KeepAlive request ?

The processing of complex network conditions has always been one of the key and difficult points of back-end development , Do you also feel cold for the debugging of various network conditions ?

So I wrote tproxy

When I was doing back-end development and writing go-zero When , It is often necessary to monitor the network connection , Analyze request content . such as :

  • analysis gRPC Connect when to connect 、 When to reconnect , And adjust various parameters accordingly , such as :MaxConnectionIdle
  • analysis MySQL Connection pool , How many connections are there currently , What is the strategy for the life cycle of a connection
  • It can also be used to observe and analyze any TCP Connect , Look at the active disconnection of the server , Or is the client actively disconnected

tproxy Installation

$ GOPROXY=https://goproxy.cn/,direct go install github.com/kevwan/[email protected]

Or use docker Mirror image :

$ docker run --rm -it -p <listen-port>:<listen-port> -p <remote-port>:<remote-port> kevinwan/tproxy:v1 tproxy -l 0.0.0.0 -p <listen-port> -r host.docker.internal:<remote-port>

arm64 System :

$ docker run --rm -it -p <listen-port>:<listen-port> -p <remote-port>:<remote-port> kevinwan/tproxy:v1-arm64 tproxy -l 0.0.0.0 -p <listen-port> -r host.docker.internal:<remote-port>

tproxy Usage of

$ tproxy --helpUsage of tproxy:  -d duration            the delay to relay packets  -l string            Local address to listen on (default "localhost")  -p int            Local port to listen on  -q        Quiet mode, only prints connection open/close and stats, default false  -r string            Remote address (host:port) to connect  -t string            The type of protocol, currently support grpc

analysis gRPC Connect

tproxy -p 8088 -r localhost:8081 -t grpc -d 100ms
  • Listen on localhost and 8088 port
  • Redirect request to localhost:8081
  • The format of the identification packet is gRPC
  • Packet delay 100 millisecond

img

Among them we can see gRPC Initialization and round trip of a request , You can see the first request in which stream id by 1.

Another example gRPC There is one MaxConnectionIdle Parameters , Used to set idle How long will the connection be closed , We can directly observe that the server will send a message after the time http2 Of GoAway package .

img

For example, I put MaxConnectioinIdle Set to 5 minute , After successful connection 5 Minutes no request , The connection is automatically closed , Then a new connection was built .

analysis MySQL Connect

Let's analyze MySQL The impact of connection pool settings on connection pools , For example, I set the parameter to :

maxIdleConns = 3maxOpenConns = 8maxLifetime  = time.Minute...conn.SetMaxIdleConns(maxIdleConns)conn.SetMaxOpenConns(maxOpenConns)conn.SetConnMaxLifetime(maxLifetime)

We put MaxIdleConns and MaxOpenConns Set to different values , And then we use hey Let's do a pressure test :

hey -c 10 -z 10s "http://localhost:8888/lookup?url=go-zero.dev"

We did the concurrency as 10QPS And sustained 10 Second pressure test , The connection result is shown in the figure below :

img

We can see :

  • 10 Within seconds 2000+ The connection of
  • In the process, the existing connection is closed continuously , Reopen a new connection
  • Put it back after each connection , May exceed MaxIdleConns 了 , Then the connection will be closed
  • Then a new request comes to get the connection , The number of connections found is less than MaxOpenConns, But there are no available requests , So a new connection is created

This is what we often see MySQL quite a lot TIME_WAIT Why .

And then we put MaxIdleConns and MaxOpenConns Set to the same value , Then do the same pressure test again :

img

We can see :

  • It's been maintained 8 Connections do not change
  • One minute after the pressure test (ConnMaxLifetime), All connections are closed

there ConnMaxLifetime Be sure to set less than wait_timeout, You can view it in the following ways wait_timeout value :

img

I suggest setting less than 5 The value of minutes , Because there are some exchange opportunities 5 Clean up idle connections in minutes , For example, when we are socializing , Generally, the heartbeat package will not exceed 5 minute . Specific reasons can be seen

https://github.com/zeromicro/go-zero/blob/master/core/stores/sqlx/sqlmanager.go#L65

among go-sql-driver Of issue 257 There's a passage in that ConnMaxLifetime, as follows :

> 14400 sec is too long. One minutes is enough for most use cases. > > Even if you configure entire your DC (OS, switch, router, etc...), TCP connection may be lost from various reasons. (bug in router firmware, unstable power voltage, electric nose, etc...)

So if you don't know MySQL How to set connection pool parameters , You can refer to go-zero Set up .

in addition ,ConnMaxIdleTime It has no effect on the above pressure measurement results , In fact, you don't need to set it .

If you have any questions about the above settings , Or think there is something wrong , Welcome to the go-zero Discuss together in the group .

Project address

tproxy: https://github.com/kevwan/tproxy

go-zero:

https://github.com/zeromicro/go-zero

https://gitee.com/kevwan/go-zero

Welcome to and star Support us !

WeChat ac group

Focus on 『 Microservice practice 』 Official account and click Communication group Get community group QR code .</remote-port></listen-port></remote-port></remote-port></listen-port></listen-port></remote-port></listen-port></remote-port></remote-port></listen-port></listen-port>

原网站

版权声明
本文为[kevwan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206211031513757.html