← All changes
|
jetpack_vendor/automattic/jetpack-sync/src/class-rest-sender.php
+17
-9
13.7.2
→
16.3-beta
View file →
| @@ -25,11 +25,11 @@ | ||
| 25 | 25 | |
| 26 | 26 | /** |
| 27 | 27 | * Checkout objects from the queue |
| 28 | 28 | * |
| 29 | - * @param string $queue_name Name of Queue. | |
| 30 | - * @param int $number_of_items Number of Items. | |
| 31 | - * @param array $args arguments. | |
| 29 | + * @param string $queue_name Name of Queue. | |
| 30 | + * @param int|null $number_of_items Number of Items. Null for memory-based checkout. | |
| 31 | + * @param array $args Request arguments. Supports 'pop', 'force', 'encode', and 'use_memory_limit'. | |
| 32 | 32 | * |
| 33 | 33 | * @return array|WP_Error |
| 34 | 34 | */ |
| 35 | 35 | public function queue_pull( $queue_name, $number_of_items, $args ) { |
| @@ -61,9 +61,9 @@ | ||
| 61 | 61 | if ( ! is_object( $buffer ) ) { |
| 62 | 62 | return new WP_Error( 'buffer_non-object', 'Buffer is not an object', 400 ); |
| 63 | 63 | } |
| 64 | 64 | |
| 65 | - $encode = isset( $args['encode'] ) ? $args['encode'] : true; | |
| 65 | + $encode = $args['encode'] ?? true; | |
| 66 | 66 | |
| 67 | 67 | Settings::set_is_syncing( true ); |
| 68 | 68 | list( $items_to_send, $skipped_items_ids ) = $sender->get_items_to_send( $buffer, $encode ); |
| 69 | 69 | Settings::set_is_syncing( false ); |
| @@ -116,24 +116,32 @@ | ||
| 116 | 116 | |
| 117 | 117 | /** |
| 118 | 118 | * Checkout items out of the sync queue. |
| 119 | 119 | * |
| 120 | - * @param Queue $queue Sync Queue. | |
| 121 | - * @param int $number_of_items Number of items to checkout. | |
| 120 | + * @param Queue $queue Sync Queue. | |
| 121 | + * @param int|null $number_of_items Number of items to checkout. When null, uses memory-based checkout with default settings. | |
| 122 | 122 | * |
| 123 | - * @return WP_Error | |
| 123 | + * @return Queue_Buffer|WP_Error | |
| 124 | 124 | */ |
| 125 | 125 | protected function get_buffer( $queue, $number_of_items ) { |
| 126 | 126 | $start = time(); |
| 127 | 127 | $max_duration = 5; // this will try to get the buffer. |
| 128 | 128 | |
| 129 | - $buffer = $queue->checkout( $number_of_items ); | |
| 129 | + $use_memory_limit = null === $number_of_items; | |
| 130 | + $memory_limit = $use_memory_limit ? Settings::get_setting( 'dequeue_max_bytes' ) : null; | |
| 131 | + $max_rows = $use_memory_limit ? Settings::get_setting( 'upload_max_rows' ) : null; | |
| 132 | + | |
| 133 | + $buffer = $use_memory_limit | |
| 134 | + ? $queue->checkout_with_memory_limit( $memory_limit, $max_rows ) | |
| 135 | + : $queue->checkout( $number_of_items ); | |
| 130 | 136 | $duration = time() - $start; |
| 131 | 137 | |
| 132 | 138 | while ( is_wp_error( $buffer ) && $duration < $max_duration ) { |
| 133 | 139 | sleep( 2 ); |
| 134 | 140 | $duration = time() - $start; |
| 135 | - $buffer = $queue->checkout( $number_of_items ); | |
| 141 | + $buffer = $use_memory_limit | |
| 142 | + ? $queue->checkout_with_memory_limit( $memory_limit, $max_rows ) | |
| 143 | + : $queue->checkout( $number_of_items ); | |
| 136 | 144 | } |
| 137 | 145 | |
| 138 | 146 | if ( false === $buffer ) { |
| 139 | 147 | return new WP_Error( 'queue_size', 'The queue is empty and there is nothing to send', 400 ); |