jQuery Ajax超时使http请求打开,导致长轮询应用程序内存泄漏

时间:2016-01-14 19:29:07

标签: jquery ajax sockets http cppcms

tl; dr :jQuery的ajax函数似乎没有关闭超时的http请求。因此,每个长轮询请求打开的每个套接字都不会关闭,并且服务器最终会因为打开套接字的数量而拒绝连接。 一旦超时,是否有可能关闭这些连接,以便我可以启动下一个长期民意调查,而不必担心无休止地打开[和随后的孤立]连接?如果没有,我的长轮询方法在哪里出错?

注意:我意识到这可能是预期的行为,但我在所有活动部分之后有一段时间的噩梦并且孤立了我对这个问题的误解。我们将非常感谢您提供的任何指导/清晰度。

我开发了一个简单的应用程序来演示使用lighttpd,cppcms(一个C ++ Web框架)和jQuery的基于推送的通知。客户这样做:

 {% comment %}

      This is your /cart template. If you are using the Ajaxify Cart plugin,
      your form (with action="/cart") layout will be used in the drawer/modal.

      For info on test orders:
        - General http://docs.shopify.com/manual/your-store/orders/test-orders
        - Shopify Payments - http://docs.shopify.com/manual/more/shopify-payments/testing-shopify-payments

    <!-- Bold: Options 4-1 --> 
    <script>function update_qty_builder(builder_id, qty){ jQuery('.'+builder_id+"_qty").val(qty.value); } function remove_product_builder(builder_id){ jQuery('.'+builder_id+"_qty").val(0); jQuery('.'+builder_id+"_qty").parents("form").submit(); }</script>
    {% include 'bold-cart-handler' %}
    <!-- // end Options 4-1 -->

    {% endcomment %}
    {% if cart.item_count > 0 %}

      <form action="/cart" method="post" novalidate class="cart">

        <div class="section-header">
          <h1 class="section-header__title">{{ 'cart.general.title' | t }}</h1>
        </div>

        <div class="cart__row medium-down--hide cart__header-labels">
          <div class="grid--full">
            <div class="grid__item large--one-half push--large--one-half">
              <div class="grid--full">
                <div class="grid__item one-third medium-down--one-third">
                  <span class="h4">{{ 'cart.label.price' | t }}</span>
                </div>
                <div class="grid__item one-third medium-down--one-third text-center">
                  <span class="h4">{{ 'cart.label.quantity' | t }}</span>
                </div>
                <div class="grid__item one-third medium-down--one-third text-right">
                  <span class="h4">{{ 'cart.label.total' | t }}</span>
                </div>
              </div>
            </div>
          </div>
        </div>

        {% comment %}
          Loop through products in the cart
        {% endcomment %}
        {% for item in cart.items %}
    <!-- Bold: Options 4-2 -->
    {% include 'boldoptions' with 'step2' %}
    <!-- // end Options 4-2 -->

        <tr style="{% include 'boldoptions' with 'step4' %}" class="{% include 'boldoptions' with 'step3' %}">
          <div class="cart__row" data-id="{{ item.id }}">
            <div class="grid--full cart__row--table-large">

              <div class="grid__item large--one-half">
                <div class="grid">

                  <div class="grid__item one-third">
                    <a href="{{ item.url | within: collections.all }}" class="cart__image">

                      {% comment %}
                        More image size options at:
                          - http://docs.shopify.com/themes/filters/product-img-url
                      {% endcomment %}

    <!-- Bold: Options 4-5 -->
    {% if builder[0] %}
    <img src="{{ builder[1] }}" alt="{{ builder[0] }}" /> 
    {% else %}

                      <img src="{{ item | img_url: 'medium' }}" alt="{{ item.title | escape }}">
                    </a>


    <!-- Bold: Options 4-6 -->
    {% include 'boldoptions' with 'step6' %}
    <!-- // end Options 4-6 -->

    {% endif %}
     <!-- // end Options 4-5 -->


                  </div>

                  <div class="grid__item two-thirds">
                    <a href="{{ item.url }}" class="h4">
                      {{ item.product.title }}
                    </a>
                    {% unless item.variant.title contains 'Default' %}
                      <br>
                      <small>{{ item.variant.title }}</small>
                    {% endunless %}

                    {% if settings.cart_vendor_enable %}
                      <p>{{ item.vendor }}</p>
                    {% endif %}

                    {% comment %}
                      Optional, loop through custom product line items if available

                      For more info on line item properties, visit:
                        - http://docs.shopify.com/support/your-store/products/how-do-I-collect-additional-information-on-the-product-page-Like-for-a-monogram-engraving-or-customization
                    {% endcomment %}

                    {% include 'product_customizer_cart' %} 

                    {% if item.properties.size > 0 %}
                      {% for p in item.properties %}
                        {% unless p.last == blank %}
                          {{ p.first }}:

                          {% comment %}
                            Check if there was an uploaded file associated
                          {% endcomment %}
                          {% if p.last contains '/uploads/' %}
                            <a href="{{ p.last }}">{{ p.last | split: '/' | last }}</a>
                          {% else %}
                            {{ p.last }}
                          {% endif %}

                          <br>
                        {% endunless %}
                      {% endfor %}
                    {% endif %}

                    <a href="{% include 'boldoptions' with 'step9' %}" data-id="{{ item.id }}" class="{% include 'boldoptions' with 'step10' %} cart__remove" {% include 'boldoptions' with 'step11' %}>
                      <small>{{ 'cart.general.remove' | t }}</small>
                    </a>
                  </div>

                </div>
              </div>

              <div class="grid__item large--one-half">
                <div class="grid--full cart__row--table-large">

                  <div class="grid__item one-third">
                    <span class="cart__mini-labels">{{ 'cart.label.price' | t }}</span>
                    <span class="h5">{% include 'boldoptions' with 'step12' %}</span>
                  </div>

                  <div class="grid__item one-third text-center">
                    <span class="cart__mini-labels">{{ 'cart.label.quantity' | t }}</span>
                    {% comment %}
                      Added data-id for the ajax cart implementation only.
                    {% endcomment %}
                    <input type="number" name="updates[]" id="updates_{{ item.id }}" class="{% include 'boldoptions' with 'step7' %}" value="{{ item.quantity }}" min="0" data-id="{{ item.id }}" {% include 'boldoptions' with 'step8' %}>
                  </div>

                  <div class="grid__item one-third text-right">
                    <span class="cart__mini-labels">{{ 'cart.label.total' | t }}</span>
                    <span class="h5">{% include 'boldoptions' with 'step13' %}</span>
                  </div>

                </div>
              </div>

            </div>
          </div>
        {% endfor %}

        <div class="cart__row">
          <div class="grid">
            {% comment %}
              Optional, add a textarea for special notes
                - Your theme settings can turn this on or off. Default is on.
                - Make sure you have name="note" for the message to be submitted properly
            {% endcomment %}
            {% if settings.cart_notes_enable %}
              {% assign noteSize = cart.note | size %}
              <div class="grid__item large--five-twelfths">
                <button type="button" class="text-link cart__note-add{% if noteSize > 0 %} is-hidden{% endif %}">
                  {{ 'cart.label.add_note' | t }}
                </button>
                <div class="cart__note{% if noteSize > 0 %} is-active{% endif %}">
                  <label for="CartSpecialInstructions">{{ 'cart.general.note' | t }}</label>
                  <textarea name="note" class="input-full" id="CartSpecialInstructions">{{ cart.note }}</textarea>
                </div>
              </div>
            {% endif %}
            <div class="grid__item text-right{% if settings.cart_notes_enable %} large--seven-twelfths{% endif %}">
              <p>
                <span class="cart__subtotal-title">{{ 'cart.general.subtotal' | t }}</span>
                <span class="h5 cart__subtotal">{{ cart.total_price | money }}</span>
              </p>
              <p><em>{{ 'cart.general.shipping_at_checkout' | t }}</em></p>
              <input type="submit" name="update" class="btn--secondary update-cart" value="{{ 'cart.general.update' | t }}">
              <input type="submit" name="checkout" class="btn" value="{{ 'cart.general.checkout' | t }}">

              {% if additional_checkout_buttons %}
                <div class="cart__additional_checkout">{{ content_for_additional_checkout_buttons }}</div>
              {% endif %}
            </div>
          </div>

        </div>

      </form>

    {% else %}
      {% comment %}
        The cart is empty
      {% endcomment %}
      <h2>{{ 'cart.general.title' | t }}</h2>
      <p>{{ 'cart.general.empty' | t }}</p>
      <p>{{ 'cart.general.continue_browsing_html' | t }}</p>
    {% endif %}


    <!-- Bold: Options 4-14 -->
    {% include 'bold-cart-modal' %}
    <!-- // end Options 4-14 -->

然后服务器执行类似这样的操作(我在python Flask微框架中完成了类似的测试,结果与以下cppcms实现相同):

function listen(){
  $.ajax({
    url: "/push_test",
    type: "POST",
    timeout: 3000, // 3 seconds
    data: {
      will_leak: true
    },
    success: function(r){
      console.log("server responded: " r);
    },
    error: function(xhr, st, e){
      console.log("push_test error");
      console.dir(xhr);
      console.log("status: " + st);
      console.log("error: " + e);
    },
    complete: function(){
      //listen();
    }
  });
}

当ajax调用超时时检查Firefox的开发人员工具的网络选项卡指示HTTP请求从未返回响应或状态代码,但控制台指示&#34;错误&#34中的超时失败;回调ajax调用。对Chrome浏览器开发人员工具网络标签的类似检查表明该请求已被取消&#34;当我们达到超时时。检查服务器上的打开套接字文件描述符表示超时的套接字处于CLOSE_WAIT状态,并且它们一直保持这种状态,直到我将服务器关闭并再次备份。

如果您花时间阅读所有这些内容,感谢您,如果您需要更多信息来帮助我在此处找出问题,请与我们联系。我会在此期间继续挖掘。

0 个答案:

没有答案