Skip to content

Commit

Permalink
Added ExponentialBackoffErrorHandler for Http and Soap connectors.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilge committed Jul 19, 2016
1 parent 97f496a commit 629f1c1
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 80 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"php": ">=5.5",
"scriptfusion/mapper": "^1|^0",
"scriptfusion/static-class": "^1",
"codelegance/retry": "^1",
"scriptfusion/retry-error-handlers": "^1",
"psr/cache": "^1",
"zendframework/zend-uri": "^2"
},
Expand Down
133 changes: 84 additions & 49 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 16 additions & 18 deletions src/Porter/Cache/CacheAdvice.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,23 @@
*/
final class CacheAdvice extends AbstractEnumeration
{
const
/**
* @var string An object should be cached if a cache is available.
*/
SHOULD_CACHE = 'SHOULD_CACHE',
/**
* @var string An object should be cached if a cache is available.
*/
const SHOULD_CACHE = 'SHOULD_CACHE';

/**
* @var string An object should not be cached even if a cache is available.
*/
SHOULD_NOT_CACHE = 'SHOULD_NOT_CACHE',
/**
* @var string An object should not be cached even if a cache is available.
*/
const SHOULD_NOT_CACHE = 'SHOULD_NOT_CACHE';

/**
* @var string An object must be cached otherwise a fatal error may occur.
*/
MUST_CACHE = 'MUST_CACHE',
/**
* @var string An object must be cached otherwise a fatal error may occur.
*/
const MUST_CACHE = 'MUST_CACHE';

/**
* @var string An object must not be cached otherwise a fatal error may occur.
*/
MUST_NOT_CACHE = 'MUST_NOT_CACHE'
;
/**
* @var string An object must not be cached otherwise a fatal error may occur.
*/
const MUST_NOT_CACHE = 'MUST_NOT_CACHE';
}
10 changes: 10 additions & 0 deletions src/Porter/Net/Http/HttpConnectionException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
namespace ScriptFUSION\Porter\Net\Http;

/**
* The exception that is thrown when an HTTP connection error occurs.
*/
class HttpConnectionException extends \RuntimeException
{
// Intentionally empty.
}
27 changes: 17 additions & 10 deletions src/Porter/Net/Http/HttpConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use ScriptFUSION\Porter\Connector\CachingConnector;
use ScriptFUSION\Porter\Net\UrlBuilder;
use ScriptFUSION\Porter\Options\EncapsulatedOptions;
use ScriptFUSION\Retry\ErrorHandler\ExponentialBackoffErrorHandler;

class HttpConnector extends CachingConnector
{
Expand All @@ -26,16 +27,22 @@ public function fetchFreshData($source, EncapsulatedOptions $options = null)
throw new \RuntimeException('Options must be an instance of HttpOptions.');
}

return file_get_contents(
$this->getOrCreateUrlBuilder()->buildUrl($source, $options ? $options->getQueryParameters() : []),
false,
stream_context_create([
'http' => array_merge(
$this->options->extractHttpContextOptions(),
$options ? $options->extractHttpContextOptions() : []
),
])
);
return \ScriptFUSION\Retry\retry(5, function () use ($source, $options) {
if (false === $response = @file_get_contents(
$this->getOrCreateUrlBuilder()->buildUrl($source, $options ? $options->getQueryParameters() : []),
false,
stream_context_create([
'http' => array_merge(
$this->options->extractHttpContextOptions(),
$options ? $options->extractHttpContextOptions() : []
),
])
)) {
throw new HttpConnectionException(error_get_last()['message']);
}

return $response;
}, new ExponentialBackoffErrorHandler);
}

private function getOrCreateUrlBuilder()
Expand Down
5 changes: 3 additions & 2 deletions src/Porter/Net/Soap/SoapConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use ScriptFUSION\Porter\Connector\CachingConnector;
use ScriptFUSION\Porter\Options\EncapsulatedOptions;
use ScriptFUSION\Porter\Type\ObjectType;
use ScriptFUSION\Retry\ErrorHandler\ExponentialBackoffErrorHandler;

class SoapConnector extends CachingConnector
{
Expand All @@ -28,9 +29,9 @@ public function fetchFreshData($source, EncapsulatedOptions $options = null)
$params = array_merge($this->options->getParameters(), $options ? $options->getParameters() : []);

return ObjectType::toArray(
\igorw\retry(5, function () use ($source, $params) {
\ScriptFUSION\Retry\retry(5, function () use ($source, $params) {
return $this->client->$source($params);
})
}, new ExponentialBackoffErrorHandler)
);
}
}

0 comments on commit 629f1c1

Please sign in to comment.