This is the folder of the Rate Limiter micro-service.
The rate limiter is based on the leaky bucket algorithm. Each client/limit has an empty bucket assigned with a given capacity (according to a limit definition). Each request goes into the bucket, that is leaking requests at a given rate (again according to a limit definition). A client is allowed to perform the request if there are remaining capacity in the bucket.
Example: Suppose that we have a limit that allows at maximum 6 requests per minute. Then our bucket can hold at maximum 6 requests. However we’ll start draining our bucket after 60 seconds at a rate of 6reqs/60sec, that is removing one request from the bucket every 10 seconds.
The rate limiter micro-service exposes a RESTful HTTP API that can perform basic CRUD operations on rate limit definitions, and an endpoint to check resource usage allowance.
PUT /limits/{resource-limit-id}
A rate limit establishes how many requests in a time interval a client is allowed to perform. For example, a client is allowed to perform at maximum 10 requests per hour.
This operation accepts the following parameters:
The following request creates a limit, identified by "check_account_balance_limit" where a client is allowed to perform a maximum of 10 requests per hour.
PUT /limits/check_account_balance_limit
Content-Type: application/json
{
"period": 3600,
"limit": 10,
"description": "Can check account balance 10 times / hour"
}
-- response
204 No Content
This operation can return the following HTTP status codes:
DELETE /limits/{resource-limit-id}
This operation can return the following HTTP status codes:
GET /limits
GET /limits
-- response
200 OK
Content-Type: application/json
[
{ "id": "other_account_operations", "period": 60, "limit": 100 },
{ "id": "create_account", "period": 3600, "limit": 10, "description": "Can create 10 accounts / hour" },
{ "id": "default", "period": 1, "limit": 10, "description": "Global request rate policy is 10req/s"}
]
POST /check
This operation checks if a client is either allowed or not allowed to perform the request according to the specified limit. Each call to this endpoint will consume one request associated to the given client.
This operation accepts the following parameters:
POST /check
{
"limit_id": "create_account",
"client_id": "user_a"
}
-- response
200 Ok
{
"allowed": true,
"remaining": 10
}
Where the response body attributes:
This operation can return the following HTTP status codes:
NOTE: In order to try this example you must have access to a redis instance. Set the
REDIS_URL if necessary (defaults to "redis://127.0.0.1:6379") and REDIS_PASSWORD (not required)
with the appropriate values.
Here's a quick start how to use the rate limiter. First start by creating a limit definition. In our example we're creating a limit that will allow one request each five seconds.
curl -XPUT http://localhost:5000/limits/meaningfull_limit_id -d '{
"period": 5,
"limit": 1,
"description": "Can check account balance 10 times / hour"
}'
Now that we have out limit definition, we can start sending requests. If you send a second request without waiting a few seconds, it will not be allowed.
curl -XPOST http://localhost:5000/check -d '{"limit_id": "meaningfull_limit_id", "client_id": "client1"}'
{"allowed":true,"remaining":0}
curl -XPOST http://localhost:5000/check -d '{"limit_id": "meaningfull_limit_id", "client_id": "client1"}'
{"allowed":false,"remaining":0}
Wait a few seconds and try again...
The license of the SONATA Gatekeeper is Apache 2.0 (please see the license file).
Please use the GitHub issues
and the SONATA development mailing list [email protected] for
feedback.
Content type
Image
Digest
Size
164 MB
Last updated
over 8 years ago
docker pull tdierich/son-gtkrlt