Skip to content
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

Implement driver.Pinger #565

Closed
wants to merge 3 commits into from
Closed

Implement driver.Pinger #565

wants to merge 3 commits into from

Conversation

edsrzf
Copy link

@edsrzf edsrzf commented Apr 2, 2017

The Pinger interface was added in Go 1.8.

This implementation ignores the context parameter to Ping because wiring it in
would be a much larger change.

Fixes #505.

The Pinger interface was added in Go 1.8.

This implementation ignores the context parameter to Ping.
@caibirdme
Copy link

But it seems useless and it may puzzle people who don't know that the ctx isn't used actually

@edsrzf
Copy link
Author

edsrzf commented Apr 3, 2017

I don't think it's useless: there are cases where a connection to MySQL can be established, but it's not actually ready to start processing commands yet. I agree it's not ideal, but the work in #551 seems to have stalled. Probably because the pull request is so large.

This seems like a marginal improvement on what's there now, and it can be built upon. Let's start small.

)

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.

@edsrzf
Copy link
Author

edsrzf commented Apr 4, 2017

Updated. Thanks for the review.

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.

@edsrzf
Copy link
Author

edsrzf commented Apr 7, 2017

Updated again! Hopefully for the final time? 😀

go func() {
_, err := mc.readResultOK()
ch <- err
}()
Copy link
Member

Choose a reason for hiding this comment

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

select {
case <-ctx.Done():
mc.netConn.SetReadDeadline(time.Now())
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.

@methane
Copy link
Member

methane commented Apr 7, 2017

I think mc.writeCommandPacket() and mc.readResultOK() should support context,
instead implementing cancellation in mc.Ping().

Please stop updating this pull request.
I need more time to think about internal design. Implementing context support is much harder
than people think.

@edsrzf
Copy link
Author

edsrzf commented May 8, 2017

Seems clear that this is not a path forward. Closing.

@edsrzf edsrzf closed this May 8, 2017
@edsrzf edsrzf deleted the ping branch May 8, 2017 02:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants