262 // WithTimeout returns WithDeadline(parent, time.Now().Add(timeout)). |
262 // WithTimeout returns WithDeadline(parent, time.Now().Add(timeout)). |
263 // |
263 // |
264 // Canceling this context releases resources associated with it, so code should |
264 // Canceling this context releases resources associated with it, so code should |
265 // call cancel as soon as the operations running in this Context complete: |
265 // call cancel as soon as the operations running in this Context complete: |
266 // |
266 // |
267 // func slowOperationWithTimeout(ctx context.Context) (Result, error) { |
267 // func slowOperationWithTimeout(ctx context.Context) (Result, error) { |
268 // ctx, cancel := context.WithTimeout(ctx, 100*time.Millisecond) |
268 // ctx, cancel := context.WithTimeout(ctx, 100*time.Millisecond) |
269 // defer cancel() // releases resources if slowOperation completes before timeout elapses |
269 // defer cancel() // releases resources if slowOperation completes before timeout elapses |
270 // return slowOperation(ctx) |
270 // return slowOperation(ctx) |
271 // } |
271 // } |
272 func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc) { |
272 func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc) { |
273 return WithDeadline(parent, time.Now().Add(timeout)) |
273 return WithDeadline(parent, time.Now().Add(timeout)) |
274 } |
274 } |
275 |
275 |
276 // WithValue returns a copy of parent in which the value associated with key is |
276 // WithValue returns a copy of parent in which the value associated with key is |