Remove cachetools dependency

This commit is contained in:
LilyRose2798 2024-04-23 11:47:23 +10:00
parent 5eee66d071
commit f6ad5e5c55
3 changed files with 3 additions and 16 deletions

View File

@ -9,7 +9,6 @@ from zlib import decompress
from re import compile from re import compile
from urllib.parse import unquote from urllib.parse import unquote
from hilbertcurve.hilbertcurve import HilbertCurve from hilbertcurve.hilbertcurve import HilbertCurve
from cachetools import TTLCache
from ipaddress import IPv4Network from ipaddress import IPv4Network
from dns.resolver import resolve_address, NXDOMAIN, NoAnswer, LifetimeTimeout, NoNameservers from dns.resolver import resolve_address, NXDOMAIN, NoAnswer, LifetimeTimeout, NoNameservers
from dns.exception import SyntaxError from dns.exception import SyntaxError
@ -56,13 +55,13 @@ def error_response(message: str, status_code: int, headers: dict = {}):
limit = int(args.limit) limit = int(args.limit)
limit_ttl = 3600 limit_ttl = 3600
ttl_cache = TTLCache(maxsize = 1024, ttl = limit_ttl) ttl_cache: dict[str, list[int]] = dict()
@app.before_request() @app.before_request()
def middleware(request: Request): def middleware(request: Request):
if request.ip_addr: if request.ip_addr:
current_timestamp = int(time()) current_timestamp = int(time())
timestamps = [t for t in ttl_cache.get(request.ip_addr, default = []) if t > current_timestamp - limit_ttl] + [current_timestamp] timestamps = [t for t in ttl_cache.get(request.ip_addr, []) if t > current_timestamp - limit_ttl] + [current_timestamp]
ttl_cache[request.ip_addr] = timestamps ttl_cache[request.ip_addr] = timestamps
if len(timestamps) > limit: if len(timestamps) > limit:
return error_response("Rate limit exceeded!", 429) return error_response("Rate limit exceeded!", 429)

13
poetry.lock generated
View File

@ -1,16 +1,5 @@
# This file is automatically @generated by Poetry 1.8.2 and should not be changed by hand. # This file is automatically @generated by Poetry 1.8.2 and should not be changed by hand.
[[package]]
name = "cachetools"
version = "5.3.3"
description = "Extensible memoizing collections and decorators"
optional = false
python-versions = ">=3.7"
files = [
{file = "cachetools-5.3.3-py3-none-any.whl", hash = "sha256:0abad1021d3f8325b2fc1d2e9c8b9c9d57b04c3932657a72465447332c24d945"},
{file = "cachetools-5.3.3.tar.gz", hash = "sha256:ba29e2dfa0b8b556606f097407ed1aa62080ee108ab0dc5ec9d6a723a007d105"},
]
[[package]] [[package]]
name = "dill" name = "dill"
version = "0.3.8" version = "0.3.8"
@ -435,4 +424,4 @@ files = [
[metadata] [metadata]
lock-version = "2.0" lock-version = "2.0"
python-versions = "^3.11" python-versions = "^3.11"
content-hash = "805d5d3d73ead8269e5250a2ba6be33cdc9b6201a97ac39479dc335c1db014c6" content-hash = "70433eecb2a0b956cabdef7a442fb1adde89a8d1fd19c53f52d6ad2fb8ef3e48"

View File

@ -11,7 +11,6 @@ python = "^3.11"
dnspython = "^2.6.1" dnspython = "^2.6.1"
hilbertcurve = "^2.0.5" hilbertcurve = "^2.0.5"
robyn = "^0.54.1" robyn = "^0.54.1"
cachetools = "^5.3.3"
[build-system] [build-system]