From c0e70ad31d7adba17c24e6d0ed79d953ce03ddab Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Fri, 14 Jun 2019 14:50:58 +0300 Subject: [PATCH] internal/pool: use min nonzero deadline --- internal/pool/conn.go | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/internal/pool/conn.go b/internal/pool/conn.go index 32d1cd43..acaa1b7c 100644 --- a/internal/pool/conn.go +++ b/internal/pool/conn.go @@ -82,19 +82,36 @@ func (cn *Conn) Close() error { } func (cn *Conn) deadline(ctx context.Context, timeout time.Duration) time.Time { - now := time.Now() - cn.SetUsedAt(now) + tm := time.Now() + cn.SetUsedAt(tm) + + if timeout > 0 { + tm = tm.Add(timeout) + } if ctx != nil { - tm, ok := ctx.Deadline() + deadline, ok := ctx.Deadline() if ok { - return tm + if timeout == 0 { + return deadline + } + return minNonzeroTime(deadline, tm) } } if timeout > 0 { - return now.Add(timeout) + return tm } return noDeadline } + +func minNonzeroTime(a, b time.Time) time.Time { + if a.IsZero() { + return b + } + if b.IsZero() || a.Before(b) { + return a + } + return b +}