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

leak of amqp connections with gevent #73

Closed
horpto opened this issue Jul 4, 2020 · 2 comments
Closed

leak of amqp connections with gevent #73

horpto opened this issue Jul 4, 2020 · 2 comments
Labels

Comments

@horpto
Copy link

horpto commented Jul 4, 2020

Hello, we use bundle of gunicorn+django+dramatiq+gevent and rabbitmq as message broker. Gunicorn gevent worker class creates new greenlet for each new request. Dramatiq creates an amqp connection first time as it needs to send message. But after request is processed amqp connection remains opened and this connection will be not used again. Of course connection will be closed by rabbit. But it's not good and takes some time and resources. I've solved this issue with django middleware (i can create PR). Maybe is there some better solution ?

import dramatiq

def CloseRabbitConnMiddleware(get_response):

    def middleware(request):
        response = get_response(request)

        broker = dramatiq.get_broker()
        if broker.connections:
            conn = broker.connection
            del broker.connection
            conn.close()

        return response

    return middleware
@mohaiminul-sust
Copy link

@horpto
Can you post an example with implementation? Will adding this middleware to the end of the chain for DRAMATIQ_BROKER -> MIDDLEWARE solve the connection issue?

@andrewgy8
Copy link
Collaborator

andrewgy8 commented Jan 11, 2025

Hey @horpto ! Thanks for providing a solution to this issue. I think the middleware solution is sufficient for now. The other option is to hardcode a conditional into the codebase. But seeing that the use case for this is quite specific with the usage of gevent, I dont think its worth adding to the codebase.

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

No branches or pull requests

3 participants