I have a ServiceBus queue with sessions enabled. The SessionId on each message is very unique, so out of 10,000 messages there will be 5,000 unique SessionIds.
I want to throttle the consumer to 16 maximum concurrent messages, so I added the following to my hosts file.
"extensions": {
"serviceBus": {
"messageHandlerOptions": {
"maxConcurrentCalls": 16
},
}
}
The problem with this is that it seems to be a maximum of 16 per session rather than a total of 8 at any given time.
So, with my Message.SessionId being so unique, I am seeing hundreds of messages flooding in instead of only 8 at a time.
I tried setting maxConcurrentSessions
too,
"extensions": {
"serviceBus": {
"messageHandlerOptions": {
"maxConcurrentCalls": 8
},
"sessionHandlerOptions": {
"maxConcurrentSessions": 2
}
},
"queues": {
"maxDequeueCount": 8
}
}
but this has the following problems
- It allows 16 messages at a time (2 sessions * 8 concurrent messages)
- Once the two messages for that session have been processed, the session sits idle for X seconds before cancelling its subscription and then getting a new one in order to process more messages.
- It limits all non-sessioned queues/topics to 8 concurrent messages.
I tried something like the following. This ensured I did not process too many messages at the same time, however, I still received lots of messages so they ended up timing out.
static SemaphoreSlim Semaphore = new SemaphoreSlim(16, 16);
[FunctionName("Do stuff")]
public async Task RunAsync(
[ServiceBusTrigger("My queue", IsSessionsEnabled = true)]
Message message)
{
await Semaphore.WaitAsync().ConfigureAwait(false);
try
{
Do the stuff here
}
finally
{
Semaphore.Release();
}
}
}
Is there a way to get my function to receive a maximum of X concurrent messages regardless of how many sessions are involved?