forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/go-sql-driver/mysql/connector.go

248 lines
3.6 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Copyright 2018 The Go-MySQL-Driver Authors. All rights reserved.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// This Source Code Form is subject to the terms of the Mozilla Public
2024-02-18 10:42:21 +00:00
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
2024-02-18 10:42:21 +00:00
// You can obtain one at http://mozilla.org/MPL/2.0/.
package mysql
import (
"context"
"database/sql/driver"
"net"
)
type connector struct {
cfg *Config // immutable private copy.
2024-02-18 10:42:21 +00:00
}
// Connect implements driver.Connector interface.
2024-02-18 10:42:21 +00:00
// Connect returns a connection to the database.
2024-02-18 10:42:21 +00:00
func (c *connector) Connect(ctx context.Context) (driver.Conn, error) {
2024-02-18 10:42:21 +00:00
var err error
// New mysqlConn
2024-02-18 10:42:21 +00:00
mc := &mysqlConn{
2024-02-18 10:42:21 +00:00
maxAllowedPacket: maxPacketSize,
maxWriteSize: maxPacketSize - 1,
closech: make(chan struct{}),
cfg: c.cfg,
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
mc.parseTime = mc.cfg.ParseTime
// Connect to Server
2024-02-18 10:42:21 +00:00
dialsLock.RLock()
2024-02-18 10:42:21 +00:00
dial, ok := dials[mc.cfg.Net]
2024-02-18 10:42:21 +00:00
dialsLock.RUnlock()
2024-02-18 10:42:21 +00:00
if ok {
2024-02-18 10:42:21 +00:00
dctx := ctx
2024-02-18 10:42:21 +00:00
if mc.cfg.Timeout > 0 {
2024-02-18 10:42:21 +00:00
var cancel context.CancelFunc
2024-02-18 10:42:21 +00:00
dctx, cancel = context.WithTimeout(ctx, c.cfg.Timeout)
2024-02-18 10:42:21 +00:00
defer cancel()
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
mc.netConn, err = dial(dctx, mc.cfg.Addr)
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
nd := net.Dialer{Timeout: mc.cfg.Timeout}
2024-02-18 10:42:21 +00:00
mc.netConn, err = nd.DialContext(ctx, mc.cfg.Net, mc.cfg.Addr)
2024-02-18 10:42:21 +00:00
}
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
// Enable TCP Keepalives on TCP connections
2024-02-18 10:42:21 +00:00
if tc, ok := mc.netConn.(*net.TCPConn); ok {
2024-02-18 10:42:21 +00:00
if err := tc.SetKeepAlive(true); err != nil {
2024-02-18 10:42:21 +00:00
// Don't send COM_QUIT before handshake.
2024-02-18 10:42:21 +00:00
mc.netConn.Close()
2024-02-18 10:42:21 +00:00
mc.netConn = nil
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// Call startWatcher for context support (From Go 1.8)
2024-02-18 10:42:21 +00:00
mc.startWatcher()
2024-02-18 10:42:21 +00:00
if err := mc.watchCancel(ctx); err != nil {
2024-02-18 10:42:21 +00:00
mc.cleanup()
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
defer mc.finish()
mc.buf = newBuffer(mc.netConn)
// Set I/O timeouts
2024-02-18 10:42:21 +00:00
mc.buf.timeout = mc.cfg.ReadTimeout
2024-02-18 10:42:21 +00:00
mc.writeTimeout = mc.cfg.WriteTimeout
// Reading Handshake Initialization Packet
2024-02-18 10:42:21 +00:00
authData, plugin, err := mc.readHandshakePacket()
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
mc.cleanup()
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
if plugin == "" {
2024-02-18 10:42:21 +00:00
plugin = defaultAuthPlugin
2024-02-18 10:42:21 +00:00
}
// Send Client Authentication Packet
2024-02-18 10:42:21 +00:00
authResp, err := mc.auth(authData, plugin)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
// try the default auth plugin, if using the requested plugin failed
2024-02-18 10:42:21 +00:00
errLog.Print("could not use requested auth plugin '"+plugin+"': ", err.Error())
2024-02-18 10:42:21 +00:00
plugin = defaultAuthPlugin
2024-02-18 10:42:21 +00:00
authResp, err = mc.auth(authData, plugin)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
mc.cleanup()
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if err = mc.writeHandshakeResponsePacket(authResp, plugin); err != nil {
2024-02-18 10:42:21 +00:00
mc.cleanup()
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
// Handle response to auth packet, switch methods if possible
2024-02-18 10:42:21 +00:00
if err = mc.handleAuthResult(authData, plugin); err != nil {
2024-02-18 10:42:21 +00:00
// Authentication failed and MySQL has already closed the connection
2024-02-18 10:42:21 +00:00
// (https://dev.mysql.com/doc/internals/en/authentication-fails.html).
2024-02-18 10:42:21 +00:00
// Do not send COM_QUIT, just cleanup and return the error.
2024-02-18 10:42:21 +00:00
mc.cleanup()
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
if mc.cfg.MaxAllowedPacket > 0 {
2024-02-18 10:42:21 +00:00
mc.maxAllowedPacket = mc.cfg.MaxAllowedPacket
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
// Get max allowed packet size
2024-02-18 10:42:21 +00:00
maxap, err := mc.getSystemVar("max_allowed_packet")
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
mc.Close()
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
mc.maxAllowedPacket = stringToInt(maxap) - 1
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if mc.maxAllowedPacket < maxPacketSize {
2024-02-18 10:42:21 +00:00
mc.maxWriteSize = mc.maxAllowedPacket
2024-02-18 10:42:21 +00:00
}
// Handle DSN Params
2024-02-18 10:42:21 +00:00
err = mc.handleParams()
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
mc.Close()
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
return mc, nil
2024-02-18 10:42:21 +00:00
}
// Driver implements driver.Connector interface.
2024-02-18 10:42:21 +00:00
// Driver returns &MySQLDriver{}.
2024-02-18 10:42:21 +00:00
func (c *connector) Driver() driver.Driver {
2024-02-18 10:42:21 +00:00
return &MySQLDriver{}
2024-02-18 10:42:21 +00:00
}