electricity_bill_calc_service/global/context.go

17 lines
449 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package global
import (
"context"
"time"
)
// 生成一个超时时间为5秒的倍率的上下文如果不传递任何值默认生成6倍的上下文即超时时间为30秒。
func TimeoutContext(multiply ...int64) (context.Context, context.CancelFunc) {
var ratio int64 = 6
if len(multiply) > 0 {
ratio = multiply[0]
}
timeout := time.Duration(ratio*5) * time.Second
return context.WithTimeout(context.Background(), timeout)
}