From a439f9862212bebe7b67fa8995d3245f71d27c70 Mon Sep 17 00:00:00 2001 From: Will Bradley Date: Sat, 28 Feb 2015 13:59:45 -0700 Subject: [PATCH] Adding timeout options to curl --- README.md | 15 +++++++++++++++ lib/InfluxPHP/BaseHTTP.php | 5 +++++ lib/InfluxPHP/Client.php | 4 +++- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 12c22d2..482e3ce 100644 --- a/README.md +++ b/README.md @@ -55,3 +55,18 @@ foreach ($db->query("SELECT * FROM foo;") as $row) { var_dump($row, $row->time); } ``` + +Options +------- + +- **Timeout** to prevent slow queries from hanging your program: + +```php +$client = new \crodas\InfluxPHP\Client( + "localhost" /*default*/, + 8086 /* default */, + "root" /* by default */, + "root" /* by default */, + 60 // 60-second timeout (CURLOPT_TIMEOUT) +); +``` \ No newline at end of file diff --git a/lib/InfluxPHP/BaseHTTP.php b/lib/InfluxPHP/BaseHTTP.php index 8b98dd5..b061a06 100644 --- a/lib/InfluxPHP/BaseHTTP.php +++ b/lib/InfluxPHP/BaseHTTP.php @@ -44,6 +44,7 @@ class BaseHTTP protected $user; protected $pass; protected $base; + protected $timeout; protected $timePrecision = 's'; protected $children = array(); @@ -60,6 +61,7 @@ class BaseHTTP $this->pass = $c->pass; $this->port = $c->port; $this->host = $c->host; + $this->timeout = $c->timeout; $this->timePrecision = $c->timePrecision; $c->children[] = $this; } @@ -71,6 +73,9 @@ class BaseHTTP $url .= "?" . http_build_query($args); $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + if($this->timeout !== null){ + curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout); + } return $ch; } diff --git a/lib/InfluxPHP/Client.php b/lib/InfluxPHP/Client.php index a304b2c..87ef51a 100644 --- a/lib/InfluxPHP/Client.php +++ b/lib/InfluxPHP/Client.php @@ -43,13 +43,15 @@ class Client extends BaseHTTP protected $port; protected $user; protected $pass; + protected $timeout; - public function __construct($host = "localhost", $port = 8086, $u = 'root', $p = 'root') + public function __construct($host = "localhost", $port = 8086, $u = 'root', $p = 'root', $timeout = null) { $this->host = $host; $this->port = $port; $this->user = $u; $this->pass = $p; + $this->timeout = $timeout; } public function deleteDatabase($name)