区分网络故障/延迟和远程服务器花费很长时间。

huangapple go评论90阅读模式
英文:

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请求/响应协议期望服务器在相对短的时间内回复响应。在这个层次上,服务器花费很长时间和网络延迟之间没有区别。关于在服务器上处理长时间运行的过程,您有几个选项:

  1. 使用一种不同的协议,该协议可以更新客户端处理进度。

  2. 在服务器上生成一个新进程来执行长时间的计算,并立即返回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:

  1. Use a different protocol that can update the client with the progress of the processing.

  2. 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.

huangapple
  • 本文由 发表于 2020年10月17日 00:46:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/64393273.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定