使用 PHP 检查服务器负载并采取适当的措施
我正在创建一个项目,其中服务器接收来自客户端的操作以应用于本地服务器文档。服务器和客户端都共享相同的文档,因此客户端发送的每条消息都包含 MD5 哈希值,服务器在生成自己的哈希值后与该哈希值进行比较,以确保服务器和客户端文档同步。
我的问题是,如果服务器过载,我能否以某种方式在 PHP 中检测到这一点,从而让我决定是否要执行哈希生成函数?
也许在定义的场景中,这不是一个完美的用例,但我总体上对这种方法感兴趣。
I'm creating a project in which a server receives operations from clients to apply to a local server document. The server and client both share the same document and therefore each message the client sends contains an MD5 hash, which the server compares to after generating its own hash to ensure the server and client documents are synchronized.
My question is, if the server is overloaded, could I somehow detect this in PHP, which would in turn let me decide whether I want to execute the hash generation function or not?
Perhaps in the scenario defined, this is not a perfect use-case, but I'm interested in this approach in general.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您在 Linux 下,您可以通过读取文件
/proc/loadavg
来获取处理器平均负载,该文件包含 3 个由空格分隔的数字。您还可以从 phpexec('uptime')
调用uptime
命令并解析其输出,其中还包含平均负载(以及其他内容)。如何解释平均负载 - 我通常将
1.0 * cores
作为最大健康负载。因此,如果您有 4 个核心,那么任何高于 4.0 的值都表明处理器正在执行一些繁重的计算,最好等待。Assuming you are under Linux, you can get processor load averages by reading from a file
/proc/loadavg
which contains 3 numbers separated by spaces. You can also calluptime
command from phpexec('uptime')
and parse its output, which also contains load averages (among other stuff).How to interpret load averages - I usually take
1.0 * cores
as a maximum healthy load. So if you have 4 cores then anything above 4.0 would indicate that processor is doing some heavy calculations and better wait.