【问题标题】: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>
    
    • 一旦设置了“REMOTE_HOST”变量,框架将在以后被 UrlRewriter 访问时避免解析它。
    • 这是一个理论。它适用于我的 Visual Studio 调试会话,但不适用于我得到 ​​System.NotSupportedException 的单声道目标框架。
    • 我做了一个错误报告https://github.com/sethyates/urlrewriter/issues/9
    • 同时,我将重写规则转移到了 nginx 配置中,并在生产中剥离了 UrlRewriter。

    【讨论】:

      猜你喜欢
      • 2015-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多