Simplify contextx.ShrinkDeadline (#596)

master
Oraoto 4 years ago committed by GitHub
parent 4e0d91f6c0
commit 425430f67c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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

Loading…
Cancel
Save