0

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

  1. It allows 16 messages at a time (2 sessions * 8 concurrent messages)
  2. 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.
  3. 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?

Peter Morris
  • 20,174
  • 9
  • 81
  • 146
  • Why do you want to restrict concurrency? Have you already tried [maxConcurrentRequests](https://stackoverflow.com/a/66163971/496289)? May not be perfect, but could work depending on few things. – Kashyap Apr 30 '21 at 15:07
  • @Kashyap Can I set maxConcurrentRequests per queue name? – Peter Morris May 01 '21 at 12:37
  • No. See the linked answer and the documentation for that param. Also you didn't answer "Why do you want to restrict concurrency?" – Kashyap May 03 '21 at 16:23

1 Answers1

1

You can change the property SessionIdleTimeout. Reference.

Changing this property, the code can await less to get messages for other sessions and will accelerate the processing.

Thiago Silva
  • 437
  • 2
  • 10