Skip to content

Commit

Permalink
Merge pull request #508 from picqer/wait-on-minutely-rate-limit-hit
Browse files Browse the repository at this point in the history
Add basic rate limit sleep support
  • Loading branch information
stephangroen authored Oct 20, 2021
2 parents 8b92410 + 45e476b commit f122c25
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ $connection->getMinutelyLimitReset(); // Retrieve the timestamp for when the min
```
_Do note when you have no more minutely calls available, Exact only sends the Minutely Limit headers. So in that case, the Daily Limit headers will remain 0 until the minutely reset rolls over._

There is basic support to `sleep` upon hitting the minutely rate limits. If you enable "Wait on minutely rate limit hit", the client will sleep until the limit is reset. Daily limits are not considered.

```php
$connection->setWaitOnMinutelyRateLimitHit(true);
```

### Use the library to do stuff (examples)

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
],
"require": {
"php": ">=7.2.0",
"guzzlehttp/guzzle": "~6.0|~7.0"
"guzzlehttp/guzzle": "~6.0|~7.0",
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "^8.5|^9.3"
Expand Down
29 changes: 29 additions & 0 deletions src/Picqer/Financials/Exact/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ class Connection
*/
protected $minutelyLimitReset;

/**
* @var bool
*/
private $waitOnMinutelyRateLimitHit = false;

/**
* @return Client
*/
Expand Down Expand Up @@ -266,6 +271,7 @@ private function createRequest($method, $endpoint, $body = null, array $params =
*/
public function get($url, array $params = [], array $headers = [])
{
$this->waitIfMinutelyRateLimitHit();
$url = $this->formatUrl($url, $url !== 'current/Me', $url == $this->nextUrl);

try {
Expand All @@ -289,6 +295,7 @@ public function get($url, array $params = [], array $headers = [])
*/
public function post($url, $body)
{
$this->waitIfMinutelyRateLimitHit();
$url = $this->formatUrl($url);

try {
Expand Down Expand Up @@ -334,6 +341,7 @@ public function upload($topic, $body)
*/
public function put($url, $body)
{
$this->waitIfMinutelyRateLimitHit();
$url = $this->formatUrl($url);

try {
Expand All @@ -356,6 +364,7 @@ public function put($url, $body)
*/
public function delete($url)
{
$this->waitIfMinutelyRateLimitHit();
$url = $this->formatUrl($url);

try {
Expand Down Expand Up @@ -870,4 +879,24 @@ private function extractRateLimits(Response $response)
$this->minutelyLimitRemaining = (int) $response->getHeaderLine('X-RateLimit-Minutely-Remaining');
$this->minutelyLimitReset = (int) $response->getHeaderLine('X-RateLimit-Minutely-Reset');
}

protected function waitIfMinutelyRateLimitHit()
{
if (! $this->waitOnMinutelyRateLimitHit) {
return;
}

$minutelyReset = $this->getMinutelyLimitReset();

if ($this->getMinutelyLimitRemaining() === 0 && $minutelyReset) {
// add a second for rounding differences
$resetsInSeconds = (($minutelyReset / 1000) - time()) + 1;
sleep($resetsInSeconds);
}
}

public function setWaitOnMinutelyRateLimitHit(bool $waitOnMinutelyRateLimitHit)
{
$this->waitOnMinutelyRateLimitHit = $waitOnMinutelyRateLimitHit;
}
}

0 comments on commit f122c25

Please sign in to comment.