Source File
conn_check.go
Belonging Package
github.com/redis/go-redis/v9/internal/pool
//go:build linux || darwin || dragonfly || freebsd || netbsd || openbsd || solaris || illumospackage poolimport ()var errUnexpectedRead = errors.New("unexpected read from socket")// connCheck checks if the connection is still alive and if there is data in the socket// it will try to peek at the next byte without consuming it since we may want to work with it// later on (e.g. push notifications)func connCheck( net.Conn) error {// Reset previous timeout._ = .SetDeadline(time.Time{})// Health checks deliberately inspect only the outer connection. Unwrapping a// buffered transport such as crypto/tls.Conn can reveal an encrypted// post-handshake record and make isHealthyConn call PeekReplyType on the TLS// stream. With the deadline cleared above, TLS may consume that control record// and then wait forever for application data., := .(syscall.Conn)if ! {return nil}return checkSyscallConn()}func checkSyscallConn( syscall.Conn) error {, := .SyscallConn()if != nil {return}var errorif := .Read(func( uintptr) bool {var [1]byte// Use MSG_PEEK to peek at data without consuming it, , := syscall.Recvfrom(int(), [:], syscall.MSG_PEEK|syscall.MSG_DONTWAIT)switch {case == 0 && == nil:= io.EOFcase > 0:= errUnexpectedReadcase == syscall.EAGAIN || == syscall.EWOULDBLOCK:= nildefault:=}return true}); != nil {return}return}// underlyingSyscallConn unwraps connections that expose their transport through// NetConn (notably crypto/tls.Conn). Limit the walk so a broken wrapper cannot// loop forever.func underlyingSyscallConn( net.Conn) (syscall.Conn, bool) {for range 8 {if , := .(syscall.Conn); {return , true}, := .(interface{ () net.Conn })if ! {return nil, false}= .()if == nil {return nil, false}}return nil, false}// maybeHasData checks if there is data in the socket without consuming itfunc maybeHasData( net.Conn) bool {, := checkForData()return}func checkForData( net.Conn) (bool, error) {// Clear any residual READ deadline first: a prior command read (WithReader)// leaves its deadline armed, and once it expires rawConn.Read fails fast// with "raw-read ... i/o timeout" BEFORE the non-blocking peek runs — the// caller would misread an idle-but-healthy conn as dead (the CSC drainer// then removes it and evicts its cache coverage; with the full-duplex// coalescer concentrating a cache's coverage on one held conn, that one// spurious removal wipes the whole cache). Read-only on purpose, unlike the// SetDeadline reset this replaced: checkForData runs CONCURRENTLY with// command WRITES on a held full-duplex connection, and a full SetDeadline// would strip an armed write deadline mid-write. No concurrent READ can// race this: every caller either owns the conn's read side (FD session// reader between reads) or holds the conn exclusively (drainer borrow,// pool health check)._ = .SetReadDeadline(time.Time{}), := underlyingSyscallConn()if ! {return false, nil}switch := checkSyscallConn(); {case nil:return false, nilcase errUnexpectedRead:return true, nildefault:return false,}}// needsCscReadProbe reports whether a command read may leave data hidden from// maybeHasData. On Unix a direct syscall.Conn has no intermediate buffering;// TLS and opaque wrappers need one bounded post-command probe.func needsCscReadProbe( net.Conn) bool {, := .(syscall.Conn)return !}// needsCscPeriodicProbe reports whether the platform can inspect the transport// at all. Opaque wrappers get a throttled bounded fallback so invalidations that// arrive after the post-command probe are still eventually consumed.func needsCscPeriodicProbe( net.Conn) bool {, := underlyingSyscallConn()return !}
![]() |
The pages are generated with Golds v0.8.7. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |