0

I have few producers who will keeping filling into a BlockingCollection with bounded capacity of 200. I am using Reactive Extensions and calling an async method below like this, and I wanted to know if this is the correct way of implementing it (will there be any performance / concurrency issue of using GetConsumingEnumerable as every subscriber will get individual subscriber). I have used FromAsync as to avoid calling async on subscriber based on other documentation on GitHub. userActivites is a BlockingCollection. This code runs on a background thread for ASP.NET Web REST API. FYI we are using BlockingCollection as being multi threaded we wanted to make sure that subscribe on order the way it receives the data and using FromAsync based on [Github]( https://github.com/dotnet/reactive/issues/459) issue.

IObservable<EventData> eventObservable = userActivites.
    GetConsumingEnumerable().
    ToObservable(TaskPoolScheduler.Default);

eventObservable.Select(number => Observable.FromAsync(async () =>
    await SendDataToEventHubWithBatchAsync(number))).Concat().Subscribe();
Punit
  • 1,347
  • 3
  • 20
  • 39

1 Answers1

0

The BlockingCollection will block a thread, which is highly undesirable for ASP.NET applications. Have you considered using an async queue like the BufferBlock or a Channel? You can easily consume both as observables:

var block = new BufferBlock<UserActivity>(new DataflowBlockOptions() { BoundedCapacity = 200 });
var observable = block.AsObservable();

...or

var channel = Channel.CreateBounded<UserActivity>(200);
var observable = channel.Reader.ReadAllAsync().ToObservable();
Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
  • @Zoulias- Thanks... But my requirement is that we want to subscribe as and when items get added to collection we want to subscribe on it so we don't have to wait for collection to full and check for count. – Punit Dec 16 '19 at 20:09
  • @Punit both `BufferBlock` and `Channel` behave this way. Did you check them, and they don't suit your needs? – Theodor Zoulias Dec 17 '19 at 03:01