-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Implement Pinger interface #572
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
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f9b1e52
Add mysql.Context for the compatibility before go1.7
photon3108 63fc0b2
Compare readTimeout with Context.Deadline()
photon3108 b6c6f03
Compare writeTimeout with Context.Deadline()
photon3108 0439f3e
Make the buffer get the context from mysqlConn
photon3108 231b0fb
Support driver.Pinger
photon3108 ab45fbb
Print errDeadline only if it is not nil
photon3108 3e3200e
Add test cases for driver.Pinger
photon3108 7110720
Implement Pinger interface (#505)
photon3108 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// +build go1.8 | ||
|
||
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package | ||
// | ||
// Copyright 2017 The Go-MySQL-Driver Authors. All rights reserved. | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
// You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package mysql | ||
|
||
import ( | ||
"context" | ||
"database/sql/driver" | ||
"net" | ||
"time" | ||
) | ||
|
||
func (mc *mysqlConn) Ping(ctx context.Context) error { | ||
var err error | ||
|
||
if ctx == nil { | ||
return mc.pingImpl() | ||
} | ||
mc.ctx = ctx | ||
defer func() { | ||
mc.ctx = nil | ||
}() | ||
|
||
if deadline, ok := ctx.Deadline(); ok { | ||
if err = mc.netConn.SetDeadline(deadline); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
if ctx.Done() == nil { | ||
return mc.pingImpl() | ||
} | ||
|
||
result := make(chan error) | ||
go func() { | ||
result <- mc.pingImpl() | ||
}() | ||
|
||
select { | ||
case <-ctx.Done(): | ||
// Because buffer.fill() and mysqlConn.writePacket() may overwrite the | ||
// deadline of read/write again and again in the above goroutine, | ||
// it has to use a loop to make sure SetDeadline() works. | ||
UpdateDeadlineLoop: | ||
for { | ||
// Copy it as a local variable because mc.netConn may be closed and | ||
// assigned nil in the above goroutine. | ||
netConn := mc.netConn | ||
if netConn != nil { | ||
errDeadline := netConn.SetDeadline(time.Now()) | ||
if errDeadline != nil { | ||
errLog.Print(errDeadline) | ||
} | ||
} | ||
select { | ||
case <-time.After(200 * time.Millisecond): | ||
// Prevent from leaking the above goroutine. | ||
case err = <-result: | ||
break UpdateDeadlineLoop | ||
} | ||
} | ||
case err = <-result: | ||
} | ||
|
||
if netErr, ok := err.(net.Error); ok { | ||
// We don't know where it timed out and it may leave some redundant data | ||
// in the connection so make it to be closed by DB.puConn() of the | ||
// caller. | ||
if netErr.Timeout() { | ||
return driver.ErrBadConn | ||
} | ||
} | ||
return err | ||
} | ||
|
||
func (mc *mysqlConn) pingImpl() error { | ||
if err := mc.writeCommandPacket(comPing); err != nil { | ||
return err | ||
} | ||
|
||
_, err := mc.readResultOK() | ||
return err | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Would
ctx.Done()
return nil?Example code in this document doesn't check nil.
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.
Yes,
context.Background().Done()
would returns nil. [1][2]