This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import redis | |
# redis connection | |
def get_connection(host="127.0.0.1", port="6379", db=0): | |
connection = redis.StrictRedis(host=host, port=port, db=db) | |
return connection | |
class SlidingWindowCounterRateLimiter(object): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import redis | |
# redis connection | |
def get_connection(host="127.0.0.1", port="6379", db=0): | |
connection = redis.StrictRedis(host=host, port=port, db=db) | |
return connection | |
class SlidingWindowLogRatelimiter(object): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import threading | |
class RequestCounters(object): | |
# Every window time is broken down to 60 parts | |
# 100 req/min translates to requests = 100 and windowTimeInSec = 60 | |
def __init__(self, requests, windowTimeInSec, bucketSize=10): | |
self.counts = {} | |
self.totalCounts = 0 | |
self.requests = requests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import threading | |
from collections import deque | |
class RequestTimestamps(object): | |
# lock is for concurrency in a multi threaded system | |
# 100 req/min translates to requests = 100 and windowTimeInSec = 60 | |
def __init__(self, requests, windowTimeInSec): | |
self.timestamps = deque() |