context deadline
A time limit attached to a context, after which it is cancelled automatically and ctx.Err() reports context.DeadlineExceeded.
In more detail
context.WithTimeout(ctx, 2*time.Second) returns a child context that expires after two seconds. Derive one per remote call or per health check so one slow target cannot hold up the rest. A child can shorten but never extend its parent's deadline.
Example
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond)
defer cancel()Related terms
- context cancellation — Telling every function using a context to stop, by calling the cancel function returned when the context was created.
- context — The Go standard library package whose Context value carries a deadline, a cancellation signal and request-scoped values through a chain of function calls.
- timeout — A limit on how long a caller waits for an answer before giving up.
- http.Client — The Go type that sends HTTP requests and returns responses, managing connections and timeouts for you.
Taught in
- The select Statement — Concurrent Systems Programming with Go
- The context Package — Concurrent Systems Programming with Go
- Context Cancellation and Timeouts — Concurrent Systems Programming with Go
- The time Package — Concurrent Systems Programming with Go
- Parsing Arguments with flag — Concurrent Systems Programming with Go
- Graceful Shutdown on SIGTERM — Concurrent Systems Programming with Go
- Handling Timeouts and Errors — Concurrent Systems Programming with Go