internal/pool: use min nonzero deadline

This commit is contained in:
Vladimir Mihailenco 2019-06-14 14:50:58 +03:00
parent f8704e4b6b
commit c0e70ad31d
1 changed files with 22 additions and 5 deletions

View File

@ -82,19 +82,36 @@ func (cn *Conn) Close() error {
} }
func (cn *Conn) deadline(ctx context.Context, timeout time.Duration) time.Time { func (cn *Conn) deadline(ctx context.Context, timeout time.Duration) time.Time {
now := time.Now() tm := time.Now()
cn.SetUsedAt(now) cn.SetUsedAt(tm)
if timeout > 0 {
tm = tm.Add(timeout)
}
if ctx != nil { if ctx != nil {
tm, ok := ctx.Deadline() deadline, ok := ctx.Deadline()
if ok { if ok {
return tm if timeout == 0 {
return deadline
}
return minNonzeroTime(deadline, tm)
} }
} }
if timeout > 0 { if timeout > 0 {
return now.Add(timeout) return tm
} }
return noDeadline 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
}