3
votes

I occasionally see timeout errors in event hub client when trying to send messages to Azure event hub. It looks like resource limit is reached for client but I'm not sure... Here the code:

   MessagingFactory messagingFactory = null;
            EventHubClient hubClient = null;

            try
            {
                messagingFactory = MessagingFactory.CreateFromConnectionString(this.connectionString);

                hubClient = messagingFactory.CreateEventHubClient(this.configurationManager.EventHubName);

                var batchIterator = new EventBatchIterator<T>(events);
                foreach (var batch in batchIterator)
                {
                    await hubClient.SendBatchAsync(batch);
                }
            }
            catch (Exception e)
            {
                this.logger.Error("An error occurred during sent messages to event hub", e);
            }
            finally
            {
                if (hubClient != null)
                {
                    await hubClient.CloseAsync();
                }

                if (messagingFactory != null)
                {
                    await messagingFactory.CloseAsync();
                }
            }

Exception is:

An error occurred during communication with 'N/A'. Check the connection information, then retry. Microsoft.ServiceBus.Messaging.MessagingCommunicationException

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond System.Net.Sockets.SocketException

1

1 Answers

3
votes

According to you mentioned execption indicates that a user-initiated operation is taking longer than the operation timeout. My workaround is that you could increase OperationTimeout or retry count.

Demo code for increasing timeout

var builder = new ServiceBusConnectionStringBuilder("connection string")
                {
                    TransportType = TransportType.Amqp,
                    OperationTimeout = TimeSpan.FromSeconds(90)
                };

messagingFactory = MessagingFactory.CreateFromConnectionString(builder.ToString());

More info about Timeout exception you refer to this document.

Common causes

There are two common causes for this error: incorrect configuration, or a transient service error.

  • Incorrect configuration The operation timeout might be too small for the operational condition. The default value for the operation timeout in the client SDK is 60 seconds. Check to see if your code has the value set to something too small. Note that the condition of the network and CPU usage can affect the time it takes for a particular operation to complete, so the operation timeout should not be set to a very small value.

  • Transient service error Sometimes the Event Hubs service can experience delays in processing requests; for example, during periods of high traffic. In such cases, you can retry your operation after a delay, until the operation is successful. If the same operation still fails after multiple attempts, please visit the Azure service status site to see if there are any known service outages.