当前位置:网站首页>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 grpcanalysis 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
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 .
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 :
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 :
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 :
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>
边栏推荐
- Yum source server configuration
- 【zz】owt-server:音视频转发示意图
- STL summary
- Esp8266/esp32 +1.3 "or 0.96" IIC OLED pointer clock
- Mqtt of NLog custom target
- How to learn function test? Ali engineer teaches 4 steps
- 03. Redis actual battle: meeting goddess nearby by geo type
- The "first city" in Central China. How can Changsha be built?
- Do website from scratch 11- blog development
- Classification of ram and ROM storage media
猜你喜欢

The backbone of the top 100 security companies! Meichuang technology was selected into the 2022 China top 100 Digital Security Report

Matplotlib 绘制圆环图的两种方法!

About Alipay - my savings plan - interest rate calculation instructions

Research and implementation of embedded software framework based on multi process architecture

One line of code accelerates sklearn operations thousands of times
![FastAPI Web框架 [Pydantic]](/img/e1/290a8a6a978b9fb56a9c86f1734c45.png)
FastAPI Web框架 [Pydantic]

The bilingual live broadcast of Oriental selection is popular, and the transformation of New Oriental is beginning to take shape

西电AI专业排名超清北,南大蝉联全国第一 | 2022软科中国大学专业排名
![leetcode:715. Range module [brainless segmenttree]](/img/70/6cfb071bb08b30945c31c4947d2cef.png)
leetcode:715. Range module [brainless segmenttree]

MySQL advanced - personal notes
随机推荐
【zz】owt-server:音视频转发示意图
WCF RestFul+JWT身份验证
记一次协程环境下类成员变量污染的问题
Port occupancy
DSP online upgrade (2) -- design framework of bootloader
MySQL 5.7都即将停只维护了,是时候学习一波MySQL 8了
Network multimedia -- linphone correlation analysis -- directory
Concept of naive Bayes
Running view of program
DSP online upgrade (3) -- how to burn two projects in the on-chip flash of a DSP chip
使用shapeit进行单倍型分析
Cvte side
K-means introduction
《Feature-metric Loss for Self-supervised Learning of Depth and Egomotion》论文笔记
support vector machine
程序员新人周一优化一行代码,周三被劝退?
Tensorflow, danger! Google itself is the one who abandoned it
One of the components of the program
Black Monday
leetcode-94-二叉树的中序遍历