Handler Socket "-1:connect: 99" error during openIndex(..) operation
Hi, I'm evaluating the usage of Handler Socket in production and making some stress tests locally. I'm facing this error while testing the following PHP code:
<?php
....
for($i=0;$i<100000;++$i)
{
try
{
$plr = Player::loadById($this->db, 100);
$plr->set('name', "Bazinga");
$plr->save();
}
catch(Exception $e)
{
echo "Error at $i\n";
throw $e;
}
}
?>
The error happens constantly after 30-40k of iterations. No error happens if Player's methods are implemented using standard PHP mysqli functions.
I'm using HandlerSocket plugin shipped with Percona Server version 5.1.54-rel12.5(installed from official lucid repo).
Player::loadById() and Player::save() are implemented with HandlerSocket using php_handlersocket extension. I looked in the source of php_handlersocket and it does nothing special - simply delegates all work to "dena::hstcpcli_i".
Implementation of Player::loadById() boils down to the following method:
protected function _fetchQuery($table_name, array $fields, $id)
{
$hs = $this->_db->openHSocket();
if(!$hs->openIndex(1, $this->_db->db_name, $table_name, 'PRIMARY', implode(',', $fields)))
throw new Exception("Could not open index: " . $hs->getError());
$resp = $hs->executeSingle(1, '=', array($id));
if(!$resp)
{
$this->_db->closeHSocket($hs);
return array();
}
$row = array();
foreach($resp[0] as $i => $v)
$row[$fields[$i]] = $v;
$this->_db->closeHSocket($hs);
return array($row);
}
And implementation of Player::save() boils down to another method:
protected function _updateQuery($table_name, array $data)
{
$hs = $this->_db->openHSocket(true/*write*/);
if(!$hs->openIndex(2, $this->_db->db_name, $table_name, 'PRIMARY', implode(',', array_keys($data))))
throw new Exception("Could not open index: " . $hs->getError());
$hs->executeUpdate(2, '=', array($this->id), array_values($data), 1, 0);
$this->_db->closeHSocket($hs);
}
The error code 99 indicates that there is no local port available. Probably your test client closes tcp connections so frequently that many local ports are in the TIME_WAIT state. The best way to avoid this problem is to use persistent connections.
That makes sense, thanks for explanation. As for persistent connections....I'm not sure how to do that. I believe the php extension should provide a pool of handlersockets in case I want to use persistent connections, right? Achieving this in pure PHP is problematic, I guess.
I made test with native API and 1 000 K iteration of set and get operation ( + / =) I d't have errors. It can be the errors of the php_handlersocket.
My connection is persisten.