Skip to content

Commit

Permalink
Add backoff behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
eljam committed May 11, 2016
1 parent d1a78ac commit 2312d39
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ This will create an yml file with this configuration:

```yaml
urls:
google:
google: #this has to be unique
url: 'https://www.google.fr'
method: GET
headers: { Accept: text/html }
Expand Down
10 changes: 10 additions & 0 deletions src/Client/GuzzleClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\MessageFormatter;
use GuzzleHttp\Middleware;
use Hogosha\Monitor\Middleware\Backoff;
use Hogosha\Monitor\Monitor;
use Symfony\Component\Console\Logger\ConsoleLogger;
use Webmozart\Console\Adapter\IOOutput;
use Webmozart\Console\Api\IO\IO;
Expand Down Expand Up @@ -47,9 +50,16 @@ public static function createClient(IO $io, $options = [])

$stack->push($middleware, 'logger');

$stack->push(
Middleware::retry(Backoff::decider(), Backoff::delay())
);

$defaults = [
'handler' => $stack,
'allow_redirects' => false,
'headers' => [
'User-Agent' => sprintf('Irongate Monitor %s', Monitor::VERSION),
],
];

$options = array_merge($defaults, $options);
Expand Down
60 changes: 60 additions & 0 deletions src/Middleware/Backoff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Hogosha\Monitor\Middleware;

use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;

/**
* Class Backoff.
*/
class Backoff
{
/**
* decider.
*
* @return callable
*/
public static function decider()
{
return function (
$retries,
Request $request,
Response $response = null,
RequestException $exception = null
) {
// Limit to 5 retry max
if ($retries >= 3) {
return false;
}

// Retry connection exceptions
if ($exception instanceof ConnectException) {
return true;
}

if ($response) {
// Retry if we have a serve error
if ($response->getStatusCode() >= 500) {
return true;
}
}

return false;
};
}

/**
* delay.
*
* @return callable
*/
public static function delay()
{
return function ($numberOfRetries) {
return 1000 * $numberOfRetries;
};
}
}
3 changes: 2 additions & 1 deletion src/Runner/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ public function run()
$handlerError = curl_strerror($tranferStats->getHandlerErrorData());
}

$resultCollection->append(
$resultCollection->offsetSet(
$url->getName(),
(new Result(
$url,
$statusCode,
Expand Down
4 changes: 2 additions & 2 deletions tests/Monitor/Runner/RunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public function testRunner()
$this->assertCount(1, $urlProvider->getUrls());
$this->assertInstanceOf(UrlInfo::class, $urlProvider->getUrls()['google']);
$this->assertInstanceOf(ResultCollection::class, $resultCollection);
$this->assertInstanceOf(Result::class, $resultCollection[0]);
$this->assertEquals((new Result($this->createUrlInfo(), 200, 0, null, true)), $resultCollection[0]);
$this->assertInstanceOf(Result::class, $resultCollection['google']);
$this->assertEquals((new Result($this->createUrlInfo(), 200, 0, null, true)), $resultCollection['google']);
}

/**
Expand Down

0 comments on commit 2312d39

Please sign in to comment.