我在 mod_perl 下运行 perl 代码,它使用 Net::LDAP 模块连接到 openldap 服务器 slapd。
我正在尝试像这样设置连接超时:
my $ldap = Net::LDAP->new($server, timeout => 120);
但是当 slapd 负载很重时,我会在大约 20 秒后连接尝试超时。
Net::LDAP 使用 IO::Socket 和 IO::Select 来实现其连接处理,特别是 IO::Socket 中的这段代码(注意我添加了一些额外的调试代码):
sub connect {
@_ == 2 or croak 'usage: $sock->connect(NAME)';
my $sock = shift;
my $addr = shift;
my $timeout = ${*$sock}{'io_socket_timeout'};
my $err;
my $blocking;
my $start = scalar localtime;
$blocking = $sock->blocking(0) if $timeout;
if (!connect($sock, $addr)) {
if (defined $timeout && ($!{EINPROGRESS} || $!{EWOULDBLOCK})) {
require IO::Select;
my $sel = new IO::Select $sock;
undef $!;
if (!$sel->can_write($timeout)) {
$err = $! || (exists &Errno::ETIMEDOUT ? &Errno::ETIMEDOUT : 1);
$@ = "connect: timeout";
}
elsif (!connect($sock,$addr) &&
not ($!{EISCONN} || ($! == 10022 && $^O eq 'MSWin32'))
) {
# Some systems refuse to re-connect() to
# an already open socket and set errno to EISCONN.
# Windows sets errno to WSAEINVAL (10022)
my $now = scalar localtime;
$err = $!;
$@ = "connect: (1) $! : start = [$start], now = [$now], timeout = [$timeout] : " . Dumper(\%!);
}
}
elsif ($blocking || !($!{EINPROGRESS} || $!{EWOULDBLOCK})) {
$err = $!;
$@ = "connect: (2) $!";
}
}
$sock->blocking(1) if $blocking;
$! = $err if $err;
$err ? undef : $sock;
}
我看到这样的日志输出:
connect: (1) Connection timed out : start = [Tue Jun 19 14:57:44 2012], now = [Tue Jun 19 14:58:05 2012], timeout = [120] : $VAR1 = {
'EBADR' => 0,
'ENOMSG' => 0,
<snipped>
'ESOCKTNOSUPPORT' => 0,
'ETIMEDOUT' => 110,
'ENXIO' => 0,
'ETXTBSY' => 0,
'ENODEV' => 0,
'EMLINK' => 0,
'ECHILD' => 0,
'EHOSTUNREACH' => 0,
'EREMCHG' => 0,
'ENOTEMPTY' => 0
};
: Started attempt at Tue Jun 19 14:57:44 2012
20 秒连接超时从何而来?
编辑:我现在找到了罪魁祸首:/proc/sys/net/ipv4/tcp_syn_retries,默认设置为 5,重试 5 次大约需要 20 秒。 http://www.sekuda.com/overriding_the_default_linux_kernel_20_second_tcp_socket_connect_timeout