Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redis client not found when created background tasks in Swagger #151

Open
scottyeung opened this issue Sep 8, 2024 · 5 comments
Open

Redis client not found when created background tasks in Swagger #151

scottyeung opened this issue Sep 8, 2024 · 5 comments

Comments

@scottyeung
Copy link

Describe the bug or question
A clear and concise description of what the bug or question is.

To Reproduce
Please provide a self-contained, minimal, and reproducible example of your use case

async def is_rate_limited(db: AsyncSession, user_id: int, path: str, limit: int, period: int) -> bool:
    if client is None:
        logger.error("Redis client is not initialized.")
        raise Exception("Redis client is not initialized.")

Description
Redis client was recognised as none when I tried to create background tasks in Swagger and caught exception in is_rate_limited

Screenshots
Screenshot 2024-09-08 at 5 12 24 PM
Screenshot 2024-09-08 at 5 12 36 PM
Screenshot 2024-09-08 at 5 14 44 PM

Additional context
Add any other context about the problem here.

@igorbenav
Copy link
Owner

How did you run it? Using docker compose?

@scottyeung
Copy link
Author

How did you run it? Using docker compose?

Exactly, docker-compose. Did I miss anything?

@studentNinja
Copy link

Having the exact same issue.
Redis name in .env = redis ,
App started by docker compose up -d
When trying to post a new task , getting raise Exception("Redis client is not initialized.")=

@yucelz
Copy link

yucelz commented Oct 3, 2024

I think the issue is that all the Redis connections and the pool are tied to the cache in setup.py, which means that anyone who isn't using the cache can't get to them.

I came up with a temporary fix. It's not ideal, but it does the job. I moved everything, like the is_rate_limited function, into cache.py, and now it works just fine.

@mlm-master
Copy link

mlm-master commented Nov 28, 2024

With our without cache, i'm still having the same exact issue too. Upon debugging, I can see that the Redis rate_limit.client instantiated when the application is starting.
Screenshot 2024-11-28 at 2 44 45 PM

However, the Redis client object somehow gets destroy and become None when the API endpoint is called:
Screenshot 2024-11-28 at 2 45 31 PM

The issue only occurs on redis rate limiter object, but not on redis cache object

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants