Skip to content

Commit 6d8e296

Browse files
authored
Merge pull request #2164 from skaut/max-batch-size
Limiting Google API batch requests to 100 individual requests per batch
2 parents 21e9bcb + 0f88568 commit 6d8e296

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/php/class-api-client.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ public static function async_request( $request, $transform, $rejection_handler =
170170
throw new Internal_Exception();
171171
}
172172

173+
self::check_batch_size();
173174
$key = wp_rand();
174175
// @phan-suppress-next-line PhanPossiblyNonClassMethodCall
175176
self::$current_batch->add( $request, $key );
@@ -202,6 +203,7 @@ public static function async_paginated_request(
202203
$pagination_helper,
203204
$rejection_handler = null
204205
) {
206+
self::check_batch_size();
205207
/**
206208
* Gets one page.
207209
*
@@ -278,6 +280,20 @@ public static function execute( $promises = array() ) {
278280
return Utils::all( $promises )->wait();
279281
}
280282

283+
/**
284+
* Google API has an upper limit on batch size of 100 requests. Should a batch be bigger than that, this function executes it and prepares a new batch for the rest of the requests
285+
*
286+
* @return void
287+
*/
288+
private static function check_batch_size() {
289+
if ( count( self::$pending_requests ) < 100 ) {
290+
return;
291+
}
292+
293+
self::execute_current_batch();
294+
self::initialize_batch();
295+
}
296+
281297
/**
282298
* Executes the current batch request and calls its callbacks
283299
*

0 commit comments

Comments
 (0)