超时控制
go-zero 在多层执行请求超时控制,防止慢上游耗尽协程池。
HTTP 服务超时
Section titled “HTTP 服务超时”Timeout: 3000 # 毫秒(默认值:3000)超过此时间的请求会自动收到 HTTP 408 响应。
gRPC 服务端超时
Section titled “gRPC 服务端超时”Timeout: 2000 # 毫秒通过带有 deadline 的 context 传递:
ctx, cancel := context.WithTimeout(l.ctx, 2*time.Second)defer cancel()
resp, err := l.svcCtx.OrderRpc.CreateOrder(ctx, req)go-zero 的 TimeoutInterceptor 会自动将入站请求的 deadline 传播到所有下游 RPC 调用。
按路由超时(HTTP)
Section titled “按路由超时(HTTP)”server.AddRoute(rest.Route{ Method: http.MethodPost, Path: "/slow-endpoint", Handler: myHandler,}, rest.WithTimeout(10*time.Second))超时与 Context 取消的区别
Section titled “超时与 Context 取消的区别”| 场景 | 行为 |
|---|---|
| 客户端断开连接 | Context 被取消,上游协程退出 |
| 超时到达 | 返回 408;协程继续运行直到完成 |
| 两者同时 | Context 先被取消 |