Timeout
Timeout
Section titled “Timeout”go-zero enforces request timeouts at multiple layers to prevent slow upstreams from exhausting goroutine pools.
HTTP Service Timeout
Section titled “HTTP Service Timeout”Timeout: 3000 # milliseconds (default: 3000)Requests exceeding this duration receive HTTP 408 automatically.
gRPC Server Timeout
Section titled “gRPC Server Timeout”Timeout: 2000 # millisecondsClient-Side Timeout
Section titled “Client-Side Timeout”Pass a context with deadline:
ctx, cancel := context.WithTimeout(l.ctx, 2*time.Second)defer cancel()
resp, err := l.svcCtx.OrderRpc.CreateOrder(ctx, req)go-zero’s TimeoutInterceptor propagates the deadline from the incoming request to all downstream RPC calls automatically.
Per-Route Timeout (HTTP)
Section titled “Per-Route Timeout (HTTP)”server.AddRoute(rest.Route{ Method: http.MethodPost, Path: "/slow-endpoint", Handler: myHandler,}, rest.WithTimeout(10*time.Second))Timeout vs Context Cancellation
Section titled “Timeout vs Context Cancellation”| Scenario | Behaviour |
|---|---|
| Client disconnects | Context cancelled, upstream goroutine exits |
| Timeout exceeded | 408 returned; goroutine keeps running until completion |
| Both | Context cancelled first |