@@ -58,6 +58,13 @@ class HttpRequest
5858 */
5959 public $ retry = 0 ;
6060
61+ /**
62+ * 重试回调.
63+ *
64+ * @var callable|null
65+ */
66+ public $ retryCallback = null ;
67+
6168 /**
6269 * 是否使用代理,默认false.
6370 *
@@ -267,6 +274,7 @@ public function open($options = [])
267274 {
268275 $ this ->handler = YurunHttp::getHandler ($ options );
269276 $ this ->retry = 0 ;
277+ $ this ->retryCallback = null ;
270278 $ this ->headers = $ this ->options = [];
271279 $ this ->url = $ this ->content = '' ;
272280 $ this ->useProxy = false ;
@@ -639,13 +647,15 @@ public function ua($userAgent)
639647 /**
640648 * 设置失败重试次数,状态码为5XX或者0才需要重试.
641649 *
642- * @param string $retry
650+ * @param int $retry
651+ * @param callable|null $callback
643652 *
644653 * @return static
645654 */
646- public function retry ($ retry )
655+ public function retry ($ retry, $ callback = null )
647656 {
648657 $ this ->retry = $ retry < 0 ? 0 : $ retry ; //至少请求1次,即重试0次
658+ $ this ->retryCallback = $ callback ;
649659
650660 return $ this ;
651661 }
@@ -938,6 +948,8 @@ public function buildRequest($url = null, $requestBody = null, $method = null, $
938948 ->withAttribute (Attributes::UPLOAD_SPEED , $ this ->uploadSpeed )
939949 ->withAttribute (Attributes::FOLLOW_LOCATION , $ this ->followLocation )
940950 ->withAttribute (Attributes::CONNECTION_POOL , $ this ->connectionPool )
951+ ->withAttribute (Attributes::RETRY , $ this ->retry )
952+ ->withAttribute (Attributes::RETRY_CALLBACK , $ this ->retryCallback )
941953 ->withProtocolVersion ($ this ->protocolVersion )
942954 ;
943955 foreach ($ this ->proxy as $ name => $ value )
0 commit comments