This repository intends to explain how timeout and cancellations can be handled usingcontext
package in go. This repository supports the
examples discussed in this blog post.
- context.Background() : Use when creating fresh default context.
- context.Todo() : For future use.
- context.WithCancel(parentCtx) : Provides a new context with a CancelFunc. CancelFunc can be used for explicit cancellation.
- context.WithDeadline(parentCtx) : Provides a new context that will automatically cancel after a durations. Also provides CancelFunc for explicit cancellation.
- context.WithTimeout(parentCtx) : Provides a new context that will expire at a given timestamp. Also provides CancelFunc for explicit cancellation.
- cancelling explicitly
func main() {
_,cancel := context.WithCancel(parentCtx)
// call the cancel to cancel the context
cancel()
}
- cancelling after few seconds
func main() {
// cancel the context in 10 seconds
_,cancel := context.WithTimeout(parentCtx, time.Second * 10)
// cancel explicitly (when function returns)
defer cancel()
}
- cancelling at a given time
func main() {
// cancel the context at given time
_,cancel := context.WithDeadline(parentCtx, time.Now().Add(2 * time.Second))
// cancel explicitly (when function returns)
defer cancel()
}