File tree Expand file tree Collapse file tree 4 files changed +36
-25
lines changed Expand file tree Collapse file tree 4 files changed +36
-25
lines changed Original file line number Diff line number Diff line change @@ -3,8 +3,10 @@ package pool
3
3
import (
4
4
"bufio"
5
5
"context"
6
+ "crypto/tls"
6
7
"net"
7
8
"sync/atomic"
9
+ "syscall"
8
10
"time"
9
11
10
12
"github.com/redis/go-redis/v9/internal/proto"
@@ -16,6 +18,9 @@ type Conn struct {
16
18
usedAt int64 // atomic
17
19
netConn net.Conn
18
20
21
+ // for checking the health status of the connection, it may be nil.
22
+ rawConn syscall.RawConn
23
+
19
24
rd * proto.Reader
20
25
bw * bufio.Writer
21
26
wr * proto.Writer
@@ -34,6 +39,7 @@ func NewConn(netConn net.Conn) *Conn {
34
39
cn .bw = bufio .NewWriter (netConn )
35
40
cn .wr = proto .NewWriter (cn .bw )
36
41
cn .SetUsedAt (time .Now ())
42
+ cn .setRawConn ()
37
43
return cn
38
44
}
39
45
@@ -50,6 +56,23 @@ func (cn *Conn) SetNetConn(netConn net.Conn) {
50
56
cn .netConn = netConn
51
57
cn .rd .Reset (netConn )
52
58
cn .bw .Reset (netConn )
59
+ cn .setRawConn ()
60
+ }
61
+
62
+ func (cn * Conn ) setRawConn () {
63
+ conn := cn .netConn
64
+ if conn == nil {
65
+ return
66
+ }
67
+ if tlsConn , ok := conn .(* tls.Conn ); ok {
68
+ conn = tlsConn .NetConn ()
69
+ }
70
+
71
+ if sysConn , ok := conn .(syscall.Conn ); ok {
72
+ if rawConn , err := sysConn .SyscallConn (); err == nil {
73
+ cn .rawConn = rawConn
74
+ }
75
+ }
53
76
}
54
77
55
78
func (cn * Conn ) Write (b []byte ) (int , error ) {
Original file line number Diff line number Diff line change 3
3
package pool
4
4
5
5
import (
6
- "crypto/tls"
7
6
"errors"
8
7
"io"
9
- "net"
10
8
"syscall"
11
- "time"
12
9
)
13
10
14
11
var errUnexpectedRead = errors .New ("unexpected read from socket" )
15
12
16
- func connCheck (conn net.Conn ) error {
17
- // Reset previous timeout.
18
- _ = conn .SetDeadline (time.Time {})
19
-
20
- // Check if tls.Conn.
21
- if c , ok := conn .(* tls.Conn ); ok {
22
- conn = c .NetConn ()
23
- }
24
- sysConn , ok := conn .(syscall.Conn )
25
- if ! ok {
26
- return nil
27
- }
28
- rawConn , err := sysConn .SyscallConn ()
29
- if err != nil {
30
- return err
31
- }
32
-
13
+ func connCheck (rawConn syscall.RawConn ) error {
33
14
var sysErr error
34
-
35
15
if err := rawConn .Read (func (fd uintptr ) bool {
36
16
var buf [1 ]byte
37
17
n , err := syscall .Read (int (fd ), buf [:])
Original file line number Diff line number Diff line change 2
2
3
3
package pool
4
4
5
- import "net"
5
+ import (
6
+ "syscall"
7
+ )
6
8
7
- func connCheck (conn net. Conn ) error {
9
+ func connCheck (_ syscall. RawConn ) error {
8
10
return nil
9
11
}
Original file line number Diff line number Diff line change @@ -499,6 +499,8 @@ func (p *ConnPool) Close() error {
499
499
return firstErr
500
500
}
501
501
502
+ var zeroTime = time.Time {}
503
+
502
504
func (p * ConnPool ) isHealthyConn (cn * Conn ) bool {
503
505
now := time .Now ()
504
506
@@ -509,8 +511,12 @@ func (p *ConnPool) isHealthyConn(cn *Conn) bool {
509
511
return false
510
512
}
511
513
512
- if connCheck (cn .netConn ) != nil {
513
- return false
514
+ if cn .rawConn != nil {
515
+ // reset previous timeout.
516
+ _ = cn .netConn .SetDeadline (zeroTime )
517
+ if connCheck (cn .rawConn ) != nil {
518
+ return false
519
+ }
514
520
}
515
521
516
522
cn .SetUsedAt (now )
You can’t perform that action at this time.
0 commit comments