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

Taught in

References