使用 PHP 检查服务器负载并采取适当的措施

发布于 2024-08-31 15:55:18 字数 229 浏览 4 评论 0原文

我正在创建一个项目,其中服务器接收来自客户端的操作以应用于本地服务器文档。服务器和客户端都共享相同的文档,因此客户端发送的每条消息都包含 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 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

满意归宿 2024-09-07 15:55:18

假设您在 Linux 下,您可以通过读取文件 /proc/loadavg 来获取处理器平均负载,该文件包含 3 个由空格分隔的数字。您还可以从 php exec('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 call uptime command from php exec('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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文