Bogdanp / django_dramatiq

A Django app that integrates with Dramatiq.
https://dramatiq.io
Other
350 stars 77 forks source link

leak of amqp connections with gevent #73

Open horpto opened 4 years ago

horpto commented 4 years ago

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 commented 1 year ago

@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?