长时间的呼叫 - 如何避免网络超时

发布于 2025-01-17 08:39:23 字数 1085 浏览 0 评论 0原文

在一个网络应用程序中,我对多个长时间运行的 php 脚本进行了一些 ajax 调用(用户等待是可以的)。不过,为了安全起见,我将在服务器前面添加 AWS cloudfront,这将导致 180 秒(允许的最大值)后超时。

我需要实现一种机制来在服务器端运行长脚本并轮询浏览器的结果。 一种选择是:

On the server side:
We create a script 'async_call' which receives the file to call in addition to the url params and does the following:

1 - calc hash for the the call (used for storing/getting apc info for this call)

2 - if status in apc value indicates not running
2.1 - Store in call-status-APC-<hash> the call status as running 
2.2 - Call the script file using curl with params and cookies without timeout
2.3 - when the data returns store it in return-value-APC-<hash> and return it.

3 - If the apc status indicates running
3.1 Poll for results in the return-value-APC-<hash> every few seconds and if exist delete and return it.

On the client side:
Ajax has retry functionality so if it times out it will call again. so:
1 - change from calling ajax_file.php -> async_call?file=ajax_file.php&all-other-params
2- set ajax retry (e.g. 10)

是否有更优雅的方法来做到这一点?

In a web app I have some ajax calls to multiple long running php scripts (its OK that the user will wait). However I'm about to add AWS cloudfront in front of the server for security which will cause timeout after 180sec (its the maximum allowed).

I need to implement a mechanism to run the long scripts on the server side and poll the results from the browser.
One option is :

On the server side:
We create a script 'async_call' which receives the file to call in addition to the url params and does the following:

1 - calc hash for the the call (used for storing/getting apc info for this call)

2 - if status in apc value indicates not running
2.1 - Store in call-status-APC-<hash> the call status as running 
2.2 - Call the script file using curl with params and cookies without timeout
2.3 - when the data returns store it in return-value-APC-<hash> and return it.

3 - If the apc status indicates running
3.1 Poll for results in the return-value-APC-<hash> every few seconds and if exist delete and return it.

On the client side:
Ajax has retry functionality so if it times out it will call again. so:
1 - change from calling ajax_file.php -> async_call?file=ajax_file.php&all-other-params
2- set ajax retry (e.g. 10)

Is there a more elegant way to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文