Spaces:
Sleeping
Sleeping
File size: 601 Bytes
ff24c30 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import time
import hmac
import hashlib
from fastapi import HTTPException
REQUEST_LOG = {}
WINDOW_SECONDS = 10
MAX_REQUESTS = 6
def rate_limit(ip):
t = time.time()
entries = REQUEST_LOG.get(ip, [])
entries = [x for x in entries if t - x < WINDOW_SECONDS]
if len(entries) >= MAX_REQUESTS:
raise HTTPException(
status_code=429,
detail="Too many requests."
)
REQUEST_LOG[ip] = entries + [t]
def sign_payload(payload, secret):
return hmac.new(
secret.encode(),
payload.encode(),
hashlib.sha256
).hexdigest() |