-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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() | ||
default: | ||
} | ||
|
||
_, err = mc.readResultOK() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code didn't cancel waiting OK package when ctx is canceled. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI, this is how to interrupt |
||
return err | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is dangerous.
mc.readResultOK()
is still working.Ping
return,database/sql.DB
will use this connection again. race may be happen.