diff --git a/inc/namespace.php b/inc/namespace.php index 88e239c..c99b49a 100644 --- a/inc/namespace.php +++ b/inc/namespace.php @@ -208,6 +208,9 @@ function load_elasticpress() { // Handle autosuggest requests. add_action( 'template_redirect', __NAMESPACE__ . '\\handle_autosuggest_endpoint' ); + // Ease the pressure on ES during bulk indexing requests. + add_action( 'ep_remote_request', __NAMESPACE__ . '\\throttle_bulk_index_requests', 10, 2 ); + // Set up packages feature. Packages\bootstrap(); } @@ -827,6 +830,24 @@ function setup_elasticpress_on_install() { WP_CLI::line( WP_CLI::colorize( '%GElasticPress configured.%n' ) ); } +/** + * Throttle how quickly bulk indexing requests are sent to ES. + * + * In some cases "Too many requests" 429 errors come up when the bulk + * thread pool is full or not being processed quickly enough. + * + * @param array $query Remote request arguments. + * @param string $type Request type. + * @return void + */ +function throttle_bulk_index_requests( $query, $type ) : void { + if ( $type !== 'bulk_index' ) { + return; + } + + sleep( defined( 'ALTIS_BULK_INDEX_INTERVAL' ) ? absint( ALTIS_BULK_INDEX_INTERVAL ) : 1 ); +} + /** * Return the correct analyzer language based on the sites configured language code. *