what is the most optimum way to make 1000+ HTTP requests without worrying about socket/port exhaustion?
I'm facing performance & socket/port issues where for example if there are 1400 requests sometimes only half of it are getting executed. It's also affecting other scripts which are running in parallel looking for sockets/port for their own execution.
I'm calling my async Task method like this : -
//Inside a for loop which can be iterated 1000+ times based on condition
c8y_pushAlert(c8y_Alert_MSG).GetAwaiter().GetResult();
//Reason behind using above line is that I want each request to complete it's
//execution cycle of both get and post request inside async Task method before
//the next c8y_Alert_MSG is pushed.
Also to note that the above line is not inside any method, There's no main method & I cannot use it inside static async Task Main(string[] args) due to some reasons
Here's whats going on in my async Task method : -
public async Task c8y_pushAlert(string Alert_MSG)
{
//Doing bunch of stuff with Alert_MSG
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic","auth_val");
HttpResponseMessage get_response = await client.GetAsync("https://myurl.com/"+controllerName);
var get_responseString = await get_response.Content.ReadAsStringAsync();
//doing bunch of stuff with get_responseString to use contents from it for Post request
client.DefaultRequestHeaders.Add("Accept", "application/vnd.com.nsn.xyz.alarm+json");
var stringContent = new StringContent(c8y_finalPayloadString, Encoding.UTF8, "application/json");
stringContent.Headers.ContentType.CharSet = "";
HttpResponseMessage response = await client.PostAsync("https://myurl.com", stringContent);
if (response.IsSuccessStatusCode)
{
var responseString = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseString);
}
}
}