C#C
C#8mo ago
20 replies
Pearl (asyncmeow)

Slow requests to internal API?

I'm working on an ASP.Net API that, as part of what it does, makes a couple hundred requests to another internal API. I know the other API can handle many more requests per second than I'm trying to do (we've benchmarked it previously with the exact request I'm making to be able to handle a few thousand reqs/sec with no issues). However, the behavior I'm seeing is that it takes ~2-3mins for it to make all of the requests. I'm using RestSharp to do the reqs, with code similar to (but not exactly - wanted to give a simple example) this (where I don't really care too much about the response):

public async Task MyEndpoint() {
    var tasks = new List<Task>();
    foreach (var thing in things) {
        tasks.Add(MakeRequest(thing));
    }
    await Task.WhenAll(tasks);
}

public async Task MakeRequest(MyThing thing) {
    var req = new RestRequest("/endpoint", Method.Post);
    req.AddJsonBody(thing);
    Logger.LogInformation("making request");
    var resp = await Client.ExecuteAsync(req); // also tried with .ConfigureAwait(false) on this
    Logger.LogInformation("made request - response status code is {statusCode}", resp.StatusCode);
}


Another issue is that when I do something like this to time the request:
    var sw = Stopwatch.StartNew();
    var resp = await Client.ExecuteAsync(req);
    sw.Stop();
    Logger.LogDebug("request took {timeMs} ms", sw.ElapsedMilliseconds);

it seems like the timer is actually timing from the start of
MyEndpoint
, not from rihgt before the request is made to when it finishes? As in, when I asked the team who maintains the service I'm making requests to, they don't see some of the requests being made for over a minute after I start running this with it finishing in (for example) 50ms, but the timer shows it taking 60050 ms.
Was this page helpful?