Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Flowing with Context

Flowing with Context

In this session, we will observe how a flowing context can help you optimize the resource usage of your server and enables context-based programming.

We will dive deeper into how context package works and why it is present in the Go as a native package at first place and how things were before this package. We will see different use cases of Context package and the do’s and don’ts being followed among the Gophers.

Avatar for Yashish Dua

Yashish Dua

July 13, 2019
Tweet

More Decks by Yashish Dua

Other Decks in Programming

Transcript

  1. Importance of Context Better decisions No wastage of resources Shared

    entities not leaked From real life's perspective
  2. Context API in Go A Context carries a deadline, cancellation

    signal, and request-scoped values across API boundaries.
  3. Context API in Go A Context carries a deadline, cancellation

    signal, and request-scoped values across API boundaries. Its methods are safe for simultaneous use by multiple goroutines.
  4. Derived Context The context package provides functions to derive new

    Context values from existing ones. These values form a tree: when a Context is canceled, all Contexts derived from it are also canceled.
  5. 1. Background Background returns an empty Context. It is never

    canceled, has no deadline, and has no values. Background is typically used in main, init, and tests, and as the top-level Context for incoming requests. func Background() Context
  6. 2. WithCancel WithCancel returns a copy of parent whose Done

    channel is closed as soon as parent.Done is closed or cancel is called. func WithCancel (parent Context) (ctx Context, cancel CancelFunc)
  7. 3. WithTimeout WithTimeout returns a copy of parent whose Done

    channel is closed as soon as parent.Done is closed, cancel is called, or timeout elapses. func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc)
  8. Context.Value Context.Value is for informing not controlling Context.Value should NEVER

    be used for values that are not created and destroyed during the lifetime of the request Important points to remember
  9. Advantages It separates the core computation parameters from the operational

    parameters. It codifies common operational aspects and how to communicate them across boundaries.
  10. Advantages It separates the core computation parameters from the operational

    parameters. It codifies common operational aspects and how to communicate them across boundaries. Optimize resource usage
  11. Criticism No acknowledgement of cancelation More prone to errors since

    avoiding static type checks Unnecessarily bloats function signature
  12. Okay, better practices! Most functions that use a Context should

    accept it as their first parameter: func F(ctx context.Context, /* other arguments */) {}
  13. Okay, better practices! Most functions that use a Context should

    accept it as their first parameter: func F(ctx context.Context, /* other arguments */) {} A function that is never request-specific may use context.Background() or context.TODO()
  14. Contexts are immutable, so it's fine to pass the same

    ctx to multiple calls that share the same deadline, cancellation signal, credentials, parent trace, etc
  15. Contexts are immutable, so it's fine to pass the same

    ctx to multiple calls that share the same deadline, cancellation signal, credentials, parent trace, etc Context should always be flowing