pgjones / quart-rate-limiter

Quart-Rate-Limiter is an extension for Quart to allow for rate limits to be defined and enforced on a per route basis.
MIT License
22 stars 6 forks source link

Can't get quart-rate-limiter working (maybe because I'm using quart_openapi?) #2

Closed TheChemicalWorkshop closed 2 years ago

TheChemicalWorkshop commented 2 years ago
from quart import Quart, render_template, request, jsonify, send_from_directory, make_response
from quart_openapi import Pint, Resource

import asyncio
import asyncpg
import aioredis
from datetime import timedelta
import json
import socket
import os
import random
import requests
import re

from quart_rate_limiter import RateLimiter, RateLimit, rate_limit

# ! create the app
# app = Quart(__name__)

app = Pint(__name__, title='The Chemical Workshop API')

rate_limiter = RateLimiter(
    default_limits=[RateLimit(1, timedelta(seconds=5))]
)

@app.before_serving
async def startup():
    # ! init redis
    app.redis = redis = aioredis.from_url(
        "redis://localhost", encoding="utf-8", decode_responses=True
    )

@app.after_serving
async def shutdown():
    print("shutting down !")
    await app.redis.close()

@app.route('/')
async def index():
    return str(request.headers) 

@app.route('/abc')
@rate_limit(1, timedelta(seconds=10))
async def hello_abc():
    return str("test")  

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=6060)

I'm expecting '/' to allow 1 request every 5 seconds and '/abc' to allow 1 every 10 seconds

and i can spam the route with 100+ requests per second and i never get limited

is this because of quart_openapi ? if so, is there an alternative that works with quart_rate_limiter ?

TheChemicalWorkshop commented 2 years ago

nevermind !

rate_limiter = RateLimiter(app,
    default_limits=[RateLimit(1, timedelta(seconds=5))]
)

fixed it, not works as intended, i hope thanks for nothing !