Skip to content

Understanding context cancellation and timeouts in golang

Notifications You must be signed in to change notification settings

PrakharSrivastav/go-context

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Understanding context in go

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.

Creating new context

  • context.Background() : Use when creating fresh default context.
  • context.Todo() : For future use.

Deriving context from an existing context

  • 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 a context

  1. cancelling explicitly
func main() {
    _,cancel := context.WithCancel(parentCtx)
    // call the cancel to cancel the context
    cancel()
}
  1. 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()  
}
  1. 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()
}

About

Understanding context cancellation and timeouts in golang

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages