-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
75 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters