Making WordPress.org


Ignore:
Timestamp:
05/31/2019 06:33:07 AM (5 years ago)
Author:
dd32
Message:

Plugin Directory: Search: Tweak the 'search stampede' code to allow two concurrent search requests.

Currently the locked-with-no-stale-response warning is triggered far too often, this relaxes it a bit while hopefully not overloading things.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/libs/site-search/jetpack-search.php

    r8897 r8901  
    230230        $response = wp_cache_get( $cache_key, self::CACHE_GROUP );
    231231
    232         // Use a temporary lock to prevent cache stampedes. This ensures only one process (per search term per memcache instance) will run the remote post.
     232
     233        // Use a temporary lock to prevent cache stampedes.
     234        // This will ensure that a maximum of two processes are performing the search, or one when the stale value is still known.
    233235        // Other processes will use the stale cached value if it's present, even for a while after the expiration time if a fresh value is still being fetched.
     236        $do_fresh_request = false;
    234237        if ( wp_cache_add( $lock_key, 1, self::CACHE_GROUP, 15 ) ) {
     238            $do_fresh_request = true;
     239        } elseif ( ! $response && 2 === wp_cache_incr( $lock_key, 1, self::CACHE_GROUP ) ) {
     240            // If we don't have cached data, this is the second request and, error volume is low, still perform the request.
     241            $do_fresh_request = $this->error_volume_is_low();
     242        }
     243
     244        if ( $do_fresh_request ) {
    235245            // Keep track of the number of concurrent requests for this key
    236246            wp_cache_set( $count_key, 1, self::CACHE_GROUP, 60 );
     
    257267
    258268                // Lock further requests for the same search for 3-7 seconds. We probably don't need anything more complex like exponential backoff here because this is per search.
    259                 wp_cache_set( $lock_key, 1, self::CACHE_GROUP, mt_rand( 3, 7 ) );
     269                wp_cache_set( $lock_key, 2, self::CACHE_GROUP, mt_rand( 3, 7 ) );
    260270
    261271                if ( is_wp_error( $request ) ) {
Note: See TracChangeset for help on using the changeset viewer.