Skip to content

Instantly share code, notes, and snippets.

View saisandeep's full-sized avatar

Sai Sandeep Mopuri saisandeep

View GitHub Profile
@saisandeep
saisandeep / sliding-window-counters-redis.py
Last active September 12, 2019 13:00
Sliding Window Counters Redis Rate Limiter
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):
@saisandeep
saisandeep / sliding-window-logs-redis.py
Last active June 10, 2018 07:00
Sliding Window Logs Redis Rate Limiter
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):
@saisandeep
saisandeep / sliding-window-counters-in-memory.py
Last active June 10, 2018 07:37
Sliding Window Counters In memory
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
@saisandeep
saisandeep / sliding-window-logs-in-memory.py
Last active June 10, 2018 06:51
Sliding Window Logs Rate Limiter
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()