Skip to content
This repository has been archived by the owner on Sep 6, 2023. It is now read-only.

Commit

Permalink
Merge pull request #2 from StarOfService/request-execution-time
Browse files Browse the repository at this point in the history
Request max execution time
  • Loading branch information
matasarei authored Sep 15, 2020
2 parents d774582 + b091936 commit 95ff5a2
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
.DS_Store
vendor/
composer.lock
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
}
],
"require": {
"php": ">=5.4"
"php": ">=5.4",
"ext-curl": "*"
},
"autoload": {
"psr-0": {"JsonRPC": "src/"}
Expand Down
48 changes: 41 additions & 7 deletions src/JsonRPC/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,18 @@ class HttpClient
protected $url;

/**
* HTTP client timeout
* HTTP connection timeout
*
* @var integer
*/
protected $timeout = 5;
protected $connectionTimeout = 5;

/**
* Request execution timeout
*
* @var int
*/
protected $requestTimeout = 60;

/**
* Default HTTP headers to send to the server
Expand Down Expand Up @@ -143,15 +150,41 @@ public function withPassword($password)
}

/**
* Set timeout
* Set connection timeout
*
* @deprecated Use withConnectionTimeout or withRequestTimeout instead
* @see withConnectionTimeout
* @see withRequestTimeout
*
* @param integer $timeout
* @param int $timeout
*
* @return $this
*/
public function withTimeout($timeout)
{
$this->timeout = $timeout;
return $this->withConnectionTimeout($timeout);
}

/**
* @param int $timeout
*
* @return $this
*/
public function withConnectionTimeout($timeout)
{
$this->connectionTimeout = $timeout;

return $this;
}

/**
* @param int $timeout
*
* @return $this
*/
public function withRequestTimeout($timeout)
{
$this->requestTimeout = $timeout;

return $this;
}
Expand Down Expand Up @@ -268,7 +301,8 @@ public function execute($payload, array $headers = [])
curl_setopt_array($ch, [
CURLOPT_URL => trim($this->url),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => $this->timeout,
CURLOPT_TIMEOUT => $this->requestTimeout,
CURLOPT_CONNECTTIMEOUT => $this->connectionTimeout,
CURLOPT_MAXREDIRS => 2,
CURLOPT_SSL_VERIFYPEER => $this->verifySslCertificate,
CURLOPT_POST => true,
Expand Down Expand Up @@ -334,7 +368,7 @@ protected function buildContext($payload, array $headers = [])
'http' => [
'method' => 'POST',
'protocol_version' => 1.1,
'timeout' => $this->timeout,
'timeout' => $this->connectionTimeout,
'max_redirects' => 2,
'header' => implode("\r\n", $headers),
'content' => $payload,
Expand Down
22 changes: 12 additions & 10 deletions tests/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@

require_once __DIR__.'/../vendor/autoload.php';

define ('CURLOPT_URL', 10002);
define ('CURLOPT_RETURNTRANSFER', 19913);
define ('CURLOPT_CONNECTTIMEOUT', 78);
define ('CURLOPT_MAXREDIRS', 68);
define ('CURLOPT_SSL_VERIFYPEER', 64);
define ('CURLOPT_POST', 47);
define ('CURLOPT_POSTFIELDS', 10015);
define ('CURLOPT_HTTPHEADER', 10023);
define ('CURLOPT_HEADERFUNCTION', 20079);
define ('CURLOPT_CAINFO', 10065);
defined('CURLOPT_URL') || define('CURLOPT_URL', 10002);
defined('CURLOPT_RETURNTRANSFER') || define('CURLOPT_RETURNTRANSFER', 19913);
defined('CURLOPT_CONNECTTIMEOUT') || define('CURLOPT_CONNECTTIMEOUT', 78);
defined('CURLOPT_TIMEOUT') || define('CURLOPT_TIMEOUT', 13);
defined('CURLOPT_MAXREDIRS') || define('CURLOPT_MAXREDIRS', 68);
defined('CURLOPT_SSL_VERIFYPEER') || define('CURLOPT_SSL_VERIFYPEER', 64);
defined('CURLOPT_POST') || define('CURLOPT_POST', 47);
defined('CURLOPT_POSTFIELDS') || define('CURLOPT_POSTFIELDS', 10015);
defined('CURLOPT_HTTPHEADER') || define('CURLOPT_HTTPHEADER', 10023);
defined('CURLOPT_HEADERFUNCTION') || define('CURLOPT_HEADERFUNCTION', 20079);
defined('CURLOPT_CAINFO') || define('CURLOPT_CAINFO', 10065);

function extension_loaded($extension) {
return HttpClientTest::$functions->extension_loaded($extension);
Expand Down Expand Up @@ -173,6 +174,7 @@ public function testWithCurl()
CURLOPT_URL => 'url',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_TIMEOUT => 60,
CURLOPT_MAXREDIRS => 2,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_POST => true,
Expand Down

0 comments on commit 95ff5a2

Please sign in to comment.