Skip to content

Commit

Permalink
added postman and server scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Manav-Khandurie committed May 21, 2024
1 parent 3e26982 commit 2c913b4
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 7 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

### About the Project
> The problem statement focuses on designing and implementing an effective rate limiting mechanism for API management to ensure fair and efficient usage of resources. The objective is to develop a system that prevents API abuse, optimizes resource allocation, and maintains high performance by applying various rate limiting strategies—Fixed Window, Sliding Window, Token Bucket, and Leaky Bucket. These strategies need to be adaptable based on parameters like request frequency, client identities, and available capacity.
#redis-cli -h rate-limiter-cache-0001-001.easvay.0001.use1.cache.amazonaws.com -p 6379

### Tech Stack
Expand Down
10 changes: 10 additions & 0 deletions server/event/ControllerLambda.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"function": "Lambda1-RL",
"payload": {
"resource": "MKResource",
"url": "http://example.com",
"bucketsize": 5,
"refillrate": 1,
"ttl": 10
}
}
7 changes: 5 additions & 2 deletions server/event/Lambda1.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"resource": "example_resource",
"time_window": 60
}
"url": "http://example.com",
"bucketsize": 5,
"refillrate": 1,
"ttl": 3600
}
6 changes: 6 additions & 0 deletions server/event/Lambda2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"resource": "example_resource",
"url": "http://example.com",
"limit": 10,
"window_size_seconds": 60
}
6 changes: 6 additions & 0 deletions server/event/Lambda3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"resource": "example_resource",
"url": "http://example.com",
"limit": 10,
"window_size_seconds": 60
}
19 changes: 19 additions & 0 deletions server/scripts/ControllerLambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,29 @@
import json

lambda_client = boto3.client('lambda')

def validate_payload(function_name, payload):
expected_parameters = {
'Lambda1-RL': ['resource', 'url', 'bucketsize', 'refillrate', 'ttl'],
'Lambda2-RL': ['resource', 'url', 'limit', 'window_size_seconds'],
'Lambda3-RL': ['resource', 'url', 'limit', 'window_size_seconds'],
'Test_Fun': ['test_param1', 'test_param2']
}

if function_name in expected_parameters:
return all(param in payload for param in expected_parameters[function_name])
else:
return True

def lambda_handler(event, context):

function_name = event.get('function')
payload = event.get('payload', {})
if not validate_payload(function_name, payload):
return {
'statusCode': 400,
'body': 'Invalid payload format for function: ' + function_name
}

function_arns = {
'Lambda1-RL': 'arn:aws:lambda:us-east-1:247477386084:function:Lambda1-RL',
Expand Down
3 changes: 2 additions & 1 deletion server/scripts/Lambda1.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import os

URL = os.environ.get('URL')
redis_client = redis.Redis(host=URL, port=6379, db=0)
# redis_client = redis.Redis(host=URL, port=6379, db=0)
redis_client = redis.Redis.from_url(URL)

def is_allowed(resource, bucket_size, refill_rate, ttl_seconds):
key = f"token_bucket:{resource}"
Expand Down
4 changes: 2 additions & 2 deletions server/scripts/Lambda2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import os

URL = os.environ.get('URL')
redis_client = redis.Redis(host=URL, port=6379, db=0)

# redis_client = redis.Redis(host=URL, port=6379, db=0)
redis_client = redis.Redis.from_url(URL)
def is_allowed(resource, limit, window_size_seconds):
key = f"fixed_window:{resource}"
current_count = redis_client.get(key)
Expand Down
3 changes: 2 additions & 1 deletion server/scripts/Lambda3.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

URL = os.environ.get('URL')
# Connect to Redis
redis_client = redis.Redis(host=URL, port=6379, db=0)
# redis_client = redis.Redis(host=URL, port=6379, db=0)
redis_client = redis.Redis.from_url(URL)

def is_allowed(resource, limit, window_size_seconds):
key = f"sliding_window_log:{resource}"
Expand Down
53 changes: 53 additions & 0 deletions server/test/Rate-Limiter-AWS.postman_collection.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"info": {
"_postman_id": "97465e81-962e-45d7-a402-356fe36a6c78",
"name": "Rate-Limiter-AWS",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "26811368",
"_collection_link": "https://www.postman.com/orbital-module-engineer-11493836/workspace/postman-api-fundamentals-student-expert/collection/26811368-97465e81-962e-45d7-a402-356fe36a6c78?action=share&source=collection_link&creator=26811368"
},
"item": [
{
"name": "Test1Controller",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\r\n \"function\": \"Lambda1-RL\",\r\n \"payload\": {\r\n \"resource\": \"MKResource\",\r\n \"url\": \"http://example.com\",\r\n \"bucketsize\": 5,\r\n \"refillrate\": 1,\r\n \"ttl\": 10\r\n }\r\n}\r\n",
"options": {
"raw": {
"language": "json"
}
}
}
},
"response": []
},
{
"name": "Test2Controller",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\r\n \"function\": \"Lambda2-RL\",\r\n \"payload\": {\r\n \"resource\": \"MK2_resource\",\r\n \"url\": \"http://example.com\",\r\n \"limit\": 5,\r\n \"window_size_seconds\": 30\r\n }\r\n}\r\n",
"options": {
"raw": {
"language": "json"
}
}
}
},
"response": []
},
{
"name": "Test3Controller",
"request": {
"method": "POST",
"header": []
},
"response": []
}
]
}

0 comments on commit 2c913b4

Please sign in to comment.