何时使用请求超时和网关超时

When to use Request timeout and Gateway timeout

我正在为第三方编写包装器 API。

如果我在特定时间内没有收到响应,我将抛出网关超时异常。

但是我看到除了HttpStatus.GATEWAY_TIMEOUT

还有一个HttpStatus.REQUEST_TIMEOUT

我不知道该用哪一个,什么时候用。

感谢任何帮助。

一旦您的服务器充当上游服务器的网关或代理,您应该使用504 来指示连接已超时。看看这个状态码是怎么定义的:

6.6.5. 504 Gateway Timeout

The 504 (Gateway Timeout) status code indicates that the server, while acting as a gateway or proxy, did not receive a timely response from an upstream server it needed to access in order to complete the request.

408 状态代码具有完全不同的含义,表示服务器想要关闭与客户端的未使用连接:

6.5.7. 408 Request Timeout

The 408 (Request Timeout) status code indicates that the server did not receive a complete request message within the time that it was prepared to wait. A server SHOULD send the "close" connection option in the response, since 408 implies that the server has decided to close the connection rather than continue waiting. If the client has an outstanding request in transit, the client MAY repeat that request on a new connection.