-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ConnPool check fd for bad conns (#1824)
* conncheck for badconn (#1821) * format imports * fix ut: pool with badconn * fix unstable ut: should facilitate failover * Revert "fix unstable ut: should facilitate failover" This reverts commit c7eeca2. * fix test error Signed-off-by: monkey92t <golang@88.com> Co-authored-by: hidu <duv123+github@gmail.com> Co-authored-by: monkey92t <golang@88.com>
- Loading branch information
1 parent
62fc2c8
commit 346bfaf
Showing
10 changed files
with
204 additions
and
18 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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