Reputation: 656
I'm trying to parallelize some calls to my api service. I found this example and it works fine. The only problem is that the server I call accepts a maximum of 10 simultaneous connections and the result is that some calls fail. How can I write this code in such a way that I am sure it does not make more than 10 simultaneous connections?
List<PlayerViewModel> p; // my list of 700 element
List<Player> listFinalPlayer = new List<Player>(); //final List
var batchSize = 100;
int numberOfBatches = (int)Math.Ceiling((double)p.Count() / batchSize);
for (int i = 0; i < numberOfBatches; i++)
{
var currentIds = p.Skip(i * batchSize).Take(batchSize);
var tasks = currentIds.Select(p => GetMarket(p));
.AddRange(await Task.WhenAll(tasks));
}
public async Task<Player> GetMarket(PlayerViewModel p)
{
try
{
HttpRequestMessage tirthRequest = createRequest("https://myapiprovider/v2/players/" + p.id + "/marketvalue");
var client3 = _clientFactory.CreateClient();
var response3 = await client3.SendAsync(tirthRequest);
Market market = JsonConvert.DeserializeObject<Market>(await response3.Content.ReadAsStringAsync());
Player finalPlayer = new Player();
finalPlayer.PlayerId = id;
finalPlayer.MarketValue = market.marketValue;
finalPlayer.Value = Convert.ToDouble(market.marketValue) / Convert.ToDouble(1000000);
return finalPlayer;
} catch(Exception e)
{
//Here I receive to many request from server
return new Player();
}
}
Thanks!!
Upvotes: 0
Views: 178
Reputation: 457027
How can I write this code in such a way that I am sure it does not make more than 10 simultaneous connections?
Throttling asynchronous code is usually done with SemaphoreSlim
:
private readonly SemaphoreSlim _throttle = new SemaphoreSlim(10);
public async Task<Player> GetMarket(PlayerViewModel p)
{
await _throttle.WaitAsync();
try
{
...
}
finally
{
_throttle.Release();
}
}
Upvotes: 1