Coder Social home page Coder Social logo

go-context's Introduction

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()
}

go-context's People

Contributors

prakharsrivastav avatar

Stargazers

 avatar Mohamad Harith avatar

Watchers

James Cloos avatar

Forkers

ankuagar

go-context's Issues

Calling cancel() more than once

Hi,

First, thanks for a great write-up on Context and Cancellation and the example code. I just wanted to add that in my opinion, cancel() can be called as often as required and all calls after the first are simple ignored. Have a look at the source of the context package. I believe it is considered best practice to defer cancel() as soon as the new cancellable context is created which is guaranteed to call cancel() even if it was called in the intervening lines.

I must say that I am no expert and would love to be corrected

I did enjoy reading your examples and many thanks for that.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.