UPSTASH_REDIS_REST_URL
and UPSTASH_REDIS_REST_TOKEN
to your environment:
python-dotenv
to load environment variables from your .env
file.
main.py
:
Redis
client with Redis.from_env()
using environment variables for configuration.Ratelimit
with a FixedWindow
limiter that allows 10 requests per 10 seconds. The prefix
option is set to organize the Redis keys used by the rate limiter./expensive_calculation
endpoint, the rate limiter is applied by calling ratelimit.limit(identifier)
.identifier
variable uniquely identifies this rate limit. You could use user-specific identifiers (like user IDs) to implement per-user limits.do_expensive_calculation
function simulates a resource-intensive operation. In real scenarios, this could represent database queries, file processing, or other time-consuming tasks.