Skip to content
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

Add ConnChecker to customize conn health check #3060

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions internal/pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type Options struct {
MaxActiveConns int
ConnMaxIdleTime time.Duration
ConnMaxLifetime time.Duration
ConnChecker func(net.Conn) error
}

type lastDialErrorWrap struct {
Expand Down Expand Up @@ -513,6 +514,12 @@ func (p *ConnPool) isHealthyConn(cn *Conn) bool {
return false
}

if p.cfg.ConnChecker != nil {
if err := p.cfg.ConnChecker(cn.netConn); err != nil {
return false
}
}

cn.SetUsedAt(now)
return true
}
4 changes: 4 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ type Options struct {
// Default is to not close idle connections.
ConnMaxLifetime time.Duration

// ConnChecker checks the health of a connection before returning it to the client.
ConnChecker func(net.Conn) error

// TLS Config to use. When set, TLS will be negotiated.
TLSConfig *tls.Config

Expand Down Expand Up @@ -520,5 +523,6 @@ func newConnPool(
MaxActiveConns: opt.MaxActiveConns,
ConnMaxIdleTime: opt.ConnMaxIdleTime,
ConnMaxLifetime: opt.ConnMaxLifetime,
ConnChecker: opt.ConnChecker,
})
}
Loading