queue: Only set QOS on a newly-opened channel, once.

As written, the QOS parameters are (re)set every time ensure_queue is
called, which is every time a message is enqueued. This is wasteful --
particularly QOS parameters only apply for consumers, and setting them
takes a RTT to the server.

Switch to only setting the QOS once, when a connection
is (re)established.  In profiling, this reduces the time to call
`queue_json_publish("noop", {})` from 878µs to 150µs.
This commit is contained in:
Alex Vandiver 2023-02-23 04:36:14 +00:00 committed by Tim Abbott
parent d145644f85
commit aa032bf62c
1 changed files with 4 additions and 3 deletions

View File

@ -160,9 +160,10 @@ class SimpleQueueClient(QueueClient[BlockingChannel]):
the callback with no arguments."""
if self.connection is None or not self.connection.is_open:
self._connect()
assert self.channel is not None
self.channel.basic_qos(prefetch_count=self.prefetch)
assert self.channel is not None
self.channel.basic_qos(prefetch_count=self.prefetch)
else:
assert self.channel is not None
if queue_name not in self.queues:
self.channel.queue_declare(queue=queue_name, durable=True)