How to design parallel web api in c# .net 4.5?

by omega   Last Updated August 08, 2018 01:05 AM

I am trying to design a web api that can get data from an external server but with limitations. I'm trying to figure out how best to design it to be efficient.

My api has an endpoint that takes an input. It is is a domain name like [email protected]. My endpoint then makes an http call to the domain to get an auth token, then makes another call to that domain with the username to get some data which is returned to the client. However my api can accept multiple usernames (comma delimited like [email protected], [email protected]). My web server knows for each domain what is the max parallel connections I can make to get the data.

So the problem is how to organize the data so I can maximize parallel computing but stay within the limits.

Here's my thoughts:

First parse the user list and group them up. Then have a static dictionary. Key is domain, value is a custom object which has 2 queues. Both queues holds a list of Tasks (from async/await). However the first queue max length will be the value of the limit for that domain.

[email protected], [email protected], [email protected], [email protected]

dictionary = {
    "D.com" : [ // limit 2
         [],
         ["[email protected]", "[email protected]", "[email protected]"]
     ],
     "A.com" : [ // limit 3
         [],
         ["[email protected]"]
     ]
}

Then I can run a code every second, which loops through all dictionary values, and fills the first queue with as many Task objects from the second queue (.e. removing from 2nd queue and putting in first) so its within the limit.

As soon as its in the first queue, the task executes using Parallel.Invoke() then when the task is completed it gets removed from first queue (unless some request is waiting for it, explained in next paragraph).

I do this because if another api request is made to my endpoint with some names thats already from the first request, I want to reuse it. So If it's in the first queue, I call await on that Task.

Somehow when a task finishes, I need to know that no other people are waiting for that user in the task, and in that case, remove it from the first queue. Also if a client disconnects it should remove the watching of the users part for that client.

Does anyone know if this is a good approach?



Related Questions




Best Practice for Wrapper / Adapters

Updated February 16, 2018 07:05 AM