【问题标题】:Web requests becoming very slow when DNS server unreachable当 DNS 服务器无法访问时,Web 请求变得非常缓慢
【发布时间】:2016-12-21 14:06:31
【问题描述】:
QA 团队发现,当网络服务器(为嵌入式设备 UI 提供服务)在主 DNS 服务器无法访问的系统上运行时,UI 会变得难以忍受地无响应 - 每个操作大约需要 16 秒。
首先我认为提供静态页面不会遇到这个问题。
排除我的第一个嫌疑人 - 日志记录 - 我终于找到了 Intelligencia.UrlRewriter(nuget 包版本 2.0.0.9)的原因。似乎在每个请求上,重写器都会复制所有服务器变量,其中包括“REMOTE_HOST”。这是一个动态变量,仅在需要时运行。这又涉及到对HttpRequest.UserHostName 的调用。
问题很简单:如何避免在使用 UrlRewriter 的 Web 应用程序中进行反向地址查找?
我自己给出了一个大锤答案,但想听听其他选择。
【问题讨论】:
标签:
c#
.net
performance
url-rewriting
dns
【解决方案1】:
我添加了另一个带有简单 http 模块的类库项目:
public sealed class NoRemoteHostLookup : IHttpModule
{
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.BeginRequest += ContextOnBeginRequest;
}
private void ContextOnBeginRequest(object sender, EventArgs eventArgs)
{
var request = HttpContext.Current?.Request;
if (request != null)
request.ServerVariables["REMOTE_HOST"] = request.ServerVariables["REMOTE_ADDR"];
}
}
然后由 web 应用项目引用并在 web.config 在 UrlRewriter 之前配置:
<system.web>
<httpModules>
<add type="NoRemoteHostLookupModule.NoRemoteHostLookup, NoRemoteHostLookupModule" name="NoRemoteHostLookup" />
<add type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter" name="UrlRewriter" />
</httpModules>
</system.web>