问题描述
我需要构建一个 Azure 函数:
I need to build an Azure Function that:
- 响应
HTTP POST
请求 - 根据数据执行 7 次
HTTP HEAD
请求.
- Responds to a
HTTP POST
request - Does 7x
HTTP HEAD
requests based on the data.
我在这里和这里
但是,不完全清楚该做什么以及它是如何工作的?
But, it's not entirely clear what to do, and how it works?
与第二个链接一样,我目前刚刚声明了一个 private static HttpClient httpClient = new HttpClient();
实例,并在我的 7x HTTP HEAD 调用中重新使用了它.
As in the second link, i've currently just declared a private static HttpClient httpClient = new HttpClient();
instance, and re-used that in my 7x HTTP HEAD calls.
我的问题:
- 这是在无状态 Azure 函数中最有效地使用
HttpClient
吗? - 我目前正在为 http 调用构建一个
List
,然后对它们执行() Task.WhenAll(tasks)
以在其中运行它们平行.这会是打这些电话的最快方法吗?还有其他建议吗?
- Is that the most efficient use of
HttpClient
in a stateless Azure function? - I'm currently just building up a
List<Task>()
for the http calls, then doingTask.WhenAll(tasks)
on them to run them in parrallel. Would that be the quickest way to do these calls? Any other suggestions?
这个 Function 端点会被大量调用(每秒多次),因此需要尽可能高效以降低成本.
This Function endpoint will be called a lot (multiple times a second), so need to be as efficient as possible to keep costs down.
谢谢!
推荐答案
是的 - 这仍然是 Azure Functions 1.x(也适用于 2.x)的当前指南,以最好地避免套接字耗尽.静态变量将确保它将与类的所有实例共享.涵盖此主题的另一篇好文章是 https://aspnetmonsters.com/2016/08/2016-08-27-httpclient错误
Yes - this is still the current guidance for Azure Functions 1.x (and applies to 2.x as well) to best avoid socket exhaustion. The static variable will ensures that it will be shared against all instances of the class. Another good article that covers this topic is https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong
这篇关于Azure Functions 中的 HttpClient 最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!