@@ -77,9 +77,15 @@ class Client
77
77
/** @var int Max number of retries if we get 429 errors */
78
78
public $ maxAttempts = 10 ;
79
79
80
+ /** @var int Max number of retries if we get 5XX errors */
81
+ public $ maxAttemptsOnServerError = 1 ;
82
+
80
83
/** @var float Number of seconds to sleep before retrying */
81
84
public $ sleepTimeOnRetry = 0.5 ;
82
85
86
+ /** @var float Number of seconds to sleep before retrying after a server error */
87
+ public $ sleepTimeOnServerError = 10 ;
88
+
83
89
/**
84
90
* Create a new client to connect to a given Alma instance.
85
91
*
@@ -241,12 +247,14 @@ public function request(RequestInterface $request, $attempt = 1)
241
247
return $ this ->http ->sendRequest ($ request );
242
248
} catch (HttpException $ e ) {
243
249
// Thrown for 400 and 500 level errors.
250
+ $ statusCode = $ e ->getResponse ()->getStatusCode ();
251
+
244
252
$ error = $ this ->parseClientError ($ e );
245
253
246
254
if ($ error ->getErrorCode () === 'PER_SECOND_THRESHOLD ' ) {
247
255
// We've run into the "Max 25 API calls per institution per second" limit.
248
256
// Wait a sec and retry, unless we've tried too many times already.
249
- if ($ attempt > $ this ->maxAttempts ) {
257
+ if ($ attempt >= $ this ->maxAttempts ) {
250
258
throw new MaxNumberOfAttemptsExhausted (
251
259
'Rate limiting error - max number of retry attempts exhausted. ' ,
252
260
0 ,
@@ -258,6 +266,15 @@ public function request(RequestInterface $request, $attempt = 1)
258
266
return $ this ->request ($ request , $ attempt + 1 );
259
267
}
260
268
269
+ if ($ statusCode >= 500 && $ statusCode < 600 ) {
270
+ if ($ attempt >= $ this ->maxAttemptsOnServerError ) {
271
+ throw $ error ;
272
+ }
273
+ time_nanosleep (0 , $ this ->sleepTimeOnServerError * 1000000000 );
274
+
275
+ return $ this ->request ($ request , $ attempt + 1 );
276
+ }
277
+
261
278
// Throw exception for other errors
262
279
throw $ error ;
263
280
} catch (NetworkException $ e ) {
0 commit comments