Skip to content

Commit

Permalink
add close trace reason for connection was expired
Browse files Browse the repository at this point in the history
  • Loading branch information
徐焱 authored and Brian Picciano committed Aug 24, 2021
1 parent feacdae commit 69371f6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
25 changes: 16 additions & 9 deletions pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ func (p *Pool) getExisting() (*ioErrConn, error) {
}
if ioc.expired(p.opts.maxLifetime) {
ioc.Close()
p.traceConnClosed(trace.PoolConnClosedReasonPoolFull)
p.traceConnClosed(trace.PoolConnClosedReasonConnExpired)
atomic.AddInt64(&p.totalConns, -1)
continue
}
Expand Down Expand Up @@ -558,7 +558,7 @@ func (p *Pool) getExisting() (*ioErrConn, error) {
}
if ioc.expired(p.opts.maxLifetime) {
ioc.Close()
p.traceConnClosed(trace.PoolConnClosedReasonPoolFull)
p.traceConnClosed(trace.PoolConnClosedReasonConnExpired)
atomic.AddInt64(&p.totalConns, -1)
continue
}
Expand All @@ -583,20 +583,27 @@ func (p *Pool) get() (*ioErrConn, error) {
// discarded.
func (p *Pool) put(ioc *ioErrConn) bool {
p.l.RLock()
if ioc.lastIOErr == nil && !p.closed && !ioc.expired(p.opts.maxLifetime) {
select {
case p.pool <- ioc:
p.l.RUnlock()
return true
default:
var expired bool
if ioc.lastIOErr == nil && !p.closed {
if expired = ioc.expired(p.opts.maxLifetime); !expired {
select {
case p.pool <- ioc:
p.l.RUnlock()
return true
default:
}
}
}
p.l.RUnlock()

// the pool might close here, but that's fine, because all that's happening
// at this point is that the connection is being closed
ioc.Close()
p.traceConnClosed(trace.PoolConnClosedReasonPoolFull)
if expired {
p.traceConnClosed(trace.PoolConnClosedReasonConnExpired)
} else {
p.traceConnClosed(trace.PoolConnClosedReasonPoolFull)
}
atomic.AddInt64(&p.totalConns, -1)
return false
}
Expand Down
1 change: 1 addition & 0 deletions pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func TestPool(t *T) {
// This one is expected to error, since this test empties the pool by design
//t.Run("onEmptyErr", func(t *T) { do(PoolOnEmptyErrAfter(0)) })
t.Run("onEmptyErrAfter", func(t *T) { do(PoolOnEmptyErrAfter(1 * time.Second)) })
t.Run("maxLifetime", func(t *T) { do(PoolMaxLifetime(1 * time.Second)) })

t.Run("withTrace", func(t *T) {
var connCreatedCount int
Expand Down
4 changes: 4 additions & 0 deletions trace/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ const (
// PoolConnClosedReasonPoolFull indicates a connection was closed due to
// the Pool already being full. See The radix.PoolOnFullClose options.
PoolConnClosedReasonPoolFull PoolConnClosedReason = "pool full"

// PoolConnClosedReasonConnExpired indicates a connection was closed because
// the connection was expired. See The radix.PoolMaxLifetime options.
PoolConnClosedReasonConnExpired PoolConnClosedReason = "conn expired"
)

// PoolConnClosed is passed into the PoolTrace.ConnClosed callback whenever the
Expand Down

0 comments on commit 69371f6

Please sign in to comment.