connector.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. package mysql
  9. import (
  10. "context"
  11. "database/sql/driver"
  12. "net"
  13. )
  14. type connector struct {
  15. cfg *Config // immutable private copy.
  16. }
  17. // Connect implements driver.Connector interface.
  18. // Connect returns a connection to the database.
  19. func (c *connector) Connect(ctx context.Context) (driver.Conn, error) {
  20. var err error
  21. // New mysqlConn
  22. mc := &mysqlConn{
  23. maxAllowedPacket: maxPacketSize,
  24. maxWriteSize: maxPacketSize - 1,
  25. closech: make(chan struct{}),
  26. cfg: c.cfg,
  27. }
  28. mc.parseTime = mc.cfg.ParseTime
  29. // Connect to Server
  30. dialsLock.RLock()
  31. dial, ok := dials[mc.cfg.Net]
  32. dialsLock.RUnlock()
  33. if ok {
  34. mc.netConn, err = dial(ctx, mc.cfg.Addr)
  35. } else {
  36. nd := net.Dialer{Timeout: mc.cfg.Timeout}
  37. mc.netConn, err = nd.DialContext(ctx, mc.cfg.Net, mc.cfg.Addr)
  38. }
  39. if err != nil {
  40. if nerr, ok := err.(net.Error); ok && nerr.Temporary() {
  41. errLog.Print("net.Error from Dial()': ", nerr.Error())
  42. return nil, driver.ErrBadConn
  43. }
  44. return nil, err
  45. }
  46. // Enable TCP Keepalives on TCP connections
  47. if tc, ok := mc.netConn.(*net.TCPConn); ok {
  48. if err := tc.SetKeepAlive(true); err != nil {
  49. // Don't send COM_QUIT before handshake.
  50. mc.netConn.Close()
  51. mc.netConn = nil
  52. return nil, err
  53. }
  54. }
  55. // Call startWatcher for context support (From Go 1.8)
  56. mc.startWatcher()
  57. if err := mc.watchCancel(ctx); err != nil {
  58. return nil, err
  59. }
  60. defer mc.finish()
  61. mc.buf = newBuffer(mc.netConn)
  62. // Set I/O timeouts
  63. mc.buf.timeout = mc.cfg.ReadTimeout
  64. mc.writeTimeout = mc.cfg.WriteTimeout
  65. // Reading Handshake Initialization Packet
  66. authData, plugin, err := mc.readHandshakePacket()
  67. if err != nil {
  68. mc.cleanup()
  69. return nil, err
  70. }
  71. if plugin == "" {
  72. plugin = defaultAuthPlugin
  73. }
  74. // Send Client Authentication Packet
  75. authResp, err := mc.auth(authData, plugin)
  76. if err != nil {
  77. // try the default auth plugin, if using the requested plugin failed
  78. errLog.Print("could not use requested auth plugin '"+plugin+"': ", err.Error())
  79. plugin = defaultAuthPlugin
  80. authResp, err = mc.auth(authData, plugin)
  81. if err != nil {
  82. mc.cleanup()
  83. return nil, err
  84. }
  85. }
  86. if err = mc.writeHandshakeResponsePacket(authResp, plugin); err != nil {
  87. mc.cleanup()
  88. return nil, err
  89. }
  90. // Handle response to auth packet, switch methods if possible
  91. if err = mc.handleAuthResult(authData, plugin); err != nil {
  92. // Authentication failed and MySQL has already closed the connection
  93. // (https://dev.mysql.com/doc/internals/en/authentication-fails.html).
  94. // Do not send COM_QUIT, just cleanup and return the error.
  95. mc.cleanup()
  96. return nil, err
  97. }
  98. if mc.cfg.MaxAllowedPacket > 0 {
  99. mc.maxAllowedPacket = mc.cfg.MaxAllowedPacket
  100. } else {
  101. // Get max allowed packet size
  102. maxap, err := mc.getSystemVar("max_allowed_packet")
  103. if err != nil {
  104. mc.Close()
  105. return nil, err
  106. }
  107. mc.maxAllowedPacket = stringToInt(maxap) - 1
  108. }
  109. if mc.maxAllowedPacket < maxPacketSize {
  110. mc.maxWriteSize = mc.maxAllowedPacket
  111. }
  112. // Handle DSN Params
  113. err = mc.handleParams()
  114. if err != nil {
  115. mc.Close()
  116. return nil, err
  117. }
  118. return mc, nil
  119. }
  120. // Driver implements driver.Connector interface.
  121. // Driver returns &MySQLDriver{}.
  122. func (c *connector) Driver() driver.Driver {
  123. return &MySQLDriver{}
  124. }