高流量网站的长轮询问题
假设我有脚本,它会在服务器上进行长轮询以检查用户是否有任何新消息。服务器端会是这样的,
while counter < 5
if something_changed
push_changes_to_client
break
else
counter++
sleep 5
它检查数据库 5 次,每次如果没有变化,它会等待 5 秒直到下一次检查,这导致最大执行时间约为 25 秒。
当客户快速从一个页面移动到另一页面时会发生什么?我想即使客户端移动到不同的页面(在该页面发送另一个更改请求)之后,服务器脚本也会继续运行。
这是否意味着,当很多人在站点上快速移动时(每个页面上的最大执行时间少于 25 秒),服务器必须继续运行所有试图响应不存在页面的脚本还有吗?这不会导致服务器很快地使用所有线程池吗?
Say I have script, that does long polling on server to check if user has any new mesages. Server side would be something like this
while counter < 5
if something_changed
push_changes_to_client
break
else
counter++
sleep 5
Which checks database 5 times and every time if there is no change, it waits 5s untill next check, which results in maximum execution time of about 25s.
What happens when client moves from one page to another really fast? I suppose the server script keep on running even after client move to different page, where it sends another request for changes.
Does this mean, that when lot of people are moving quickly around the site (less than the 25s max execution on each page), then the server has to keep running all the scripts, that are trying to respond to page that doesn't exist any more? Wouldn't this cause the server to use all of it's thread pool pretty fast?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在具有同步睡眠调用的每连接线程模型中,这确实可能占用大量线程。但是,如果“睡眠”只是调度回调并返回,则可以避免线程池堵塞。
In a thread-per-connection model with synchronous sleep calls, this indeed may tie up a large number of threads. However, if the "sleep" simply schedules a callback and returns, the thread pool logjam can be avoided.