请求可访问网页时 HttpClient 超时
我们实施了简单的健康检查来验证网站是否可用。 我们在生产中面临超时。
已注册一个命名的 HTTP 客户端
。 客户端向网站的公共页面发送一个简单的 GET 请求。我可以在几秒钟内通过浏览器访问该页面。 有接受标头来接受任何内容类型。 请求完成设置为读取的标头。 超时设置为 30 秒。执行需要 30 秒。 该网站位于负载均衡器 (CloudFront) 后面。
注册:
services.AddHttpClient("ClientName", client =>
{
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*"));
client.BaseAddress = ServiceUri;
}).AddHttpMessageHandler<HttpLoggingHandler>();
handler:
// copy response to memory stream
using MemoryStream stream = recyclableMemoryStreamManager.GetStream();
await response.Content.CopyToAsync(stream);
复制后写回响应内容。我们有多个客户遵循相同的模式,没有出现任何问题。
调用:
using HttpClient httpClient = httpClientFactory.CreateClient(context.Registration.Name);
using HttpResponseMessage response = await httpClient.GetAsync(
options.HealthUri,
HttpCompletionOption.ResponseHeadersRead,
source.Token); // the token expires in 30 secs
是否缺少任何请求标头?
可能是 HttpLoggingHandler 问题吗?它尝试读取请求/响应内容并将其写入日志。在这种情况下,HttpCompletionOption.ResponseHeadersRead 标头会导致问题吗?可能是消息处理程序的 IHttpClientFactory 内部缓存的原因吗?
默认的 System.Net.Http.IHttpClientFactory 实现可能会缓存底层 System.Net.Http.HttpMessageHandler 实例以提高性能。
任何想法表示赞赏。
We have implemented a simple health check to verify a website is available.
We are facing timeouts in production.
There is a named HTTP client
registered.
The client sends a simple GET request to a public page of the website. I can reach the page via browser in several seconds.
There is the accept header to accept any content type.
The request completion is set to headers read.
The timeout is set to 30 seconds. The execution takes 30 seconds.
The website is behind a load balancer (CloudFront).
The registration:
services.AddHttpClient("ClientName", client =>
{
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*"));
client.BaseAddress = ServiceUri;
}).AddHttpMessageHandler<HttpLoggingHandler>();
The handler:
// copy response to memory stream
using MemoryStream stream = recyclableMemoryStreamManager.GetStream();
await response.Content.CopyToAsync(stream);
The response content is written back after the copy. We have multiple clients following the same pattern without an issue.
The call:
using HttpClient httpClient = httpClientFactory.CreateClient(context.Registration.Name);
using HttpResponseMessage response = await httpClient.GetAsync(
options.HealthUri,
HttpCompletionOption.ResponseHeadersRead,
source.Token); // the token expires in 30 secs
Is there any request header missing?
Can be the HttpLoggingHandler
issue? It tries to read the request/response content and write it to the logs. Can be HttpCompletionOption.ResponseHeadersRead header causing an issue in this case? Can be the reason in the IHttpClientFactory's internal cache of the message handlers?
The default System.Net.Http.IHttpClientFactory implementation may cache the underlying System.Net.Http.HttpMessageHandler instances to improve performance.
Any ideas are appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论