Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ $handlerStack->push(
)
);

$handlerStack->push(
\HubSpot\RetryMiddlewareFactory::createConnectionErrorsMiddleware(
\HubSpot\Delay::getExponentialDelayFunction(2)
)
);

$client = new \GuzzleHttp\Client(['handler' => $handlerStack]);

$hubspot = \HubSpot\Factory::createWithAccessToken('your-access-token', $client);
Expand Down
46 changes: 46 additions & 0 deletions lib/RetryMiddlewareFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,31 @@

namespace HubSpot;

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

class RetryMiddlewareFactory
{
public const DEFAULT_MAX_RETRIES = 5;
public const TRANSIENT_CURL_ERROR_CODES = [52, 55, 56];
public const INTERNAL_ERROR_RANGES = [
['from' => 500, 'to' => 503],
['from' => 520, 'to' => 599],
];

public static function createConnectionErrorsMiddleware(
?callable $delayFunction = null,
int $maxRetries = self::DEFAULT_MAX_RETRIES,
array $curlErrorCodes = self::TRANSIENT_CURL_ERROR_CODES
): callable {
return Middleware::retry(
static::getRetryFunctionByConnectionErrors($maxRetries, $curlErrorCodes),
$delayFunction
);
}

public static function createInternalErrorsMiddleware(
?callable $delayFunction = null,
int $maxRetries = self::DEFAULT_MAX_RETRIES
Expand Down Expand Up @@ -131,4 +144,37 @@ public static function getRetryFunction(
return false;
};
}

public static function getRetryFunctionByConnectionErrors(
int $maxRetries = self::DEFAULT_MAX_RETRIES,
array $curlErrorCodes = self::TRANSIENT_CURL_ERROR_CODES
): callable {
return function (
$retries,
Request $request,
?Response $response = null,
$exception = null
) use ($maxRetries, $curlErrorCodes) {
if ($retries >= $maxRetries) {
return false;
}

if (!$exception instanceof ConnectException) {
return false;
}

$handlerContext = $exception->getHandlerContext();
$errno = $handlerContext['errno'] ?? null;

if (is_numeric($errno) && in_array((int) $errno, $curlErrorCodes, true)) {
return true;
}

if (preg_match('/cURL error\s+(\d+):/i', $exception->getMessage(), $matches) === 1) {
return in_array((int) $matches[1], $curlErrorCodes, true);
}

return false;
};
}
}
74 changes: 74 additions & 0 deletions tests/Unit/RetryMiddlewareFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Hubspot\Tests\Unit;

use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Psr7\Request;
use HubSpot\RetryMiddlewareFactory;
use PHPUnit\Framework\TestCase;

/**
* @internal
*
* @coversNothing
*/
class RetryMiddlewareFactoryTest extends TestCase
{
/** @test */
public function itRetriesRetriableConnectionErrorsByErrno(): void
{
$retry = RetryMiddlewareFactory::getRetryFunctionByConnectionErrors(3);
$request = new Request('GET', 'https://api.hubapi.com/test');
$exception = new ConnectException(
'cURL error 56: OpenSSL SSL_read unexpected eof while reading',
$request,
null,
['errno' => 56]
);

$this->assertTrue($retry(0, $request, null, $exception));
}

/** @test */
public function itRetriesRetriableConnectionErrorsByMessageWhenErrnoMissing(): void
{
$retry = RetryMiddlewareFactory::getRetryFunctionByConnectionErrors(3);
$request = new Request('GET', 'https://api.hubapi.com/test');
$exception = new ConnectException(
'cURL error 55: Send failure: Broken pipe',
$request
);

$this->assertTrue($retry(0, $request, null, $exception));
}

/** @test */
public function itDoesNotRetryNonRetriableConnectionErrors(): void
{
$retry = RetryMiddlewareFactory::getRetryFunctionByConnectionErrors(3);
$request = new Request('GET', 'https://api.hubapi.com/test');
$exception = new ConnectException(
'cURL error 60: SSL certificate problem',
$request,
null,
['errno' => 60]
);

$this->assertFalse($retry(0, $request, null, $exception));
}

/** @test */
public function itStopsRetryingWhenMaxRetriesReached(): void
{
$retry = RetryMiddlewareFactory::getRetryFunctionByConnectionErrors(1);
$request = new Request('GET', 'https://api.hubapi.com/test');
$exception = new ConnectException(
'cURL error 56: OpenSSL SSL_read unexpected eof while reading',
$request,
null,
['errno' => 56]
);

$this->assertFalse($retry(1, $request, null, $exception));
}
}
Loading