Making WordPress.org

Changeset 4840


Ignore:
Timestamp:
01/30/2017 04:36:01 PM (9 years ago)
Author:
iandunn
Message:

Official WordPress Events: Pause to avoid hitting API rate limits

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/official-wordpress-events/official-wordpress-events.php

    r4839 r4840  
    1818     * @todo
    1919     *
    20      * Add pausing to avoid rate limit
    2120     * Meetups only pulling 1 week instead of full month
    2221     * Maybe pull more than 1 month of meetups
    2322     * Make meetups and wordcamps cut off on the same date, so it doesn't look like there aren't any meetups later in the year
     23     * Always display cached results, and refresh stale cache asynchronously, to avoid visitors having to wait while the data is pulled
     24     * After async cache refresh, bump remote_get timeout limit to 30 to avoid premature timeouts
    2425     * Ability to feature a camp in a hero area
    2526     * Add a "load more" button that retrieves more events via AJAX and updates the DOM. Have each click load the next month of events?
     
    508509                    set_transient( $transient_key, $response, HOUR_IN_SECONDS );
    509510                }
     511
     512                $this->maybe_pause( wp_remote_retrieve_headers( $response ) );
    510513            }
    511514        }
    512515
    513516        return $response;
     517    }
     518
     519    /**
     520     * Maybe pause the script to avoid rate limiting
     521     *
     522     * @param array $headers
     523     */
     524    protected function maybe_pause( $headers ) {
     525        if ( ! isset( $headers['x-ratelimit-remaining'], $headers['x-ratelimit-reset'] ) ) {
     526            return;
     527        }
     528
     529        $remaining = absint( $headers['x-ratelimit-remaining'] );
     530        $period    = absint( $headers['x-ratelimit-reset'] );
     531
     532        // Pause more frequently than we need to, and for longer, just to be safe
     533        if ( $remaining > 2 ) {
     534            return;
     535        }
     536
     537        if ( $period < 2 ) {
     538            $period = 2;
     539        }
     540
     541        sleep( $period );
    514542    }
    515543}
Note: See TracChangeset for help on using the changeset viewer.