Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature idea - throttling rate limiter plugin #973

Open
ondrejmirtes opened this issue Mar 2, 2021 · 0 comments
Open

Feature idea - throttling rate limiter plugin #973

ondrejmirtes opened this issue Mar 2, 2021 · 0 comments
Labels
feature-request good first issue Ideal for your first contribution!

Comments

@ondrejmirtes
Copy link

ondrejmirtes commented Mar 2, 2021

Something like this would come in handy:

<?php declare(strict_types = 1);

use Github\Api\RateLimit\RateLimitResource;
use Github\Client;
use Http\Client\Common\Plugin;
use Http\Promise\Promise;
use Psr\Http\Message\RequestInterface;

class RateLimitPlugin implements Plugin
{

	private Client $client;

	public function setClient(Client $client): void
	{
		$this->client = $client;
	}

	public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise
	{
		$path = $request->getUri()->getPath();
		if ($path === '/rate_limit') {
			return $next($request);
		}

		/** @var RateLimitResource $resource */
		$resource = $this->client->api('rate_limit')->getResource('core');
		if ($resource->getRemaining() < 10) {
			$reset = $resource->getReset();
			$sleepFor = $reset - time();
			if ($sleepFor > 0) {
				sleep($sleepFor);
			}
		}

		return $next($request);
	}

}
@acrobat acrobat added feature-request good first issue Ideal for your first contribution! labels Mar 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request good first issue Ideal for your first contribution!
Projects
None yet
Development

No branches or pull requests

2 participants