Azure / azure-service-bus

☁️ Azure Service Bus service issue tracking and samples
https://azure.microsoft.com/services/service-bus
MIT License
580 stars 775 forks source link

Unexpected latency change while sending request to azure service bus #701

Closed Divyasri1510 closed 3 months ago

Divyasri1510 commented 3 months ago

Description

Hi, I'm seeing some latency change while sending a message to an Azure Service Bus queue. We tried to check this by creating client and then sending message after specific interval durations of 2mins, 5mins and 10mins. Initially it took 2 secs for message to be sent. After making thread to sleep for 5mins, we sent message again and now it took only few milliseconds. Then after 10mins when we sent message again it took around 10seconds. We also observed multiple active connections being created. Below is the code snippet that we tried. Can you please explain why this is happening and what needs to be done for time to take few milliseconds for sending message always?

import time
from azure.servicebus import ServiceBusClient, ServiceBusMessage, ServiceBusSender

conn_str = "sample str"
asb_client = ServiceBusClient.from_connection_string(
                    conn_str=conn_str,logging_enable=True
                )

asb_sender = asb_client.get_queue_sender(queue_name="harshit_test")

msg = ServiceBusMessage("test")

print (datetime.now())
print (asb_sender)
asb_sender.send_messages(msg)
print (datetime.now())
time.sleep(120)
print (datetime.now())
print (asb_sender)
asb_sender.send_messages(msg)
print (datetime.now())
time.sleep(300)
print (datetime.now())
print (asb_sender)
asb_sender.send_messages(msg)
print (datetime.now())
time.sleep(600)
print (datetime.now())
print (asb_sender)
asb_sender.send_messages(msg)
print (datetime.now())

The result we got is: image (1) (1)

Actual Behavior

  1. It is taking unexpected time everytime while sending message

Expected Behavior

  1. It should take ideal time less than seconds for sending message
EldertGrootenboer commented 3 months ago

This is expected behavior, as the connection is automatically closed after ~5 minutes of idleness (or ~1 minute if no link was created). At this point a new connection needs to be created, which takes longer then re-using a previously created connection and link. To keep the connection and link open, make sure to do an operation before the connection times out.