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 2 commits
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
24 changes: 24 additions & 0 deletions connection_go18.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// +build go1.8

package mysql

import (
"context"
)

func (mc *mysqlConn) Ping(ctx context.Context) error {
err := mc.writeCommandPacket(comPing)
if err != nil {
return err
}

select {
case <-ctx.Done():
mc.Close()
return ctx.Err()
Copy link
Member

Choose a reason for hiding this comment

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

This is dangerous.

  • mc is not thread safe.
  • mc.readResultOK() is still working.
  • After Ping return, database/sql.DB will use this connection again. race may be happen.

default:
}

_, err = mc.readResultOK()
Copy link
Member

Choose a reason for hiding this comment

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

This code didn't cancel waiting OK package when ctx is canceled.

Copy link
Author

Choose a reason for hiding this comment

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

I'm not sure what you mean. Are you saying we should still read the OK result after the Done channel is closed? If so, what's the point if we're closing the connection anyway? And if not, please explain more.

Copy link
Member

Choose a reason for hiding this comment

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

No. readResultOK() waits OK packet from MySQL server. It may take long time.
While waiting OK packet, context can be cancelled. Then, driver should abandon waiting OK.

Copy link
Author

@edsrzf edsrzf Apr 6, 2017

Choose a reason for hiding this comment

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

So are you looking for something like this, then?

ch := make(chan err)
go func() {
    _, err = mc.readResultOK()
   ch <- err
}()
select {
case <-ctx.Done():
    mc.Close()
    return ctx.Err()
case err := <- ch:
    return err
}

Copy link
Member

Choose a reason for hiding this comment

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

Yes

Copy link
Member

Choose a reason for hiding this comment

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

return err
}
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)
}
})
}