In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the function of golang mysql library connection pool". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "what is the role of golang mysql library connection pool"!
Golang mysql Library connection Pool Analysis of 0x1 background
Golang's protocol is easy to use, but sometimes the bottleneck lies not in the language, but in the following data sources, such as our common mysql,redis, etc. When a back-end service has many requests, the language can hold, but mysql produces errors, such as too many connection, too many time_wait and so on. Today we will analyze how to solve this problem.
0x2 code example
Please check main.go and halokid (if you can help, please start or follow. Oh, thank you)
0x3 analysis
Only execute the ini function, and the process of checking the mysql is shown as (the original mysql is not being processed by a process)
Before the execution
Mysql > show processlist +-- +-- +-+ | Id | User | Host | db | Command | Time | State | Info | +-+-- -- +-+ | 4 | event_scheduler | localhost | NULL | Daemon | 2304 | Waiting on empty queue | NULL | | 9 | root | 10.244.1.1 root 64000 | test | Sleep | 1315 | | NULL | 10 | root | 10.244.1.1 NULL 64022 | test | Query | 0 | starting | show processlist | + -+ 3 rows in set (0.01 sec)
After implementation
Mysql > show processlist +-- +-- +-+ | Id | User | Host | db | Command | Time | State | Info | +-+-- -- +-+ | 4 | event_scheduler | localhost | NULL | Daemon | 2284 | Waiting on empty queue | NULL | | 9 | root | 10.244.1.1 root 64000 | test | Sleep | 1295 | | NULL | 10 | root | 10.244.1.1 NULL 64022 | test | Query | 0 | starting | show processlist | | 13 | root | 10.244.1.1 show processlist 52134 | test | Sleep | 20 | | NULL | + -+ 4 rows in set (0.00 sec)
It can be seen that after the execution of db.Ping (), process has one more Sleep connection, that is, a connection into the connection pool.
Running
Db.SetMaxOpenConns (10) db.SetMaxIdleConns (5)
After two sentences, the connection pool has not changed, so it can be seen that the above logic takes effect only when the database processing logic is actually executed.
Execute cooperative process query
Mysql > show processlist +-- +-- +-+ | Id | User | Host | db | Command | Time | State | Info | +-+-- -- +-+ | 4 | event_scheduler | localhost | NULL | Daemon | 4397 | Waiting on empty queue | NULL | | 9 | root | 10.244.1.1 root 64000 | test | Sleep | 3408 | | NULL | 10 | root | 10.244.1.1 NULL 64022 | test | Query | 0 | starting | show processlist | | 19 | root | 10.244.1.1 show processlist 54823 | test | Sleep | 952 | NULL | | 20 | root | 10.244.1.1 show processlist 54824 | test | Sleep | 1104 | NULL | 47 | root | 10 .244.1.1: 57906 | test | Sleep | 0 | NULL | | 48 | root | 10.244.1.1test 57909 | test | Sleep | 0 | | NULL | 49 | root | 10.244.1.1 | Sleep | 0 | | NULL | | 50 | root | 10.244.1.1 NULL 57907 | test | Sleep | 0 | NULL | 51 | root | 10.244.1.1 | test | Sleep | 0 | NULL | 52 | root | 10.244.1. 1test | Sleep | 0 | NULL | 54 | root | 10.244.1.1 | test | Sleep | 0 | NULL | test | Sleep | 0 | NULL | | 55 | root | 10.244.1.1 Sleep 57915 | test | Sleep | 0 | NULL | 56 | root | 10.244.1.1virtual 57914 | test | Sleep | 0 | NULL | + -- +-+ 15 rows in set (0.00 sec)
Waiting after execution.
Mysql > show processlist +-- +-- +-+ | Id | User | Host | db | Command | Time | State | Info | +-+-- -- +-+ | 4 | event_scheduler | localhost | NULL | Daemon | 3931 | Waiting on empty queue | NULL | | 9 | root | 10.244.1.1 root 64000 | test | Sleep | 2942 | | NULL | 10 | root | 10.244.1.1 NULL 64022 | test | Query | 0 | starting | show processlist | | 19 | root | 10.244.1.1 show processlist 54823 | test | Sleep | 486 | | NULL | | 20 | root | 10.244.1.1 | 54824 | test | Sleep | 638 | NULL | | 32 | root | 10 .244.1.1: 56588 | test | Sleep | 22 | NULL | | 33 | root | 10.244.1.1 NULL | test | Sleep | 22 | | NULL | 34 | root | 10.244.1.1 | test | Sleep | 22 | | | | NULL | | 35 | root | 10.244.1.1 NULL 56590 | test | Sleep | 22 | NULL | 36 | root | 10.244.1.1 | test | Sleep | 22 | | NULL | +-| -+ 10 rows in set (0.00 sec)
After the execution of the cooperation program
Mysql > show processlist +-- +-- +-+ | Id | User | Host | db | Command | Time | State | Info | +-+-- -- +-+ | 4 | event_scheduler | localhost | NULL | Daemon | 3941 | Waiting on empty queue | NULL | | 9 | root | 10.244.1.1 root 64000 | test | Sleep | 2952 | | NULL | 10 | root | 10.244.1.1 NULL 64022 | test | Query | 0 | starting | show processlist | | 19 | root | 10.244.1.1 show processlist 54823 | test | Sleep | 496 | | NULL | | 20 | root | 10.244.1.Suzhou 54824 | test | Sleep | 648 | NULL | + -+ 5 rows in set (0.00 sec)
We found that the maximum number of connections was controlled at 10, and there were still 5 connections maintained after execution.
There is a very important question here, that is, the expiration time of the connection pool.
0x4 in-depth analysis. If we set the life cycle of db.SetConnMaxLifetime (15 * time.Second) connection pool to 15 seconds, we will find that after 15 seconds, all connections in the connection pool will be broken.
Mysql > show processlist +-- +-- +-+ | Id | User | Host | db | Command | Time | State | Info | +-+-- -- +-+ | 4 | event_scheduler | localhost | NULL | Daemon | 4987 | Waiting on empty queue | NULL | | 9 | root | 10.244.1.1 root 64000 | test | Sleep | 3998 | | NULL | 10 | root | 10.244.1.1 NULL 64022 | test | Query | 0 | starting | show processlist | | 19 | root | 10.244.1.1 | test | Sleep | 1542 | | NULL | | 20 | root | 10.244.1.1 | test | Sleep | 1694 | NULL | + -+ 5 rows in set (0.00 sec)
Query the database again after 30 seconds
Time.Sleep (30 * time.Second) rows, err: = db.Query ("select name from users") fmt.Println ("err -", err) defer rows.Close () for rows.Next () {var name string rows.Scan (& name) fmt.Println ("name---", name)}
At this point, the discovery program will re-initiate a new db connection.
Summary:
The connection life cycle of the mysql server
Another request is that the connection pool life cycle setting of our program is greater than that of the mysql server. At this time, there will be a situation. If we reuse connection pool connections, there will be connection errors. There are two solutions:
You can set the life cycle time in the program to be less than the connection life cycle time of the mysql server.
Increase the reconnection (keepalive) mechanism of the program, that is, regularly send a connection package server about the second point we can spread later, generally, if allowed, in the first way.
Mysql > show variables like 'mysqlx_wait_timeout' +-+-+ | Variable_name | Value | +-+-+ | mysqlx_wait_timeout | 28800 | +-+-+ 1 row in set (0.00 sec) so far I believe that everyone on the "golang mysql library connection pool what is the role of" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.