Go implementation of redis connection pool

Time:2021-10-18

1、 What is connection pool and what is the use of connection pool

Let’s take a look at how others introduce connection pools:

The basic idea of connection pool is to store the database connection as an object in memory during system initialization. When users need to access the database, they do not establish a new connection, but take out an established idle connection object from the connection pool. After use, the user does not close the connection, but puts the connection back into the connection pool for access by the next request. The establishment and disconnection of connections are managed by the connection pool itself. At the same time, you can also set the parameters of the connection pool to control the initial number of connections in the connection pool, the upper and lower limits of connections, as well as the maximum usage times and maximum idle time of each connection. You can also monitor the number and usage of database connections through its own management mechanism.

Let me explain briefly below, because it takes a period of time for the redis client to connect to the redis server, and the time for processing various operations is often very short. If redis needs to be reconnected every time for various operations, it will waste a lot of time. Therefore, redis introduces the connection pool, which can establish multiple client connections without releasing, avoid wasting IO resources, and put them in the connection pool when not in use, so as to reduce the time required to connect to the database and improve efficiency.
Connection pool is to build a pool and a certain amount of pipes. Each time the pipeline is exhausted, the IO resources cannot be consumed, which ensures that the IO resources will not be exhausted.

2、 Code display

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
    "fmt"
    "github.com/garyburd/redigo/redis"
    "strconv"
    "time"
)
func main() {
    pool := &redis.Pool{
        // Maximum number of connections allocated by the pool at a given time.
        // When zero, there is no limit on the number of connections in the pool.
        //Maximum number of active connections, 0 for infinite
        MaxActive: 888,
        //Maximum idle connections
        // Maximum number of idle connections in the pool.
        MaxIdle: 20,
        //Timeout for idle connections
        // Close connections after remaining idle for this duration. If the value
        // is zero, then idle connections are not closed. Applications should set
        // the timeout to a value less than the server's timeout.
        IdleTimeout: time.Second * 100,
        //Defines the function to dial up a connection
        // Dial is an application supplied function for creating and configuring a
        // connection.
        //
        // The connection returned from Dial must not be in a special state
        // (subscribed to pubsub channel, transaction started, ...).
        Dial: func() (redis.Conn, error) {
            return redis.Dial("tcp","127.0.0.1:6379"),
        }
    }
    //Delay closing connection pool
    defer pool.Close()
    //IO concurrent connection
    for i:=0;i<10;i++{
        go getConnFromPoolAndHappy(pool,i)
    }
    //Keep the main process alive
    time.Sleep(3*time.Second)
}
func getConnFromPoolAndHappy(pool *redis.Pool, i int)  {
    //Get connection through connection pool
    conn := pool.Get()
    //Delay closing connection
    defer conn.Close()
    //Use connection operation data
    reply, err := conn.Do("set", "conn"+strconv.Itoa(i), i)
    s, _ := redis.String(reply, err)
    fmt.Println(s)
}

MaxActive: 888

The above code indicates the maximum number of connections allocated by the connection pool within a given time. When the value is 0, it indicates that there is no upper limit on the number of connections in the connection pool.

MaxIdle: 20

Indicates the maximum number of idle connections in the connection pool.

IdleTimeout: time.Second * 100

This value means that if the connection of the connection pool is idle, the connection will be closed. If the value is zero, idle connections in the connection pool will not be closed. The application should set this limit timeout to not exceed the limit timeout of the server.

?
1
2
3
Dial: func() (redis.Conn, error) {
   return redis.Dial("tcp", "127.0.0.1:6379")
}

This value is a function that should be used to create and configure connections. The connection returned from dial cannot be in a special state.
Open a concurrent procedure to execute the function. Because the range of the for loop is 0-9, the concurrency number is 10, indicating that 10 people go to the connection pool to get the pipeline at the same time.

?
1
go getConnFromPoolAndHappy(pool,i)

The following code is used to keep the main process alive.

?
1
time.Sleep(3*time.Second)

The following shows the connection obtained through the connection pool. That is, take a pipe from the pool.

?
1
conn := pool.Get()

Then the following code uses the pipeline.

?
1
2
3
reply, err := conn.Do("set", "conn"+strconv.Itoa(i), i)
 s, _ := redis.String(reply, err)
 fmt.Println(s)

If a person occupies the pipeline, it will be idle. If you stay idle for too long and reach the timeout of idle connection, you will be asked to leave.
Because the pool uses the same, you need to use the pointer to the pool.

?
1
pool := &redis.Pool

This is the end of this article about the method of go redis connection pool. For more information about go redis connection pool, please search the previous articles of developeppaper or continue to browse the relevant articles below. I hope you will support developeppaper in the future!

Recommended Today

Swift advanced (XV) extension

The extension in swift is somewhat similar to the category in OC Extension can beenumeration、structural morphology、class、agreementAdd new features□ you can add methods, calculation attributes, subscripts, (convenient) initializers, nested types, protocols, etc What extensions can’t do:□ original functions cannot be overwritten□ you cannot add storage attributes or add attribute observers to existing attributes□ cannot add parent […]