-
Notifications
You must be signed in to change notification settings - Fork 18k
Proposal: net: Add API to check connection Readability without blocking #28607
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
Comments
Is the /cc @kardianos |
The MS SQL driver spins up goroutines. That's what I recommen MySQL doing too. Your proposal ends up racy or spinning I believe if you tried it. There was one proposal to expose the polling interface from the runtime, but I don't think it went anywhere. |
@kardianos Would you point out where go-mssqldb uses goroutine to check connection closed while it is idle (not waiting response)? |
I'm confused about the proposal. The title says "check EOF" but the text seems to be about "check whether Read will proceed without blocking" (data available or EOF). Assuming this is really about readability and not specifically EOF, this is a duplicate of #15735. (Operating systems do provide us ways to watch general readability. I'm not sure how to watch EOF (without reading).) Closing as duplicate of #15735, since that's the one we could actually implement. |
@rsc It bit different. I want to check readable without blocking. I don't want to wait until readable. I don't want callbacks. func (mc mysqlConn) Query(query string, args... driver.Value) error {
if mc.conn.Readable() {
// Connection is closed from server.
return mc.handleServerGone()
}
mc.sendQuery(query, args...)
return mc.recvResult()
} |
I'm a maintainer of go-sql-driver/mysql.
Since MySQL protocol is a request-response protocol, we don't have dedicated goroutine for
Write()
andRead()
. We call them in the goroutine executingDB.Query()
.Sometimes, DB connection is closed from a server or other middlewares.
But we can not detect connection closed by peer until we call
Read()
.Since we send a query before reading result, we can not retry the query. Otherwise, the query may be executed twice. See also: https://golang.org/pkg/database/sql/driver/
If we can detect connection closed by peer before sending a query, we can retry safely. But there is no easy way to do it now.
DB.Query()
.conn.SetReadDeadline(time.Now().Add(time.Millisecond)); conn.Read()
introduces unnecessary sleep for normal case.So I want a easy way to check EOF. I have two ideas about it:
c.Readable() bool
-- Returns true whenc.Read()
will not block.c.ReadNonblock(buff []bytes) (int, error)
-- Nonblocking variants ofc.Read()
.The text was updated successfully, but these errors were encountered: