52 // WithTimeout returns WithDeadline(parent, time.Now().Add(timeout)). |
52 // WithTimeout returns WithDeadline(parent, time.Now().Add(timeout)). |
53 // |
53 // |
54 // Canceling this context releases resources associated with it, so code should |
54 // Canceling this context releases resources associated with it, so code should |
55 // call cancel as soon as the operations running in this Context complete: |
55 // call cancel as soon as the operations running in this Context complete: |
56 // |
56 // |
57 // func slowOperationWithTimeout(ctx context.Context) (Result, error) { |
57 // func slowOperationWithTimeout(ctx context.Context) (Result, error) { |
58 // ctx, cancel := context.WithTimeout(ctx, 100*time.Millisecond) |
58 // ctx, cancel := context.WithTimeout(ctx, 100*time.Millisecond) |
59 // defer cancel() // releases resources if slowOperation completes before timeout elapses |
59 // defer cancel() // releases resources if slowOperation completes before timeout elapses |
60 // return slowOperation(ctx) |
60 // return slowOperation(ctx) |
61 // } |
61 // } |
62 func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc) { |
62 func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc) { |
63 return WithDeadline(parent, time.Now().Add(timeout)) |
63 return WithDeadline(parent, time.Now().Add(timeout)) |
64 } |
64 } |
65 |
65 |
66 // WithValue returns a copy of parent in which the value associated with key is |
66 // WithValue returns a copy of parent in which the value associated with key is |