connector.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. dctx := ctx
  35. if mc.cfg.Timeout > 0 {
  36. var cancel context.CancelFunc
  37. dctx, cancel = context.WithTimeout(ctx, c.cfg.Timeout)
  38. defer cancel()
  39. }
  40. mc.netConn, err = dial(dctx, mc.cfg.Addr)
  41. } else {
  42. nd := net.Dialer{Timeout: mc.cfg.Timeout}
  43. mc.netConn, err = nd.DialContext(ctx, mc.cfg.Net, mc.cfg.Addr)
  44. }
  45. if err != nil {
  46. return nil, err
  47. }
  48. // Enable TCP Keepalives on TCP connections
  49. if tc, ok := mc.netConn.(*net.TCPConn); ok {
  50. if err := tc.SetKeepAlive(true); err != nil {
  51. // Don't send COM_QUIT before handshake.
  52. mc.netConn.Close()
  53. mc.netConn = nil
  54. return nil, err
  55. }
  56. }
  57. // Call startWatcher for context support (From Go 1.8)
  58. mc.startWatcher()
  59. if err := mc.watchCancel(ctx); err != nil {
  60. mc.cleanup()
  61. return nil, err
  62. }
  63. defer mc.finish()
  64. mc.buf = newBuffer(mc.netConn)
  65. // Set I/O timeouts
  66. mc.buf.timeout = mc.cfg.ReadTimeout
  67. mc.writeTimeout = mc.cfg.WriteTimeout
  68. // Reading Handshake Initialization Packet
  69. authData, plugin, err := mc.readHandshakePacket()
  70. if err != nil {
  71. mc.cleanup()
  72. return nil, err
  73. }
  74. if plugin == "" {
  75. plugin = defaultAuthPlugin
  76. }
  77. // Send Client Authentication Packet
  78. authResp, err := mc.auth(authData, plugin)
  79. if err != nil {
  80. // try the default auth plugin, if using the requested plugin failed
  81. errLog.Print("could not use requested auth plugin '"+plugin+"': ", err.Error())
  82. plugin = defaultAuthPlugin
  83. authResp, err = mc.auth(authData, plugin)
  84. if err != nil {
  85. mc.cleanup()
  86. return nil, err
  87. }
  88. }
  89. if err = mc.writeHandshakeResponsePacket(authResp, plugin); err != nil {
  90. mc.cleanup()
  91. return nil, err
  92. }
  93. // Handle response to auth packet, switch methods if possible
  94. if err = mc.handleAuthResult(authData, plugin); err != nil {
  95. // Authentication failed and MySQL has already closed the connection
  96. // (https://dev.mysql.com/doc/internals/en/authentication-fails.html).
  97. // Do not send COM_QUIT, just cleanup and return the error.
  98. mc.cleanup()
  99. return nil, err
  100. }
  101. if mc.cfg.MaxAllowedPacket > 0 {
  102. mc.maxAllowedPacket = mc.cfg.MaxAllowedPacket
  103. } else {
  104. // Get max allowed packet size
  105. maxap, err := mc.getSystemVar("max_allowed_packet")
  106. if err != nil {
  107. mc.Close()
  108. return nil, err
  109. }
  110. mc.maxAllowedPacket = stringToInt(maxap) - 1
  111. }
  112. if mc.maxAllowedPacket < maxPacketSize {
  113. mc.maxWriteSize = mc.maxAllowedPacket
  114. }
  115. // Handle DSN Params
  116. err = mc.handleParams()
  117. if err != nil {
  118. mc.Close()
  119. return nil, err
  120. }
  121. return mc, nil
  122. }
  123. // Driver implements driver.Connector interface.
  124. // Driver returns &MySQLDriver{}.
  125. func (c *connector) Driver() driver.Driver {
  126. return &MySQLDriver{}
  127. }