Simplify contextx.ShrinkDeadline (#596)

This commit is contained in:
Oraoto
2021-04-03 21:25:32 +08:00
committed by GitHub
parent 4e0d91f6c0
commit 425430f67c

View File

@@ -8,12 +8,5 @@ import (
// ShrinkDeadline returns a new Context with proper deadline base on the given ctx and timeout.
// And returns a cancel function as well.
func ShrinkDeadline(ctx context.Context, timeout time.Duration) (context.Context, func()) {
if deadline, ok := ctx.Deadline(); ok {
leftTime := time.Until(deadline)
if leftTime < timeout {
timeout = leftTime
}
}
return context.WithDeadline(ctx, time.Now().Add(timeout))
return context.WithTimeout(ctx, timeout)
}