Skip to content

Commit

Permalink
Remove obsolete "keepalive" feature
Browse files Browse the repository at this point in the history
  • Loading branch information
julienschmidt committed Feb 25, 2013
1 parent cec3390 commit 9d66799
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 130 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Note: `go get` doesn't install the `master` branch, but the tag `go1`, which bui
* Automatic Connection-Pooling *(by database/sql package)*

## Requirements
* Go 1 or higher (Go 1.0.3 or higher recommended)
* Go 1.0.3 or higher
* MySQL (Version 4.1 or higher), MariaDB or Percona Server

---------------------------------------
Expand Down Expand Up @@ -104,9 +104,8 @@ For Unix-sockets the address is the absolute path to the MySQL-Server-socket, e.

Possible Parameters are:
* `charset`: *"SET NAMES `value`"*. If multiple charsets are set (seperated by a comma), the following charset is used if setting the charset failes. This enables support for `utf8mb4` ([introduced in MySQL 5.5.3](http://dev.mysql.com/doc/refman/5.5/en/charset-unicode-utf8mb4.html)) with fallback to `utf8` for older servers.
* _(deprecated)_ <s>`keepalive`: If `value` equals 1, the keepalive-time is set to [wait_timeout](https://dev.mysql.com/doc/refman/5.5/en/server-system-variables.html#sysvar_wait_timeout)-60, which pings the Server 60 seconds before the MySQL server would close the connection to avoid timeout. If the value is greater than 1, the server gets pinged every `value` seconds without a command. System variables are executed **before**, so it may be possible to change the *wait_timeout* value.</s> **With Go 1.0.3 this is not necessary anymore. Now closed connections can be automatically detected and handled.**
* _(pending)_ <s>`tls`</s>: will enable SSL/TLS-Encryption
* _(pending)_ <s>`compress`</s>: will enable Compression
* _(pending)_ <s>`tls`</s>: will enable SSL/TLS-Encryption
* _(pending)_ <s>`compress`</s>: will enable Compression

All other parameters are interpreted as system variables:
* `time_zone`: *"SET time_zone='`value`'"*
Expand Down Expand Up @@ -152,6 +151,6 @@ That means:
* You **needn't publish** the source code of your library as long the files licensed under the MPL 2.0 are **unchanged**
* You **must publish** the source code of any **changed files** licensed under the MPL 2.0 under a) the MPL 2.0 itself or b) a compatible license (e.g. GPL 3.0 or Apache License 2.0)

Please read the [MPL 2.0 FAQ](http://www.mozilla.org/MPL/2.0/FAQ.html) if you have further questions regarding the license.
Please read the [MPL 2.0 FAQ](http://www.mozilla.org/MPL/2.0/FAQ.html) if you have further questions regarding the license.

You can read the full terms here: [LICENSE](https://raw.github.com/Go-SQL-Driver/MySQL/master/LICENSE)
127 changes: 8 additions & 119 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,19 @@ package mysql
import (
"bufio"
"database/sql/driver"
"errors"
"net"
"strconv"
"strings"
"time"
)

type mysqlConn struct {
cfg *config
server *serverSettings
netConn net.Conn
bufReader *bufio.Reader
protocol uint8
sequence uint8
affectedRows uint64
insertId uint64
lastCmdTime time.Time
keepaliveTimer *time.Timer
cfg *config
server *serverSettings
netConn net.Conn
bufReader *bufio.Reader
protocol uint8
sequence uint8
affectedRows uint64
insertId uint64
}

type config struct {
Expand All @@ -48,7 +43,6 @@ type serverSettings struct {
charset uint8
scrambleBuff []byte
threadID uint32
keepalive int64
}

// Handles parameters set in DSN
Expand Down Expand Up @@ -76,10 +70,6 @@ func (mc *mysqlConn) handleParams() (e error) {
case "compress":
dbgLog.Print("Compression not implemented yet")

// We don't want to set keepalive as system var
case "keepalive":
continue

// System Vars
default:
e = mc.exec("SET " + param + "=" + val + "")
Expand All @@ -89,52 +79,6 @@ func (mc *mysqlConn) handleParams() (e error) {
}
}

// KeepAlive
if val, param := mc.cfg.params["keepalive"]; param {
mc.server.keepalive, e = strconv.ParseInt(val, 10, 64)
if e != nil {
return errors.New("Invalid keepalive time")
}

// Get keepalive time by MySQL system var wait_timeout
if mc.server.keepalive == 1 {
val, e = mc.getSystemVar("wait_timeout")
mc.server.keepalive, e = strconv.ParseInt(val, 10, 64)
if e != nil {
return errors.New("Error getting wait_timeout")
}

// Trigger 1min BEFORE wait_timeout
if mc.server.keepalive > 60 {
mc.server.keepalive -= 60
}
}

if mc.server.keepalive > 0 {
mc.lastCmdTime = time.Now()

// Ping-Timer to avoid timeout
mc.keepaliveTimer = time.AfterFunc(
time.Duration(mc.server.keepalive)*time.Second, func() {
var diff time.Duration
for {
// Fires only if diff > keepalive. Makes it collision safe
for mc.netConn != nil &&
mc.lastCmdTime.Unix()+mc.server.keepalive > time.Now().Unix() {
diff = mc.lastCmdTime.Sub(time.Unix(time.Now().Unix()-mc.server.keepalive, 0))
time.Sleep(diff)
}
if mc.netConn != nil {
if e := mc.Ping(); e != nil {
break
}
} else {
return
}
}
})
}
}
return
}

Expand All @@ -148,9 +92,6 @@ func (mc *mysqlConn) Begin() (driver.Tx, error) {
}

func (mc *mysqlConn) Close() (e error) {
if mc.server.keepalive > 0 {
mc.keepaliveTimer.Stop()
}
mc.writeCommandPacket(COM_QUIT)
mc.bufReader = nil
mc.netConn.Close()
Expand Down Expand Up @@ -271,55 +212,3 @@ func (mc *mysqlConn) Query(query string, args []driver.Value) (driver.Rows, erro

return rows, e
}

// Gets the value of the given MySQL System Variable
func (mc *mysqlConn) getSystemVar(name string) (val string, e error) {
// Send command
e = mc.writeCommandPacket(COM_QUERY, "SELECT @@"+name)
if e != nil {
return
}

// Read Result
resLen, e := mc.readResultSetHeaderPacket()
if e != nil {
return
}

if resLen > 0 {
var n uint64
n, e = mc.readUntilEOF()
if e != nil {
return
}

var row *[]*[]byte
row, e = mc.readRow(int(n))
if e != nil {
return
}

_, e = mc.readUntilEOF()
if e != nil {
return
}

val = string(*(*row)[0])
}

return
}

// *** DEPRECATED ***
// Executes a simple Ping-CMD to test or keepalive the connection
func (mc *mysqlConn) Ping() (e error) {
// Send command
e = mc.writeCommandPacket(COM_PING)
if e != nil {
return
}

// Read Result
e = mc.readResultOK()
return
}
7 changes: 1 addition & 6 deletions packets.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,13 @@ func (mc *mysqlConn) readNumber(nr uint8) (uint64, error) {
}

func (mc *mysqlConn) writePacket(data *[]byte) error {
// Set time BEFORE to avoid possible collisions
if mc.server.keepalive > 0 {
mc.lastCmdTime = time.Now()
}

// Write packet
n, e := mc.netConn.Write(*data)
if e != nil || n != len(*data) {
if e == nil {
e = errors.New("Length of send data does not match packet length")
}
errLog.Print(`packets:104 `, e)
errLog.Print(`packets:103 `, e)
return driver.ErrBadConn
}

Expand Down

0 comments on commit 9d66799

Please sign in to comment.