Skip to content

Commit 64e5365

Browse files
committed
Reduce loop wait time and make it configurable
1 parent ca1410d commit 64e5365

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

src/HangFire.Azure.ServiceBusQueue/ServiceBusManager.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,15 @@ public ServiceBusManager(ServiceBusQueueOptions options)
3535
}
3636
}
3737

38+
public ServiceBusQueueOptions Options { get { return _options; } }
39+
3840
public QueueClient GetClient(string queue)
3941
{
4042
if (_clients.Count != _options.Queues.Length)
4143
{
4244
CreateQueueClients();
4345
}
44-
46+
4547
return _clients[queue];
4648
}
4749

@@ -98,7 +100,7 @@ private static void CreateQueueIfNotExists(string prefixedQueue, NamespaceManage
98100
{
99101
var errorMessage = string.Format(
100102
"Queue '{0}' could not be checked / created, likely due to missing the 'Manage' permission. " +
101-
"You must either grant the 'Manage' permission, or setServiceBusQueueOptions.CheckAndCreateQueues to false",
103+
"You must either grant the 'Manage' permission, or set ServiceBusQueueOptions.CheckAndCreateQueues to false",
102104
prefixedQueue);
103105

104106
throw new UnauthorizedAccessException(errorMessage, ex);

src/HangFire.Azure.ServiceBusQueue/ServiceBusQueueJobQueue.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ namespace Hangfire.Azure.ServiceBusQueue
1212
internal class ServiceBusQueueJobQueue : IPersistentJobQueue
1313
{
1414
private static readonly TimeSpan MinSyncReceiveTimeout = TimeSpan.FromTicks(1);
15-
private static readonly TimeSpan SyncReceiveTimeout = TimeSpan.FromSeconds(5);
1615

1716
private readonly ServiceBusManager _manager;
1817

@@ -39,9 +38,10 @@ public IFetchedJob Dequeue(string[] queues, CancellationToken cancellationToken)
3938
try
4039
{
4140
var client = clients[queueIndex];
41+
var isLastQueue = queueIndex == queues.Length - 1;
4242

43-
message = queueIndex == queues.Length - 1
44-
? client.Receive(SyncReceiveTimeout)
43+
message = isLastQueue
44+
? client.Receive(_manager.Options.LoopReceiveTimeout) // Last queue
4545
: client.Receive(MinSyncReceiveTimeout);
4646
}
4747
catch (TimeoutException)
@@ -51,7 +51,7 @@ public IFetchedJob Dequeue(string[] queues, CancellationToken cancellationToken)
5151
{
5252
var errorMessage = string.Format(
5353
"Queue {0} could not be found. Either create the queue manually, " +
54-
"or grant the Manage permission and set ServiceBusQueueOptions.CheckAndCreateQueues to true",
54+
"or grant the Manage permission and set ServiceBusQueueOptions.CheckAndCreateQueues to true",
5555

5656
clients[queueIndex].Path);
5757

src/HangFire.Azure.ServiceBusQueue/ServiceBusQueueOptions.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public class ServiceBusQueueOptions
88
public ServiceBusQueueOptions()
99
{
1010
this.CheckAndCreateQueues = true;
11+
this.LoopReceiveTimeout = TimeSpan.FromMilliseconds(500);
1112
}
1213

1314
/// <summary>
@@ -19,7 +20,7 @@ public ServiceBusQueueOptions()
1920

2021
/// <summary>
2122
/// Configures a queue on construction, for example setting maximum message
22-
/// size of default TTL.
23+
/// size or default TTL.
2324
/// </summary>
2425
public Action<QueueDescription> Configure { get; set; }
2526

@@ -38,6 +39,17 @@ public ServiceBusQueueOptions()
3839

3940
public string[] Queues { get; set; }
4041

42+
/// <summary>
43+
/// Gets or sets a timeout that is used between loop runs of receiving messages from Azure Service Bus. This is the timeout
44+
/// used when waiting on the last queue before looping around again (does not apply when only a single-queue exists).
45+
/// </summary>
46+
/// <remarks>
47+
/// Typically a lower value is desired to keep the throughput of message processing high. A lower timeout means more calls to
48+
/// Azure Service Bus which can increase costs, especially on an under-utilised server with few jobs.
49+
/// </remarks>
50+
/// <value>Defaults to <c>TimeSpan.FromMilliseconds(500)</c></value>
51+
public TimeSpan LoopReceiveTimeout { get; set; }
52+
4153
internal string GetQueueName(string name)
4254
{
4355
if (QueuePrefix != null)

0 commit comments

Comments
 (0)