Create a context with a timeout, but only if the timeout is longer than 0
(parent context.Context, timeout int)
| 7 | |
| 8 | // Create a context with a timeout, but only if the timeout is longer than 0 |
| 9 | func ContextWithConditionalTimeout(parent context.Context, timeout int) (context.Context, context.CancelFunc) { |
| 10 | var ( |
| 11 | ctx context.Context |
| 12 | cancel context.CancelFunc |
| 13 | ) |
| 14 | |
| 15 | if timeout <= 0 { |
| 16 | ctx, cancel = context.WithCancel(context.TODO()) |
| 17 | } else { |
| 18 | ctx, cancel = context.WithTimeout(context.TODO(), time.Duration(timeout)*time.Second) |
| 19 | } |
| 20 | |
| 21 | return ctx, cancel |
| 22 | } |
no outgoing calls
no test coverage detected