Skip to content

Commit

Permalink
Throttling
Browse files Browse the repository at this point in the history
  • Loading branch information
johnwedgbury committed May 13, 2020
1 parent 46860e4 commit f8b0f36
Show file tree
Hide file tree
Showing 14 changed files with 196 additions and 28 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,35 @@ Product::create($data);
Product::delete($id)
```

# Throttling (Enabled by default)

*NOTE: THIS CAN CAUSE THREAD BLOCKING*

(https://help.shopwired.co.uk/api/introduction/api-rate-limiting)
> We use the leaky bucket algorithm as a controller.
>
>The leaky bucket algorithm allows for infrequent bursts of calls to the API, and allows your APP to continue to make an unlimited amount of calls over time.
>
> The bucket size is 40 and this cannot be exceeded at any given time. The leak rate is 2 calls per second that continually empty the bucket.
>
> If your APP averages 2 calls per second then it will never trip the 429 too many requests error.
>
> API calls will be processed almost instantaneously as long as there is room in your bucket. You can make quick bursts of API calls that exceed the leak rate as long as your average call rate does not exceed 2 calls per second.

This package, by default, will throttle your requests based on this algorithm and these parameters. This uses `sleep` to time requests once the limits are hit, will mean that thread may run slowly.

## Requirements

The throttling requires redis, to implement the limit between different sessions.

## Disable

Warning: Disabling the throttling may cause the request to fail with `429 too many requests`.

To disable the throttling use:

```
ShopWiredThrottle::disable();
```

48 changes: 24 additions & 24 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/Requests/All.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Appoly\ShopWiredPHPSDK\Requests;

use Appoly\ShopWiredPHPSDK\ShopWiredClient;
use Appoly\ShopWiredPHPSDK\ShopWiredThrottle;

trait All
{
Expand All @@ -14,6 +15,8 @@ trait All
*/
public static function all($options = [])
{
ShopWiredThrottle::throttle();

$shopwired_client = ShopWiredClient::get();

$response = $shopwired_client->get(static::$endpoint, [
Expand Down
3 changes: 3 additions & 0 deletions src/Requests/Count.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Appoly\ShopWiredPHPSDK\Requests;

use Appoly\ShopWiredPHPSDK\ShopWiredClient;
use Appoly\ShopWiredPHPSDK\ShopWiredThrottle;

trait Count
{
Expand All @@ -14,6 +15,8 @@ trait Count
*/
public static function count($options = [])
{
ShopWiredThrottle::throttle();

$shopwired_client = ShopWiredClient::get();

$response = $shopwired_client->get(static::$endpoint.'/count', [
Expand Down
3 changes: 3 additions & 0 deletions src/Requests/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Appoly\ShopWiredPHPSDK\Requests;

use Appoly\ShopWiredPHPSDK\ShopWiredClient;
use Appoly\ShopWiredPHPSDK\ShopWiredThrottle;

trait Create
{
Expand All @@ -14,6 +15,8 @@ trait Create
*/
public static function create($data)
{
ShopWiredThrottle::throttle();

$shopwired_client = ShopWiredClient::get();

$response = $shopwired_client->post(static::$endpoint, [
Expand Down
3 changes: 3 additions & 0 deletions src/Requests/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Appoly\ShopWiredPHPSDK\Requests;

use Appoly\ShopWiredPHPSDK\ShopWiredClient;
use Appoly\ShopWiredPHPSDK\ShopWiredThrottle;

trait Delete
{
Expand All @@ -13,6 +14,8 @@ trait Delete
*/
public static function delete($id)
{
ShopWiredThrottle::throttle();

$shopwired_client = ShopWiredClient::get();

$shopwired_client->delete(static::$endpoint.'/'.$id);
Expand Down
6 changes: 5 additions & 1 deletion src/Requests/Extending/SubAll.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ trait SubAll
public static function all($parent_id, $options = [])
{
$parent = new self::$extends;

$endpoint = self::$endpoint;
self::$endpoint = $parent::$endpoint."/$parent_id/".self::$endpoint;
$response = self::_all($options);
self::$endpoint = $endpoint;

return self::_all($options);
return $response;
}
}
6 changes: 5 additions & 1 deletion src/Requests/Extending/SubCreate.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ trait SubCreate
public static function create($parent_id, $data)
{
$parent = new self::$extends;

$endpoint = self::$endpoint;
self::$endpoint = $parent::$endpoint."/$parent_id/".self::$endpoint;
$response = self::_create($data);
self::$endpoint = $endpoint;

return self::_create($data);
return $response;
}
}
3 changes: 3 additions & 0 deletions src/Requests/Extending/SubDelete.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ trait SubDelete
public static function delete($parent_id, $id)
{
$parent = new self::$extends;

$endpoint = self::$endpoint;
self::$endpoint = $parent::$endpoint."/$parent_id/".self::$endpoint;
self::_delete($id);
self::$endpoint = $endpoint;
}
}
6 changes: 5 additions & 1 deletion src/Requests/Extending/SubGet.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ trait SubGet
public static function get($parent_id, $id)
{
$parent = new self::$extends;

$endpoint = self::$endpoint;
self::$endpoint = $parent::$endpoint."/$parent_id/".self::$endpoint;
$response = self::_get($id);
self::$endpoint = $endpoint;

return self::_get($id);
return $response;
}
}
6 changes: 5 additions & 1 deletion src/Requests/Extending/SubUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ trait SubUpdate
public static function update($parent_id, $id, $data)
{
$parent = new self::$extends;

$endpoint = self::$endpoint;
self::$endpoint = $parent::$endpoint."/$parent_id/".self::$endpoint;
$response = self::_update($id, $data);
self::$endpoint = $endpoint;

return self::_update($id, $data);
return $response;
}
}
3 changes: 3 additions & 0 deletions src/Requests/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Appoly\ShopWiredPHPSDK\Requests;

use Appoly\ShopWiredPHPSDK\ShopWiredClient;
use Appoly\ShopWiredPHPSDK\ShopWiredThrottle;

trait Get
{
Expand All @@ -15,6 +16,8 @@ trait Get
*/
public static function get($id, $options = [])
{
ShopWiredThrottle::throttle();

$shopwired_client = ShopWiredClient::get();

$endpoint = static::$endpoint;
Expand Down
3 changes: 3 additions & 0 deletions src/Requests/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Appoly\ShopWiredPHPSDK\Requests;

use Appoly\ShopWiredPHPSDK\ShopWiredClient;
use Appoly\ShopWiredPHPSDK\ShopWiredThrottle;

trait Update
{
Expand All @@ -15,6 +16,8 @@ trait Update
*/
public static function update($id, $data)
{
ShopWiredThrottle::throttle();

$shopwired_client = ShopWiredClient::get();

$endpoint = static::$endpoint;
Expand Down
Loading

0 comments on commit f8b0f36

Please sign in to comment.