-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pool): add check for badConnection
* fix: badConn check(#2053) * fix: internalpool test * fix: sentinel test * fix: conncheck ut * fix: remove maxBadConnRetries * fix: add connCheck.deadline check Signed-off-by: monkey92t <[email protected]>
- Loading branch information
Showing
10 changed files
with
204 additions
and
8 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,49 @@ | ||
// +build linux darwin dragonfly freebsd netbsd openbsd solaris illumos | ||
|
||
package pool | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
"net" | ||
"syscall" | ||
"time" | ||
) | ||
|
||
var errUnexpectedRead = errors.New("unexpected read from socket") | ||
|
||
func connCheck(conn net.Conn) error { | ||
// Reset previous timeout. | ||
_ = conn.SetDeadline(time.Time{}) | ||
|
||
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,48 @@ | ||
//go:build linux || darwin || dragonfly || freebsd || netbsd || openbsd || solaris || illumos | ||
// +build linux darwin dragonfly freebsd netbsd openbsd solaris illumos | ||
|
||
package pool | ||
|
||
import ( | ||
"net" | ||
"net/http/httptest" | ||
"time" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("tests conn_check with real conns", func() { | ||
var ts *httptest.Server | ||
var conn net.Conn | ||
var err error | ||
|
||
BeforeEach(func() { | ||
ts = httptest.NewServer(nil) | ||
conn, err = net.DialTimeout(ts.Listener.Addr().Network(), ts.Listener.Addr().String(), time.Second) | ||
Expect(err).NotTo(HaveOccurred()) | ||
}) | ||
|
||
AfterEach(func() { | ||
ts.Close() | ||
}) | ||
|
||
It("good conn check", func() { | ||
Expect(connCheck(conn)).NotTo(HaveOccurred()) | ||
|
||
Expect(conn.Close()).NotTo(HaveOccurred()) | ||
Expect(connCheck(conn)).To(HaveOccurred()) | ||
}) | ||
|
||
It("bad conn check", func() { | ||
Expect(conn.Close()).NotTo(HaveOccurred()) | ||
Expect(connCheck(conn)).To(HaveOccurred()) | ||
}) | ||
|
||
It("check conn deadline", func() { | ||
Expect(conn.SetDeadline(time.Now())).NotTo(HaveOccurred()) | ||
time.Sleep(time.Millisecond * 10) | ||
Expect(connCheck(conn)).NotTo(HaveOccurred()) | ||
Expect(conn.Close()).NotTo(HaveOccurred()) | ||
}) | ||
}) |
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 |
---|---|---|
@@ -1,9 +1,14 @@ | ||
package pool | ||
|
||
import ( | ||
"net" | ||
"time" | ||
) | ||
|
||
func (cn *Conn) SetCreatedAt(tm time.Time) { | ||
cn.createdAt = tm | ||
} | ||
|
||
func (cn *Conn) NetConn() net.Conn { | ||
return cn.netConn | ||
} |
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