liveBook · Manning

Timeout

As explained in gRPC Microservices in Go

A timeout is a maximum permitted duration after which an operation is treated as failed or cancelled.

A timeout limits how long a service waits for an operation. In an order workflow, slow Payment or Shipping responses can delay order creation and breach a latency commitment. A client-side timeout lets the call fail rather than wait indefinitely. section 6.1.1 section 2.5.3

For a Go gRPC client call:

  • Create a context with context.WithTimeout(parent, duration), or use context.WithDeadline when you have a target time. section 6.1.1
  • Pass that context to the generated client method and handle the returned error if the call does not complete in time. section 2.5.3 section 6.1.1
  • Call the returned cancellation function when finished; defer cancel() is a concise way to do so. section 2.5.3 section 6.1.1
ctx, cancel := context.WithTimeout(context.Background(), 
10 * time.Second)
defer cancel()                                                #1
payment := pb.NewPaymentClient(conn)
result, err := payment.Create(ctx, &CreatePaymentRequest{})   #2
if err != nil {
    // handle Payment create an error
}

The Payment client gets ten seconds to complete Create; the same context is passed into the RPC, and cancellation is deferred. section 2.5.3

Pass the context onward when a service calls another service, so the downstream request remains subject to the caller’s time limit. Timeouts complement selective retries: retries can prolong a blocked call, while a deadline bounds the wait. Circuit breakers address repeated failures by temporarily stopping requests to an unhealthy dependency. section 6.1.1 section 6.4

How gRPC Microservices in Go covers it

How deep it goes
Demonstrates client-side deadlines, duration-based timeouts, cancellation and context propagation; does not detail timer internals.
Examples
Uses a ten-second Payment call and a one-second Shipping call, with downstream context propagation through Order and Product services.
What it assumes
Assumes generated gRPC clients and service calls, while introducing the context features needed to control their execution.
What you can do after
Apply it via Go context.WithTimeout or context.WithDeadline to bound a gRPC call and handle its error.
How it connects
Connects timeouts to gRPC contexts, deadlines, retries, circuit breakers and cascading failures.

Also explained in

Compare how 9 books explain timeout →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of gRPC Microservices in Go. The section links point to the parts of the book it draws on.