Skip to content

Implement driver.Pinger #565

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions connection_go18.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// +build go1.8

package mysql

import (
"context"
)

func (mc *mysqlConn) Ping(ctx context.Context) error {
return mc.writeCommandPacket(comPing)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://dev.mysql.com/doc/internals/en/com-ping.html

You should read OK packet after send COM_PING.
Additionally, cancellation should be implemented too.
When Ping is cancelled between PING and OK, the connection should be disposed.

}
17 changes: 17 additions & 0 deletions driver_go18_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
package mysql

import (
"context"
"database/sql"
"database/sql/driver"
"fmt"
"reflect"
"testing"
Expand Down Expand Up @@ -188,3 +190,18 @@ func TestSkipResults(t *testing.T) {
}
})
}

func TestPing(t *testing.T) {
runTests(t, dsn, func(dbt *DBTest) {
mysqlDriver := dbt.db.Driver().(driver.Driver)
conn, err := mysqlDriver.Open(dsn)
if err != nil {
dbt.Fatalf("error opening conn: %s", err)
}
pinger := conn.(driver.Pinger)
err = pinger.Ping(context.Background())
if err != nil {
dbt.Fatalf("error on ping: %s", err)
}
})
}