gpt4 book ai didi

php - SSH 连接在重负载下失败

转载 作者:行者123 更新时间:2023-12-02 13:49:13 26 4
gpt4 key购买 nike

$con = ssh2_connect($host, 22);
ssh2_auth_password($con, $rem_acc, $pass);
ssh2_scp_send($con,$rand.".gz","./".$rand.".gz");
$stream = ssh2_exec($con, "./exeonserv.sh ".$rand);

只要我将 PHP 脚本的负载保持在每秒 2 个请求以下(脚本中有 2 个 SSH 连接,因此每秒有 4 个连接),就可以正常工作

但是当它每秒超过 2 个请求时,连接开始失败,日志中出现以下错误:

[Sat Apr 21 11:51:40 2012] [error] [client 172.16.57.97] PHP Warning: ssh2_connect(): Error starting up SSH connection(-1): Failed getting banner in /var/www/fsproj/result.php on line 105
[Sat Apr 21 11:51:40 2012] [error] [client 172.16.57.97] PHP Warning: ssh2_connect(): Unable to connect to localhost in /var/www/fsproj/result.php on line 105



我使用以下代码尝试解决问题,但如果持续负载大于 2req/sec。它只是最终增加了响应时间
$con=false;    
while(!$con)
{
$con = ssh2_connect($host, 22);
}

可以打开 SSH 连接的最大速率是否有上限?如果是这样,我在哪里可以更改该值? (或任何其他解决方案?)

我在 Ubuntu 上使用 Apache

最佳答案

看看man sshd_config ,以下部分似乎控制了一次可以打开的最大 SSH 连接数以及最大并发连接尝试数。您需要修改 /etc/ssh/sshd_config与您想要的设置。

     MaxSessions
Specifies the maximum number of open sessions permitted per net-
work connection. The default is 10.

MaxStartups
Specifies the maximum number of concurrent unauthenticated con-
nections to the SSH daemon. Additional connections will be
dropped until authentication succeeds or the LoginGraceTime
expires for a connection. The default is 10.

Alternatively, random early drop can be enabled by specifying the
three colon separated values ``start:rate:full'' (e.g.
"10:30:60"). sshd(8) will refuse connection attempts with a
probability of ``rate/100'' (30%) if there are currently
``start'' (10) unauthenticated connections. The probability
increases linearly and all connection attempts are refused if the
number of unauthenticated connections reaches ``full'' (60).

此外,对于您尝试连接到服务器的示例,您可能需要添加 sleep 连接尝试失败后。如果没有此退避并且服务器被淹没,您的脚本可能会通过尝试通过连接尝试更多地淹没服务器而使事情变得更糟。

关于php - SSH 连接在重负载下失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10256887/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com