@@ -83,6 +83,12 @@ class RequestWrapper
8383 */
8484 private $ retryFunction ;
8585
86+ /**
87+ * @var callable|null Lets the user listen for retries and
88+ * modify the next retry arguments
89+ */
90+ private $ retryListener ;
91+
8692 /**
8793 * @var callable Executes a delay.
8894 */
@@ -136,6 +142,8 @@ class RequestWrapper
136142 * determining how long to wait between attempts to retry. Function
137143 * signature should match: `function (int $attempt) : int`.
138144 * @type string $universeDomain The expected universe of the credentials. Defaults to "googleapis.com".
145+ * @type callable $restRetryListener A function to run custom logic between retries. This function can modify
146+ * the next server call arguments for the next retry.
139147 * }
140148 */
141149 public function __construct (array $ config = [])
@@ -151,6 +159,7 @@ public function __construct(array $config = [])
151159 'componentVersion ' => null ,
152160 'restRetryFunction ' => null ,
153161 'restDelayFunction ' => null ,
162+ 'restRetryListener ' => null ,
154163 'restCalcDelayFunction ' => null ,
155164 'universeDomain ' => GetUniverseDomainInterface::DEFAULT_UNIVERSE_DOMAIN ,
156165 ];
@@ -160,6 +169,7 @@ public function __construct(array $config = [])
160169 $ this ->restOptions = $ config ['restOptions ' ];
161170 $ this ->shouldSignRequest = $ config ['shouldSignRequest ' ];
162171 $ this ->retryFunction = $ config ['restRetryFunction ' ] ?: $ this ->getRetryFunction ();
172+ $ this ->retryListener = $ config ['restRetryListener ' ];
163173 $ this ->delayFunction = $ config ['restDelayFunction ' ] ?: function ($ delay ) {
164174 usleep ($ delay );
165175 };
@@ -362,7 +372,7 @@ private function applyHeaders(RequestInterface $request, array $options = [])
362372 */
363373 private function addAuthHeaders (RequestInterface $ request , FetchAuthTokenInterface $ fetcher )
364374 {
365- $ backoff = new ExponentialBackoff ($ this ->retries , $ this ->getRetryFunction ());
375+ $ backoff = new ExponentialBackoff ($ this ->retries , $ this ->getRetryFunction (), $ this -> retryListener );
366376
367377 try {
368378 return $ backoff ->execute (
@@ -485,7 +495,7 @@ private function getRetryOptions(array $options)
485495 : $ this ->retryFunction ,
486496 'retryListener ' => isset ($ options ['restRetryListener ' ])
487497 ? $ options ['restRetryListener ' ]
488- : null ,
498+ : $ this -> retryListener ,
489499 'delayFunction ' => isset ($ options ['restDelayFunction ' ])
490500 ? $ options ['restDelayFunction ' ]
491501 : $ this ->delayFunction ,
0 commit comments