Limit the number of simultaneous jobs in the Azure Functions queue

I have a Function application in Azure that starts when an item is queued. It looks something like this (greatly simplified):

public static async Task Run(string myQueueItem, TraceWriter log)
{
    using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri(Config.APIUri);
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        StringContent httpContent = new StringContent(myQueueItem, Encoding.UTF8, "application/json");
        HttpResponseMessage response = await client.PostAsync("/api/devices/data", httpContent);
        response.EnsureSuccessStatusCode();

        string json = await response.Content.ReadAsStringAsync();
        ApiResponse apiResponse = JsonConvert.DeserializeObject<ApiResponse>(json);

        log.Info($"Activity data successfully sent to platform in {apiResponse.elapsed}ms.  Tracking number: {apiResponse.tracking}");
    }
}

All this works great and works very well. Each time an element is queued, we send data to some API on our side and register the response. Cool.

The problem arises when there is a big splash in the "thing that generates messages in the queue" and many items are immediately placed in the queue. This typically occurs from about 1,000 to 1,500 pieces per minute. The error log will have the following:

2017-02-14T01: 45: 31.692 mscorlib: : Functions.SendToLimeade. f-SendToLimeade __- 1078179529: . : . : (/ /) 123.123.123.123:443.

, Azure Function, , . IP-. IP- 123.123.123.123 (, ) - IP-, HttpClient. , , .

, . .

:

(!) !

+2
4

, , . 3 6 , . 30 .

-, HttpClient.

public static class Connection
{
    public static HttpClient Client { get; private set; }

    static Connection()
    {
        Client = new HttpClient();

        Client.BaseAddress = new Uri(Config.APIUri);
        Client.DefaultRequestHeaders.Add("Connection", "Keep-Alive");
        Client.DefaultRequestHeaders.Add("Keep-Alive", "timeout=600");
        Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    }
}

HttpClient, . HttpClient , -, HttpClient . , Keep-Alive ( , , , ).

HttpClient, :

var client = Connection.Client;
StringContent httpContent = new StringContent(myQueueItem, Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.PostAsync("/api/devices/data", httpContent);
response.EnsureSuccessStatusCode();

, ( -, ), , HTTP-, . , , , , . , - , .

+1

-, # 3 . , , , .

maxConcurrentCalls , concurrency. , maxConcurrentCalls * instanceCount.

- - , . .

123.123.123.123, / .

afaik , . " " , , .

+1

, : using (var client = new HttpClient())

:

. HttpClient . - .

+1

, , Microsoft -, .

0
source

Source: https://habr.com/ru/post/1690750/


All Articles