• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

go-sql-driver / mysql / 4698632915

pending completion
4698632915

push

github

GitHub
Increase default maxAllowedPacket size. (#1411)

2930 of 3584 relevant lines covered (81.75%)

1713103.53 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

74.76
/connector.go
1
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
2
//
3
// Copyright 2018 The Go-MySQL-Driver Authors. All rights reserved.
4
//
5
// This Source Code Form is subject to the terms of the Mozilla Public
6
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
7
// You can obtain one at http://mozilla.org/MPL/2.0/.
8

9
package mysql
10

11
import (
12
        "context"
13
        "database/sql/driver"
14
        "net"
15
)
16

17
type connector struct {
18
        cfg *Config // immutable private copy.
19
}
20

21
// Connect implements driver.Connector interface.
22
// Connect returns a connection to the database.
23
func (c *connector) Connect(ctx context.Context) (driver.Conn, error) {
12,407✔
24
        var err error
12,407✔
25

12,407✔
26
        // New mysqlConn
12,407✔
27
        mc := &mysqlConn{
12,407✔
28
                maxAllowedPacket: maxPacketSize,
12,407✔
29
                maxWriteSize:     maxPacketSize - 1,
12,407✔
30
                closech:          make(chan struct{}),
12,407✔
31
                cfg:              c.cfg,
12,407✔
32
        }
12,407✔
33
        mc.parseTime = mc.cfg.ParseTime
12,407✔
34

12,407✔
35
        // Connect to Server
12,407✔
36
        dialsLock.RLock()
12,407✔
37
        dial, ok := dials[mc.cfg.Net]
12,407✔
38
        dialsLock.RUnlock()
12,407✔
39
        if ok {
12,624✔
40
                dctx := ctx
217✔
41
                if mc.cfg.Timeout > 0 {
372✔
42
                        var cancel context.CancelFunc
155✔
43
                        dctx, cancel = context.WithTimeout(ctx, c.cfg.Timeout)
155✔
44
                        defer cancel()
155✔
45
                }
155✔
46
                mc.netConn, err = dial(dctx, mc.cfg.Addr)
217✔
47
        } else {
12,190✔
48
                nd := net.Dialer{Timeout: mc.cfg.Timeout}
12,190✔
49
                mc.netConn, err = nd.DialContext(ctx, mc.cfg.Net, mc.cfg.Addr)
12,190✔
50
        }
12,190✔
51

52
        if err != nil {
12,555✔
53
                return nil, err
148✔
54
        }
148✔
55

56
        // Enable TCP Keepalives on TCP connections
57
        if tc, ok := mc.netConn.(*net.TCPConn); ok {
24,387✔
58
                if err := tc.SetKeepAlive(true); err != nil {
12,128✔
59
                        // Don't send COM_QUIT before handshake.
×
60
                        mc.netConn.Close()
×
61
                        mc.netConn = nil
×
62
                        return nil, err
×
63
                }
×
64
        }
65

66
        // Call startWatcher for context support (From Go 1.8)
67
        mc.startWatcher()
12,259✔
68
        if err := mc.watchCancel(ctx); err != nil {
12,290✔
69
                mc.cleanup()
31✔
70
                return nil, err
31✔
71
        }
31✔
72
        defer mc.finish()
12,228✔
73

12,228✔
74
        mc.buf = newBuffer(mc.netConn)
12,228✔
75

12,228✔
76
        // Set I/O timeouts
12,228✔
77
        mc.buf.timeout = mc.cfg.ReadTimeout
12,228✔
78
        mc.writeTimeout = mc.cfg.WriteTimeout
12,228✔
79

12,228✔
80
        // Reading Handshake Initialization Packet
12,228✔
81
        authData, plugin, err := mc.readHandshakePacket()
12,228✔
82
        if err != nil {
12,323✔
83
                mc.cleanup()
95✔
84
                return nil, err
95✔
85
        }
95✔
86

87
        if plugin == "" {
12,133✔
88
                plugin = defaultAuthPlugin
×
89
        }
×
90

91
        // Send Client Authentication Packet
92
        authResp, err := mc.auth(authData, plugin)
12,133✔
93
        if err != nil {
12,133✔
94
                // try the default auth plugin, if using the requested plugin failed
×
95
                errLog.Print("could not use requested auth plugin '"+plugin+"': ", err.Error())
×
96
                plugin = defaultAuthPlugin
×
97
                authResp, err = mc.auth(authData, plugin)
×
98
                if err != nil {
×
99
                        mc.cleanup()
×
100
                        return nil, err
×
101
                }
×
102
        }
103
        if err = mc.writeHandshakeResponsePacket(authResp, plugin); err != nil {
12,133✔
104
                mc.cleanup()
×
105
                return nil, err
×
106
        }
×
107

108
        // Handle response to auth packet, switch methods if possible
109
        if err = mc.handleAuthResult(authData, plugin); err != nil {
12,233✔
110
                // Authentication failed and MySQL has already closed the connection
100✔
111
                // (https://dev.mysql.com/doc/internals/en/authentication-fails.html).
100✔
112
                // Do not send COM_QUIT, just cleanup and return the error.
100✔
113
                mc.cleanup()
100✔
114
                return nil, err
100✔
115
        }
100✔
116

117
        if mc.cfg.MaxAllowedPacket > 0 {
23,973✔
118
                mc.maxAllowedPacket = mc.cfg.MaxAllowedPacket
11,940✔
119
        } else {
12,033✔
120
                // Get max allowed packet size
93✔
121
                maxap, err := mc.getSystemVar("max_allowed_packet")
93✔
122
                if err != nil {
93✔
123
                        mc.Close()
×
124
                        return nil, err
×
125
                }
×
126
                mc.maxAllowedPacket = stringToInt(maxap) - 1
93✔
127
        }
128
        if mc.maxAllowedPacket < maxPacketSize {
12,033✔
129
                mc.maxWriteSize = mc.maxAllowedPacket
×
130
        }
×
131

132
        // Handle DSN Params
133
        err = mc.handleParams()
12,033✔
134
        if err != nil {
12,250✔
135
                mc.Close()
217✔
136
                return nil, err
217✔
137
        }
217✔
138

139
        return mc, nil
11,816✔
140
}
141

142
// Driver implements driver.Connector interface.
143
// Driver returns &MySQLDriver{}.
144
func (c *connector) Driver() driver.Driver {
×
145
        return &MySQLDriver{}
×
146
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc