Skip to content

Commit

Permalink
reduce connections and time by a factor of 10
Browse files Browse the repository at this point in the history
  • Loading branch information
olivierlambert committed Dec 13, 2012
1 parent f5e75db commit 871df2e
Showing 1 changed file with 23 additions and 31 deletions.
54 changes: 23 additions & 31 deletions xenapi.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?
<?php
/*
* PHP XenAPI v1.0
* a class for XenServer API calls
Expand Down Expand Up @@ -27,14 +27,12 @@
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/

class XenApi {
private $_url;

private $_session_id;
private $_user;
private $_password;

private $_ch;
function __construct ($url, $user, $password) {
$r = $this->xenrpc_request($url, $this->xenrpc_method('session.login_with_password', array($user, $password, '1.3')));
if (is_array($r) && $r['Status'] == 'Success') {
Expand All @@ -46,7 +44,13 @@ function __construct ($url, $user, $password) {
echo "API failure. (" . implode(' ', $r['ErrorDescription']) . ")\n"; exit;
}
}

function __destruct()
{
if (isset($this->_ch))
{
curl_close($this->_ch);
}
}
function __call($name, $args) {
if (!is_array($args)) {
$args = array();
Expand All @@ -56,7 +60,6 @@ function __call($name, $args) {
$this->xenrpc_method($mod . '.' . $method, array_merge(array($this->_session_id), $args))));
return $ret;
}

function xenrpc_parseresponse($response) {
if (!@is_array($response) && !@$response['Status']) {
echo "API failure. (500)\n"; exit;
Expand All @@ -79,38 +82,27 @@ function xenrpc_parseresponse($response) {
}
return $ret;
}

function xenrpc_method($name, $params) {
$ret = xmlrpc_encode_request($name, $params);

return $ret;
}

function xenrpc_request($url, $req) {
$headers = array('Content-type: text/xml', 'Content-length: ' . strlen($req));

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS, $req);

$resp = curl_exec($ch);
curl_close($ch);

if (!isset($this->_ch))
{
$this->_ch = curl_init($url);
curl_setopt($this->_ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($this->_ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($this->_ch, CURLOPT_TIMEOUT, 60);
curl_setopt($this->_ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($this->_ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($this->_ch, CURLOPT_RETURNTRANSFER, 1);
}
curl_setopt($this->_ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($this->_ch, CURLOPT_POSTFIELDS, $req);

$resp = curl_exec($this->_ch);
$ret = xmlrpc_decode($resp);

return $ret;
}
}

1 comment on commit 871df2e

@hellracer
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quite a significant improvement

Please sign in to comment.