-
Notifications
You must be signed in to change notification settings - Fork 2.4k
ConnPool check fd for bad conns #1824
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f22b38d
conncheck for badconn (#1821)
78875ec
format imports
6e0ed33
fix ut: pool with badconn
c7eeca2
fix unstable ut: should facilitate failover
7f52d73
Revert "fix unstable ut: should facilitate failover"
e4bc7ad
fix test error
monkey92t 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// +build linux darwin dragonfly freebsd netbsd openbsd solaris illumos | ||
|
||
package pool | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
"net" | ||
"syscall" | ||
) | ||
|
||
var errUnexpectedRead = errors.New("unexpected read from socket") | ||
|
||
func connCheck(conn net.Conn) error { | ||
sysConn, ok := conn.(syscall.Conn) | ||
if !ok { | ||
return nil | ||
} | ||
rawConn, err := sysConn.SyscallConn() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var sysErr error | ||
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 | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return sysErr | ||
} |
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,9 @@ | ||
// +build !linux,!darwin,!dragonfly,!freebsd,!netbsd,!openbsd,!solaris,!illumos | ||
|
||
package pool | ||
|
||
import "net" | ||
|
||
func connCheck(conn net.Conn) error { | ||
return nil | ||
} |
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,46 @@ | ||
// +build linux darwin dragonfly freebsd netbsd openbsd solaris illumos | ||
|
||
package pool | ||
|
||
import ( | ||
"net" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func Test_connCheck(t *testing.T) { | ||
// tests with real conns | ||
ts := httptest.NewServer(nil) | ||
defer ts.Close() | ||
|
||
t.Run("good conn", func(t *testing.T) { | ||
conn, err := net.DialTimeout(ts.Listener.Addr().Network(), ts.Listener.Addr().String(), time.Second) | ||
if err != nil { | ||
t.Fatalf(err.Error()) | ||
} | ||
defer conn.Close() | ||
if err = connCheck(conn); err != nil { | ||
t.Fatalf(err.Error()) | ||
} | ||
conn.Close() | ||
|
||
if err = connCheck(conn); err == nil { | ||
t.Fatalf("expect has error") | ||
} | ||
}) | ||
|
||
t.Run("bad conn 2", func(t *testing.T) { | ||
conn, err := net.DialTimeout(ts.Listener.Addr().Network(), ts.Listener.Addr().String(), time.Second) | ||
if err != nil { | ||
t.Fatalf(err.Error()) | ||
} | ||
defer conn.Close() | ||
|
||
ts.Close() | ||
|
||
if err = connCheck(conn); err == nil { | ||
t.Fatalf("expect has err") | ||
} | ||
}) | ||
} |
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
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
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.
Nice!