Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Paste
P1887
Fast_reconnect.php
Active
Public
Actions
Authored by
Joe
on Aug 17 2015, 9:14 AM.
Edit Paste
Archive Paste
View Raw File
Subscribe
Mute Notifications
Award Token
Flag For Later
Tags
acl*sre-team
Referenced Files
F1563078: Fast_reconnect.php
Aug 17 2015, 9:14 AM
2015-08-17 09:14:41 (UTC+0)
Subscribers
None
<?php
function
timedelta
(
$mend
,
$mstart
)
{
list
(
$uend
,
$send
)
=
explode
(
" "
,
$mend
);
list
(
$ustart
,
$sstart
)
=
explode
(
" "
,
$mstart
);
$sdiff
=
(
float
)
$send
-
(
float
)
$sstart
;
$udiff
=
(
float
)
$uend
-
(
float
)
$ustart
;
return
(
$sdiff
+
$udiff
);
}
/*
Let's test what happens to fsockopen if we don't get an answer after sending a SYN
*/
$start
=
microtime
();
$timeout
=
0.5
;
$errno
=
$errstr
=
null
;
$ips
=
array
(
'192.168.1.104'
,
'192.168.1.4'
);
foreach
(
$ips
as
$ip
)
{
$s
=
fsockopen
(
$ip
,
22
,
$errno
,
$errstr
,
$timeout
);
if
(
$s
)
{
$end
=
microtime
();
$tfirst
=
timedelta
(
$end
,
$start
);
echo
"First socket opened within $tfirst
\n
"
;
fclose
(
$s
);
break
;
}
}
$timeout
=
0.03
;
// 30 milliseconds
$start
=
microtime
();
foreach
(
$ips
as
$ip
)
{
$retries
=
2
;
for
(
$i
=
0
;
$i
<
$retries
;
$i
++)
{
$s
=
fsockopen
(
$ip
,
22
,
$errno
,
$errstr
,
$timeout
);
if
(
$s
)
{
$end
=
microtime
();
$tfirst
=
timedelta
(
$end
,
$start
);
echo
"Second socket opened within $tfirst
\n
"
;
fclose
(
$s
);
break
;
}
}
}
?>
joe@test:~$ php test.php
PHP Warning: fsockopen(): unable to connect to 192.168.1.104:22 (Connection timed out) in /home/joe/test.php on line 17
First socket opened within 0.508883
PHP Warning: fsockopen(): unable to connect to 192.168.1.104:22 (Connection timed out) in /home/joe/test.php on line 32
PHP Warning: fsockopen(): unable to connect to 192.168.1.104:22 (Connection timed out) in /home/joe/test.php on line 32
Second socket opened within 0.065743
Event Timeline
Joe
edited the content of this paste.
(Show Details)
Aug 17 2015, 9:14 AM
2015-08-17 09:14:41 (UTC+0)
Joe
changed the title of this paste from untitled to
Fast_reconnect.php
.
Joe
updated the paste's language from
autodetect
to
autodetect
.
Joe
added a project:
acl*sre-team
.
Joe
mentioned this in
T105378: Stop a poolcounter server fail from being a SPOF for the service and the api (and the site)
.
Aug 17 2015, 9:17 AM
2015-08-17 09:17:39 (UTC+0)
Log In to Comment