mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-24 01:30:25 +08:00
18 lines
334 B
Go
18 lines
334 B
Go
package contextx
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
)
|
|
|
|
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))
|
|
}
|