none
Why calls from the same client are not executed at the same time RRS feed

  • Question

  • I have azure function that only have 2 lines of code, first is await Task.Delay(5000) and second is returning status Ok to client. In host.json maxOutstandingRequests and maxConcurrentRequests are set. AF is executed locally and also i tried with deployed version. The problem occurs when I try to send multiple requests from the same HttpClient, function calls will not be async. Execution time per call will be 5, 10, 15, 20 and 25s. When i run same client's code with WebApi instead of AF (WebApi controller has same function as AF), then execution time per each call is 5s. I have two questions:

    1. Why AF can not process multiple requests from the same HttpClient at the same time?

    2. How can i get the same behavior with Azure function as I have it with WebApi?

    AF's code

    public static async Task<HttpResponseMessage> Run(
    [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "AF")]HttpRequestMessage req, 
    TraceWriter log) 
    { 
        await Task.Delay(5 * 1000); 
        return req.CreateResponse(HttpStatusCode.OK); 
    }

    Cleint's code

    class Program
    {
           private static System.Net.Http.HttpClient httpClient = new System.Net.Http.HttpClient();
    
           static void Main(string[] args)
           {
               IEnumerable<TimeSpan> result = Test(10);
    
               Console.WriteLine(string.Join("\n", result));
               Console.ReadLine();
           }
    
           private static IEnumerable<TimeSpan> Test(int taskCount)
           {
               Task<TimeSpan>[] tasks = new Task<TimeSpan>[taskCount];
    
               for (int i = 0; i < taskCount; i++)
                    tasks[i] = Send();
    
               Task.WaitAll(tasks);
    
               return tasks.Select(t => t.Result);
           }
    
           private static async Task<TimeSpan> Send()
           {
               using (var request = new HttpRequestMessage(HttpMethod.Post, ConfigurationManager.AppSettings["AF"]))
               {
                    Stopwatch sw = new Stopwatch();
    
                    sw.Start();
    
                    using (var response = await httpClient.SendAsync(request))
                    {
                        sw.Stop();
    
                        return sw.Elapsed;
                    }
               }
           }
       }

    Saturday, September 21, 2019 8:33 AM

All replies

  • Hello UniqueDisplayName11,

    Is your WebAPI code also have the same static method like Function Run method?

    Thursday, September 26, 2019 12:05 PM
    Moderator
  • Hello,
    My WebApi doesn't have static method, but I also tried same code in Azure Functions V2 (method is not static), and I got same behavior as I have it with Azure Functions V1 (method is static).
    Saturday, September 28, 2019 10:23 PM