Skip to content

Instantly share code, notes, and snippets.

@jasonlewis
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonlewis/1ca64d51d9c035098db0 to your computer and use it in GitHub Desktop.
Save jasonlewis/1ca64d51d9c035098db0 to your computer and use it in GitHub Desktop.
Looking at an improved way of throttling API requests with the package. This will allow you to register custom throttles when you want to grant more requests for different types of authentication or user levels.
<?php
return [
'throttling' => [
new Dingo\Api\Http\RateLimit\AuthenticatedThrottle(['limit' => 0, 'expires' => 60]),
new Dingo\Api\Http\RateLimit\UnauthenticatedThrottle(['limit' => 0, 'expires' => 60]),
// Register a custom throttle.
new OAuth2Throttle(['limit' => 2000, 'expires' => 20])
]
];
<?php
class OAuth2Throttle extends Dingo\Api\Http\RateLimit\Throttle
{
public function condition($app)
{
return $app['api.auth']->via('oauth2');
}
}
@jasonlewis
Copy link
Author

Instead of instantiating the classes directly it might be better to instead use a multi-dimensional array?

<?php 

return [
    'throttling' => [
        [
            'class' => 'OAuth2Throttle',
            'limit' => 2000,
            'expires' => 20
        ],
        [
            'class' => 'Dingo\Api\Http\RateLimit\AuthenticatedThrottle',
            'limit' => 0,
            'expires' => 60
        ]
    ]
];

I kind of like the cleanliness of instantiating the classes though.

@mitchellvanw
Copy link

I'd rather instantiate classes than use more multidimensional arrays

@jasonlewis
Copy link
Author

Yeah I'm much the same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment