Skip to content

conncheck: move var declarations into closure #997

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

Merged
merged 1 commit into from
Sep 7, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
43 changes: 22 additions & 21 deletions conncheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,36 @@ import (

var errUnexpectedRead = errors.New("unexpected read from socket")

func connCheck(c net.Conn) error {
var (
n int
err error
buff [1]byte
)

sconn, ok := c.(syscall.Conn)
func connCheck(conn net.Conn) error {
var sysErr error

sysConn, ok := conn.(syscall.Conn)
if !ok {
return nil
}
rc, err := sconn.SyscallConn()
rawConn, err := sysConn.SyscallConn()
if err != nil {
return err
}
rerr := rc.Read(func(fd uintptr) bool {
n, err = syscall.Read(int(fd), buff[:])

err = rawConn.Read(func(fd uintptr) bool {
var buf [1]byte
n, err := syscall.Read(int(fd), buf[:])
switch {
case n == 0 && err == nil:
sysErr = io.EOF
case n > 0:
sysErr = errUnexpectedRead
case err == syscall.EAGAIN || err == syscall.EWOULDBLOCK:
sysErr = nil
default:
sysErr = err
}
return true
})
switch {
case rerr != nil:
return rerr
case n == 0 && err == nil:
return io.EOF
case n > 0:
return errUnexpectedRead
case err == syscall.EAGAIN || err == syscall.EWOULDBLOCK:
return nil
default:
if err != nil {
return err
}

return sysErr
}
2 changes: 1 addition & 1 deletion conncheck_dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ package mysql

import "net"

func connCheck(c net.Conn) error {
func connCheck(conn net.Conn) error {
return nil
}