golang in mysql establish a connection timeout timeout test

This paper test the connection timeout mysql.

Here the "connection" is meant to establish the connection.

Mysql connection timeout is set by the timeout parameter.

1. Establish a connection timeout test

In the following example, provided for the connection time 5s.
MySQL server IP is 192.168.0.101, port 3306.

3s performed once per SQL.

// simple.go

package main

import (
        "database/sql"
        "log"
        "time"

        _ "github.com/go-sql-driver/mysql"
)

var DB *sql.DB
var dataBase = "root:Aa123456@tcp(192.168.0.101:3306)/?timeout=5s&readTimeout=6s"

func mysqlInit() {
        var err error
        DB, err = sql.Open("mysql", dataBase)
        if err != nil {
                log.Fatalln("open db fail:", err)
        }

        DB.SetMaxOpenConns(3)
        DB.SetMaxIdleConns(3)
}

func main() {
        mysqlInit()

        for {
                log.Println("start")
                execSql()
                time.Sleep(3*time.Second)
        }
}

func execSql() {
        var value int
        err := DB.QueryRow("select 1").Scan(&value)
        if err != nil {
                log.Println("query failed:", err)
                return
        }

        log.Println("value:", value)
}

starting program:

go run simple.go

After then use iptables to send all packets to MySQL service drop off:

sudo iptables -A OUTPUT -p tcp --dport 3306 -d 192.168.0.101 -j DROP

This will trigger a retry mechanism, last time out.

output:

2019/10/27 18:34:52 start
2019/10/27 18:34:52 value: 1
2019/10/27 18:34:55 start
2019/10/27 18:34:55 value: 1
2019/10/27 18:34:58 start
2019/10/27 18:34:58 value: 1
2019/10/27 18:35:01 start
[mysql] 2019/10/27 18:35:07 packets.go:36: read tcp 192.168.0.104:54462->192.168.0.101:3306: i/o timeout
2019/10/27 18:35:07 query failed: invalid connection
2019/10/27 18:35:10 start
[mysql] 2019/10/27 18:35:15 driver.go:81: net.Error from Dial()': dial tcp 192.168.0.101:3306: i/o timeout
[mysql] 2019/10/27 18:35:20 driver.go:81: net.Error from Dial()': dial tcp 192.168.0.101:3306: i/o timeout
2019/10/27 18:35:20 query failed: driver: bad connection
2019/10/27 18:35:23 start
[mysql] 2019/10/27 18:35:28 driver.go:81: net.Error from Dial()': dial tcp 192.168.0.101:3306: i/o timeout
[mysql] 2019/10/27 18:35:33 driver.go:81: net.Error from Dial()': dial tcp 192.168.0.101:3306: i/o timeout
[mysql] 2019/10/27 18:35:38 driver.go:81: net.Error from Dial()': dial tcp 192.168.0.101:3306: i/o timeout
2019/10/27 18:35:38 query failed: driver: bad connection
2019/10/27 18:35:41 start
[mysql] 2019/10/27 18:35:46 driver.go:81: net.Error from Dial()': dial tcp 192.168.0.101:3306: i/o timeout
[mysql] 2019/10/27 18:35:51 driver.go:81: net.Error from Dial()': dial tcp 192.168.0.101:3306: i/o timeout
[mysql] 2019/10/27 18:35:56 driver.go:81: net.Error from Dial()': dial tcp 192.168.0.101:3306: i/o timeout
2019/10/27 18:35:56 query failed: driver: bad connection
2019/10/27 18:35:59 start
[mysql] 2019/10/27 18:36:04 driver.go:81: net.Error from Dial()': dial tcp 192.168.0.101:3306: i/o timeout
[mysql] 2019/10/27 18:36:09 driver.go:81: net.Error from Dial()': dial tcp 192.168.0.101:3306: i/o timeout
[mysql] 2019/10/27 18:36:14 driver.go:81: net.Error from Dial()': dial tcp 192.168.0.101:3306: i/o timeout
2019/10/27 18:36:14 query failed: driver: bad connection

You can see from the output every time you perform SQL, there are three connection error log indicates a connection, it will try three times (timeout that is set) timeout 5s.

Here it begs the question, why every time SQL execution, will be conducted three times a connection attempt it?

Check QueryRowthe underlying implementation code:

func (db *DB) QueryRow(query string, args ...interface{}) *Row {
    return db.QueryRowContext(context.Background(), query, args...)
}

func (db *DB) QueryRowContext(ctx context.Context, query string, args ...interface{}) *Row {
    rows, err := db.QueryContext(ctx, query, args...)
    return &Row{rows: rows, err: err}
}

// QueryContext executes a query that returns rows, typically a SELECT.
// The args are for any placeholder parameters in the query.
func (db *DB) QueryContext(ctx context.Context, query string, args ...interface{}) (*Rows, error) {
    var rows *Rows
    var err error
    for i := 0; i < maxBadConnRetries; i++ {
        rows, err = db.query(ctx, query, args, cachedOrNewConn)
        if err != driver.ErrBadConn {
            break
        }
    }
    if err == driver.ErrBadConn {
        return db.query(ctx, query, args, alwaysNewConn)
    }
    return rows, err
}

Wherein, maxBadConnRetries 2 is defined as:

// maxBadConnRetries is the number of maximum retries if the driver returns
// driver.ErrBadConn to signal a broken connection before forcing a new
// connection to be opened.
const maxBadConnRetries = 2

When err is driver.ErrBadConnwhen will try three times to connect. That's why just log in three times a connection error.

2.reject test

In addition, if you use iptables will all be sent to the MySQL server packets reject out:

sudo iptables -A OUTPUT -p tcp --dport 3306 -d 192.168.0.101 -j REJECT

RESET operation will be returned directly reject the connection reset, it does not trigger connection establishment timeout.

Log output:

2019/10/27 21:39:55 start
2019/10/27 21:39:55 value: 1
2019/10/27 21:39:58 start
2019/10/27 21:39:58 value: 1
2019/10/27 21:40:01 start
2019/10/27 21:40:01 value: 1
2019/10/27 21:40:04 start
2019/10/27 21:40:04 value: 1
2019/10/27 21:40:07 start
[mysql] 2019/10/27 21:40:13 packets.go:36: read tcp 192.168.0.104:54536->192.168.0.101:3306: i/o timeout
2019/10/27 21:40:13 query failed: invalid connection
2019/10/27 21:40:16 start
2019/10/27 21:40:17 query failed: dial tcp 192.168.0.101:3306: connect: connection refused
2019/10/27 21:40:20 start
2019/10/27 21:40:21 query failed: dial tcp 192.168.0.101:3306: connect: connection refused
2019/10/27 21:40:24 start
2019/10/27 21:40:25 query failed: dial tcp 192.168.0.101:3306: connect: connection refused
2019/10/27 21:40:28 start
2019/10/27 21:40:29 query failed: dial tcp 192.168.0.101:3306: connect: connection refused
2019/10/27 21:40:32 start
2019/10/27 21:40:33 query failed: dial tcp 192.168.0.101:3306: connect: connection refused

3. References

golang database sql DSN (Data Source Name)中的timeout, readTimeout

Guess you like

Origin www.cnblogs.com/lanyangsh/p/11749270.html