Skip to content

Commit

Permalink
Close RabbitMQ connection after publishing to prevent file descriptor…
Browse files Browse the repository at this point in the history
… leaks
  • Loading branch information
knmarcin authored and BBooijLiewes committed Dec 20, 2024
1 parent f0d61be commit e12a8a7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 14 deletions.
32 changes: 18 additions & 14 deletions binder/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,24 @@ def list_rooms_for_user(self, user):

def trigger(data, rooms):
if 'rabbitmq' in getattr(settings, 'HIGH_TEMPLAR', {}):
import pika
from pika import BlockingConnection

connection_credentials = pika.PlainCredentials(settings.HIGH_TEMPLAR['rabbitmq']['username'],
settings.HIGH_TEMPLAR['rabbitmq']['password'])
connection_parameters = pika.ConnectionParameters(settings.HIGH_TEMPLAR['rabbitmq']['host'],
credentials=connection_credentials)
connection = BlockingConnection(parameters=connection_parameters)
channel = connection.channel()

channel.basic_publish('hightemplar', routing_key='*', body=jsondumps({
'data': data,
'rooms': rooms,
}))
connection = None
try:
import pika
from pika import BlockingConnection
connection_credentials = pika.PlainCredentials(settings.HIGH_TEMPLAR['rabbitmq']['username'],
settings.HIGH_TEMPLAR['rabbitmq']['password'])
connection_parameters = pika.ConnectionParameters(settings.HIGH_TEMPLAR['rabbitmq']['host'],
credentials=connection_credentials)
connection = BlockingConnection(parameters=connection_parameters)
channel = connection.channel()

channel.basic_publish('hightemplar', routing_key='*', body=jsondumps({
'data': data,
'rooms': rooms,
}))
finally:
if connection and not connection.is_closed:
connection.close()
if getattr(settings, 'HIGH_TEMPLAR_URL', None):
url = getattr(settings, 'HIGH_TEMPLAR_URL')
try:
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
'Pillow >= 3.2.0',
'django-request-id >= 1.0.0',
'requests >= 2.13.0',
'pika == 1.3.2',
],
tests_require=[
'django-hijack >= 2.1.10, < 3.0.0',
Expand Down
25 changes: 25 additions & 0 deletions tests/test_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.contrib.auth.models import User
from unittest import mock
from binder.views import JsonResponse
from binder.websocket import trigger
from .testapp.urls import room_controller
from .testapp.models import Animal, Costume
import requests
Expand Down Expand Up @@ -68,3 +69,27 @@ def test_post_succeeds_when_trigger_fails(self):
costume.save()

self.assertIsNotNone(costume.pk)


class TriggerConnectionCloseTest(TestCase):
@override_settings(
HIGH_TEMPLAR={
'rabbitmq': {
'host': 'localhost',
'username': 'guest',
'password': 'guest'
}
}
)
@mock.patch('pika.BlockingConnection')
def test_trigger_calls_connection_close(self, mock_connection_class):
mock_connection = mock_connection_class.return_value
mock_connection.is_closed = False

data = {'id': 123}
rooms = [{'costume': 123}]

trigger(data, rooms)

mock_connection.close.assert_called_once()

0 comments on commit e12a8a7

Please sign in to comment.