long2ice / fastapi-limiter

A request rate limiter for fastapi
https://github.com/long2ice/fastapi-limiter
Apache License 2.0
484 stars 53 forks source link

Connect call failed #28

Open ahmed-881994 opened 1 year ago

ahmed-881994 commented 1 year ago

While trying out the example script in the repo I am getting the below error OSError: Multiple exceptions: [Errno 61] Connect call failed ('::1', 6379, 0, 0), [Errno 61] Connect call failed ('127.0.0.1', 6379)

This is the script from the repo (I removed the web socket route)

import redis.asyncio as redis
import uvicorn
from fastapi import Depends, FastAPI, HTTPException, WebSocket

from fastapi_limiter import FastAPILimiter
from fastapi_limiter.depends import RateLimiter, WebSocketRateLimiter

app = FastAPI()

@app.on_event("startup")
async def startup():
    r = redis.from_url("redis://localhost", encoding="utf8")
    await FastAPILimiter.init(r)

@app.on_event("shutdown")
async def shutdown():
    await FastAPILimiter.close()

@app.get("/", dependencies=[Depends(RateLimiter(times=2, seconds=5))])
async def index_get():
    return {"msg": "Hello World"}

@app.post("/", dependencies=[Depends(RateLimiter(times=1, seconds=5))])
async def index_post():
    return {"msg": "Hello World"}

@app.get(
    "/multiple",
    dependencies=[
        Depends(RateLimiter(times=1, seconds=5)),
        Depends(RateLimiter(times=2, seconds=15)),
    ],
)
async def multiple():
    return {"msg": "Hello World"}

if __name__ == "__main__":
    uvicorn.run("test:app", reload=True)
INFO:     Will watch for changes in these directories: ['/Users/safwat/Projects/playground']
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [74024] using WatchFiles
INFO:     Started server process [74026]
INFO:     Waiting for application startup.
ERROR:    Traceback (most recent call last):
  File "/Users/safwat/Projects/playground/.venv/lib/python3.11/site-packages/redis/asyncio/connection.py", line 603, in connect
    await self.retry.call_with_retry(
  File "/Users/safwat/Projects/playground/.venv/lib/python3.11/site-packages/redis/asyncio/retry.py", line 59, in call_with_retry
    return await do()
           ^^^^^^^^^^
  File "/Users/safwat/Projects/playground/.venv/lib/python3.11/site-packages/redis/asyncio/connection.py", line 640, in _connect
    reader, writer = await asyncio.open_connection(
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/streams.py", line 48, in open_connection
    transport, _ = await loop.create_connection(
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/base_events.py", line 1087, in create_connection
    raise OSError('Multiple exceptions: {}'.format(
OSError: Multiple exceptions: [Errno 61] Connect call failed ('::1', 6379, 0, 0), [Errno 61] Connect call failed ('127.0.0.1', 6379)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/safwat/Projects/playground/.venv/lib/python3.11/site-packages/starlette/routing.py", line 671, in lifespan
    async with self.lifespan_context(app):
  File "/Users/safwat/Projects/playground/.venv/lib/python3.11/site-packages/starlette/routing.py", line 566, in __aenter__
    await self._router.startup()
  File "/Users/safwat/Projects/playground/.venv/lib/python3.11/site-packages/starlette/routing.py", line 648, in startup
    await handler()
  File "/Users/safwat/Projects/playground/test.py", line 14, in startup
    await FastAPILimiter.init(r)
  File "/Users/safwat/Projects/playground/.venv/lib/python3.11/site-packages/fastapi_limiter/__init__.py", line 85, in init
    cls.lua_sha = await redis.script_load(cls.lua_script)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/safwat/Projects/playground/.venv/lib/python3.11/site-packages/redis/asyncio/client.py", line 509, in execute_command
    conn = self.connection or await pool.get_connection(command_name, **options)
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/safwat/Projects/playground/.venv/lib/python3.11/site-packages/redis/asyncio/connection.py", line 1400, in get_connection
    await connection.connect()
  File "/Users/safwat/Projects/playground/.venv/lib/python3.11/site-packages/redis/asyncio/connection.py", line 611, in connect
    raise ConnectionError(self._error_message(e))
redis.exceptions.ConnectionError: Error connecting to localhost:6379. Multiple exceptions: [Errno 61] Connect call failed ('::1', 6379, 0, 0), [Errno 61] Connect call failed ('127.0.0.1', 6379).

ERROR:    Application startup failed. Exiting.

My environment is OS: macOS Ventura 13.2.1 Python: 3.11.1 fastapi-limiter: 0.1.5

stevleibelt commented 1 year ago

@ahmed-881994

is your redis running and functional?

What happens if you replace the lines like following:

#replace this line
r = redis.from_url("redis://localhost", encoding="utf8")
#with that line please
r = redis.Redis(host="localhost", encoding="utf8")

Cheers, Stev