- Published on
Context in GoLang
- Authors
- Name
- Milad E. Fahmy
- @miladezzat12
Context in GoLang
What is Context?
In Go, the context package provides a powerful mechanism for managing the lifecycle of operations and propagating cancellation signals across goroutine. It allows you to set deadlines, timeouts, and cancellation signals, helping you control the execution of your code and handle resource cleanup gracefully. There are three commonly used functions in the context package for managing cancellation: WithCancel, WithTimeout, and WithDeadline.
1.WithCancel: WithCancel returns a derived Context and a CancelFunc. You can call the CancelFunc to cancel the Context manually. This is useful when you want to cancel an operation explicitly or based on some condition.
2.WithTimeout: WithTimeout returns a derived Context that is automatically canceled after a specified timeout duration. This is useful when you want to limit the execution time of an operation
3.WithDeadline: WithDeadline returns a derived Context that is automatically canceled at a specified deadline time. This is useful when you want to set an absolute deadline for an operation.
withTimeout and withDeadline are very much similar. infact, withtimeout used deadline internally.
Let’s, jump into few examples:-
- With Cancel:-
func. main(){
ctx, cancelFunc := context.WithCancel(context.Background())
go stopTheTaskIfNotInterested(ctx)
// cancelling the context
cancelFunc()
}
func stopTheTaskIfNotInterested(ctx context.Context) {
for {
select {
case <-ctx.Done():
fmt.Println("exiting the task... because parent not interested..!!")
return
}
}
}
- with Timeout:-
func. main(){
ctx, cancelFunc := context.WithTimeout(ctx, time.Second*1)
go stopTheTaskIfNotCompletedWithInGivenTime(ctx)
// cancelling the context
defer func() {
cancelFunc()
}()
}
func stopTheTaskIfNotCompletedWithInGivenTime(ctx context.Context) {
for {
select {
case <-ctx.Done():
fmt.Println("exiting the task. because i am not able to complete with in time..!!!")
return
}
}
}
- withDeadLine:-
func. main(){
ctx, cancelFunc := context.WithDeadline(context.Background(), time.Now().Add(time.Second*2))
go stopTheTaskIfNotCompletedWithInGivenTime(ctx)
// cancelling the context
defer func() {
cancelFunc()
}()
}
func stopTheTaskIfNotCompletedWithInGivenTime(ctx context.Context) {
for {
select {
case <-ctx.Done():
fmt.Println("exiting the task. because i am not able to complete with in time..!!!")
return
}
}
}
few general use cases of context:-
- Graceful Shutdown of servers (when you want to drain all the request before existing the application)
- Managing Go routine Lifecycle(when you want to cancel , set timeouts to long running tasks)
- Tracing
- Cancellation and Timeout
- Passing request scoped data
- Managing request scoped data