Skip to content

Commit

Permalink
Added new config option to disable/enable local rate limit (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentin Schmick authored Aug 10, 2022
1 parent 0b84595 commit d253254
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
1 change: 1 addition & 0 deletions config/shipengine.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
'base' => env('SHIP_ENGINE_ENDPOINT', 'https://api.shipengine.com/'),
],
'request_limit_per_minute' => env('SHIP_ENGINE_REQUEST_LIMIT_PER_MINUTE', 200),
'use_local_rate_limit' => env('SHIP_ENGINE_USE_LOCAL_RATE_LIMIT', false),
'retries' => env('SHIP_ENGINE_RETRIES', 1),
'response' => [
'as_object' => env('SHIP_ENGINE_RESPONSE_AS_OBJECT', false),
Expand Down
22 changes: 12 additions & 10 deletions src/ShipEngineClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,19 +296,21 @@ private static function responseIsRateLimit(array $response) : bool

private static function incrementRequestCount(ShipEngineConfig $config) : void
{
$lock = tap(Cache::lock('shipengine.api-request.lock', 10))->block(10);
if ($config->useLocalRateLimit) {
$lock = tap(Cache::lock('shipengine.api-request.lock', 10))->block(10);

try {
$count = Cache::get('shipengine.api-request.count', 0);
$nextExpire = now()->seconds(0)->addMinute();
try {
$count = Cache::get('shipengine.api-request.count', 0);
$nextExpire = now()->seconds(0)->addMinute();

if ($count > $config->requestLimitPerMinute) {
throw new RateLimitExceededException(retryAfter: new DateInterval('PT1S'), message: 'Internal config API rate limit of ' . $config->requestLimitPerMinute . ' exceeded.');
}
if ($count > $config->requestLimitPerMinute) {
throw new RateLimitExceededException(retryAfter: new DateInterval('PT1S'), message: 'Internal config API rate limit of ' . $config->requestLimitPerMinute . ' exceeded.');
}

Cache::put('shipengine.api-request.count', $count + 1, $nextExpire);
} finally {
$lock->release();
Cache::put('shipengine.api-request.count', $count + 1, $nextExpire);
} finally {
$lock->release();
}
}
}
}
3 changes: 3 additions & 0 deletions src/ShipEngineConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ final class ShipEngineConfig implements \JsonSerializable, Arrayable

public bool $asObject = false;

public bool $useLocalRateLimit = false;

public DateInterval $timeout;

public DateInterval $timeoutTotal;
Expand Down Expand Up @@ -60,6 +62,7 @@ public function __construct(array $config = [])
$assert->isTimeoutValid($timeout_total_value);
$this->timeoutTotal = $timeout_total_value;

$this->useLocalRateLimit = boolval($config['use_local_rate_limit'] ?? config('shipengine.use_local_rate_limit', false));
$this->asObject = boolval($config['asObject'] ?? config('shipengine.response.as_object', false));

$this->baseUrl = $config['baseUrl'] ?? self::getBaseUri();
Expand Down

0 comments on commit d253254

Please sign in to comment.