context/

directory
v0.0.0-...-b3f521c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 19, 2017 License: Apache-2.0

README

Context - Standard Library

The package context defines the Context type, which carries deadlines, cancelation signals, and other request-scoped values across API boundaries and between processes.

Notes

  • Incoming requests to a server should create a Context.
  • Outgoing calls to servers should accept a Context.
  • The chain of function calls between them must propagate the Context.
  • Replace a Context using WithCancel, WithDeadline, WithTimeout, or WithValue.
  • When a Context is canceled, all Contexts derived from it are also canceled.
  • Do not store Contexts inside a struct type; instead, pass a Context explicitly to each function that needs it.
  • Do not pass a nil Context, even if a function permits it. Pass context.TODO if you are unsure about which Context to use.
  • Use context Values only for request-scoped data that transits processes and APIs, not for passing optional parameters to functions.
  • The same Context may be passed to functions running in different goroutines; Contexts are safe for simultaneous use by multiple goroutines.

Package context
Go Concurrency Patterns: Context - Sameer Ajmani
Cancellation, Context, and Plumbing - Sameer Ajmani
Using contexts to avoid leaking goroutines -- Jaana Burcu Dogan

Code Review

Store / Retrieve context values (Go Playground)
WithCancel (Go Playground)
WithDeadline (Go Playground)
WithTimeout (Go Playground)
Request/Response (Go Playground)

Exercises

Exercise 1

Use the template and follow the directions. You will be writing a web handler that performs a mock database call but will timeout based on a context if the call takes too long. You will also save state into the context.

Template (Go Playground) | Answer (Go Playground)


All material is licensed under the Apache License Version 2.0, January 2004.

Directories

Path Synopsis
Sample program to show how to store and retrieve values from a context.
Sample program to show how to store and retrieve values from a context.
Sample program to show how to use the WithCancel function.
Sample program to show how to use the WithCancel function.
Sample program to show how to use the WithDeadline function.
Sample program to show how to use the WithDeadline function.
Sample program to show how to use the WithTimeout function of the Context package.
Sample program to show how to use the WithTimeout function of the Context package.
Sample program that implements a web request with a context that is used to timeout the request if it takes too long.
Sample program that implements a web request with a context that is used to timeout the request if it takes too long.
exercises
exercise1
Sample program that implements a simple web service using the context to handle timeouts and pass context into the request.
Sample program that implements a simple web service using the context to handle timeouts and pass context into the request.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL