Skip to content
This repository has been archived by the owner on Nov 10, 2023. It is now read-only.

Prevent sites from making too many API requests #11

Merged
merged 14 commits into from
Aug 23, 2022
Merged

Conversation

desrosj
Copy link
Contributor

@desrosj desrosj commented Aug 23, 2022

Proposed changes

This adds a throttle transient that will prevent sites from hitting the API too many times.

Type of Change

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation Update (if none of the other choices apply)

Checklist

  • I have read the CONTRIBUTING doc
  • Linting and tests pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

Further comments

$throttle = Transient::get( self::THROTTLE );
$retry_count = (int) \get_option( self::RETRY_COUNT, 0 );

if ( false !== $throttle || $retry_count >= 10 ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be

Suggested change
if ( false !== $throttle || $retry_count >= 10 ) {
if ( true === $throttle || $retry_count >= 10 ) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to return if the transient is not set, which will return explicit false. When it exists, the value (1) will be returned.

@@ -123,6 +137,13 @@ public static function connect( $path ) {
return $provided;
}

$throttle = Transient::get( self::THROTTLE );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need a throttle and a retry count? Can't we just use retry count?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're using the transient to track when to retry, and the retry count to prevent excessive retries.

$timeout = HOURS_IN_SECONDS * $retry_count;
}

Transient::set( self::THROTTLE, 1, $timeout );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This gets set, but never unset.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The THROTTLE transient is basically a blocker that is meant to only self-expire. We shouldn't need to delete it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't need to delete this. It can just expire which would allow the site to retry eventually.

@wpscholar wpscholar merged commit 7e23128 into main Aug 23, 2022
@wpscholar wpscholar deleted the fix/too-many-retries branch August 23, 2022 15:09
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants