英文:
Distinguish between network failure/latency and the remote server taking a long time
问题
当我们发起HTTP请求时,可以设置连接/读取/写入超时时间,目前我遇到了以下异常:
java.net.SocketTimeoutException: Read timed out
如何判断是因为服务器处理请求花费了很长时间,还是因为存在大量的网络延迟?谢谢。
英文:
When we make a http request we can set a Connection/Read/Write timeout, currently I am getting below exception:
java.net.SocketTimeoutException: Read timed out
How to figure out of this is because server is taking long time to process the request or because there is a lot of network latency ? Thank You.
答案1
得分: 1
HTTP请求/响应协议期望服务器在相对短的时间内回复响应。在这个层次上,服务器花费很长时间和网络延迟之间没有区别。关于在服务器上处理长时间运行的过程,您有几个选项:
-
使用一种不同的协议,该协议可以更新客户端处理进度。
-
在服务器上生成一个新进程来执行长时间的计算,并立即返回HTTP响应。这个新的服务器进程可以在计算完成时通知HTTP服务器,例如将它们保存在文件中或提交到数据库中。然后提供另一个HTTP端点,客户端可以查询以检查进程是否已经完成。
英文:
The HTTP request/response protocol expects that the server replies with a response in a relatively short amount of time. At this level, there is no difference between the server taking a long time and network latency. You have a couple of options for handling long running processes on the server:
-
Use a different protocol that can update the client with the progress of the processing.
-
Spawn a new process on the server to do the long computation and return an HTTP response immediately. This new server process can notify the HTTP server when the calculations finish, such as by saving them in a file or committing to a database. Then provide another HTTP endpoint that the client can query to check if the process has finished.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论