Skip to content

Commit

Permalink
实现重试回调 (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft authored Nov 15, 2022
1 parent fc11816 commit 0976581
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 12 deletions.
16 changes: 14 additions & 2 deletions src/HttpRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ class HttpRequest
*/
public $retry = 0;

/**
* 重试回调.
*
* @var callable|null
*/
public $retryCallback = null;

/**
* 是否使用代理,默认false.
*
Expand Down Expand Up @@ -267,6 +274,7 @@ public function open($options = [])
{
$this->handler = YurunHttp::getHandler($options);
$this->retry = 0;
$this->retryCallback = null;
$this->headers = $this->options = [];
$this->url = $this->content = '';
$this->useProxy = false;
Expand Down Expand Up @@ -639,13 +647,15 @@ public function ua($userAgent)
/**
* 设置失败重试次数,状态码为5XX或者0才需要重试.
*
* @param string $retry
* @param int $retry
* @param callable|null $callback
*
* @return static
*/
public function retry($retry)
public function retry($retry, $callback = null)
{
$this->retry = $retry < 0 ? 0 : $retry; //至少请求1次,即重试0次
$this->retryCallback = $callback;

return $this;
}
Expand Down Expand Up @@ -938,6 +948,8 @@ public function buildRequest($url = null, $requestBody = null, $method = null, $
->withAttribute(Attributes::UPLOAD_SPEED, $this->uploadSpeed)
->withAttribute(Attributes::FOLLOW_LOCATION, $this->followLocation)
->withAttribute(Attributes::CONNECTION_POOL, $this->connectionPool)
->withAttribute(Attributes::RETRY, $this->retry)
->withAttribute(Attributes::RETRY_CALLBACK, $this->retryCallback)
->withProtocolVersion($this->protocolVersion)
;
foreach ($this->proxy as $name => $value)
Expand Down
5 changes: 5 additions & 0 deletions src/YurunHttp/Attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ abstract class Attributes
*/
const RETRY = 'retry';

/**
* 重试回调.
*/
const RETRY_CALLBACK = 'retry_callback';

/**
* 下载文件保存路径.
*/
Expand Down
21 changes: 16 additions & 5 deletions src/YurunHttp/Handler/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ public function send(&$request)
$isLocation = false;
$statusCode = 0;
$redirectCount = 0;
$retry = $request->getAttribute(Attributes::RETRY, 0);
$retryCallback = $request->getAttribute(Attributes::RETRY_CALLBACK);
$beginTime = microtime(true);
do
{
Expand All @@ -201,19 +203,28 @@ public function send(&$request)
$bodyContent = false;
}
$this->buildCurlHandlerEx($request, $handler, $uri, $method, $bodyContent);
$retry = $request->getAttribute(Attributes::RETRY, 0);
$result = null;
for ($i = 0; $i <= $retry; ++$i)
{
$receiveHeaders = [];
$curlResult = curl_exec($handler);
$this->result = $this->getResponse($request, $handler, $curlResult, $receiveHeaders);
$result = &$this->result;
$statusCode = $result->getStatusCode();
// 状态码为5XX或者0才需要重试
if (!(0 === $statusCode || (5 === (int) ($statusCode / 100))))
if ($retryCallback)
{
break;
if ($retryCallback($request, $result, $i))
{
break;
}
}
else
{
$statusCode = $result->getStatusCode();
// 状态码为5XX或者0才需要重试
if (!(0 === $statusCode || (5 === (int) ($statusCode / 100))))
{
break;
}
}
}
if ($request->getAttribute(Attributes::FOLLOW_LOCATION, true) && ($statusCode >= 300 && $statusCode < 400) && $result && '' !== ($location = $result->getHeaderLine('location')))
Expand Down
21 changes: 16 additions & 5 deletions src/YurunHttp/Handler/Swoole.php
Original file line number Diff line number Diff line change
Expand Up @@ -407,13 +407,24 @@ public function recvDefer($request, $timeout = null)
}
$result = &$this->result;
$statusCode = $result->getStatusCode();
// 状态码为5XX或者0才需要重试
if ((0 === $statusCode || (5 === (int) ($statusCode / 100))) && $retryCount < $request->getAttribute(Attributes::RETRY, 0))
if ($retryCount < $request->getAttribute(Attributes::RETRY, 0))
{
$request = $request->withAttribute(Attributes::RETRY, ++$retryCount);
$deferRequest = $this->sendDefer($request);
if ($retryCallback = $request->getAttribute(Attributes::RETRY_CALLBACK))
{
$retry = !$retryCallback($request, $result, $retryCount);
}
else
{
// 状态码为5XX或者0才需要重试
$retry = (0 === $statusCode || (5 === (int) ($statusCode / 100)));
}
if ($retry)
{
$request = $request->withAttribute(Attributes::PRIVATE_RETRY_COUNT, ++$retryCount);
$deferRequest = $this->sendDefer($request);

return $this->recvDefer($deferRequest, $timeout);
return $this->recvDefer($deferRequest, $timeout);
}
}
if (!$isWebSocket && $statusCode >= 300 && $statusCode < 400 && $request->getAttribute(Attributes::FOLLOW_LOCATION, true) && '' !== ($location = $result->getHeaderLine('location')))
{
Expand Down

0 comments on commit 0976581

Please sign in to comment.