当请求没有收到响应时会发生什么?我看到重试 [英] What happens when no response is received for a request? I'm seeing retries

查看:21
本文介绍了当请求没有收到响应时会发生什么?我看到重试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我的问题可能更多是与浏览器相关的问题,但这是一个非常基本的问题,我也想在我冒险构建 Web 应用程序时找到答案.

The question I have is probably more of a browser related question I think, but its a pretty fundamental one I'd like to find the answer too as I venture into building a web application.

在我的客户端代码中,我正在执行一个 $.ajax 调用.此帖子可能需要一段时间才能回复.我看到的是在一定时间后再次发送请求.

In my client side code I am doing an $.ajax call. This Post can take a while to respond. What I'm seeing is after a certain amount of time the request is being send again.

我还以为是我的$.ajax调用再次发送了,但是无论我在服务器上看到多少次POST请求,我都只看到了beforeSend回调调用一次.我相当确定我的代码不会多次发送,所以我认为是浏览器重试?

I thought it was my $.ajax call sending it again, but no matter how many times I see the POST request on the server, I only see the beforeSend callback called once. I am fairly sure my code isn't sending it more than once, so I think its the browser retrying?

我知道我的服务器在运行 Wireshark 时多次收到请求,并且可以多次看到发布请求.所以我的假设是这与 HTTP 有关?即,如果在一定时间内未收到响应,则重新发送请求?

I know my server is getting the request more then once as I ran up Wireshark and can see the post request multiple times. So my assumption is this is something to do with HTTP? I.e., if a response isn't received within a certain amount of time then the request is resent?

以下是我的通话示例.

$.ajax({
                    async: false,
                    type: 'POST',
                    url: '<%= url_for('importDevice') %>',
                    data: { device: val },
                    retryLimit: 0,
                    //callback
                    success: function(data) {
                    alert('calling import');
                    if ( data == 'nomaster')
                    {
                            // Display a warning  toast, with a title
                            toastr.warning('You must set the Master Key first!', 'Warning');
                            $.ismasterset = false;
                            //reset the form contents after added
                    } else
                    {
                            $("div#content").html(data);
                    }
                    },
                     beforeSend: function(){
                    alert('in before send');
                    }
            });

这是所有相关代码,'retryLimit' 没有被使用,我只是没有从我的代码中删除它,是的,在我把它放进去之前问题就存在了.

This is all of the relevent code, 'retryLimit' isn't being used, I just haven't removed it from my code and yes the problem was there before I put it in.

使用客户端和服务器的输出进行编辑.

好的,我为 Firefox 安装了Live Http headers".

ok I installed 'Live Http headers for Firefox'.

在生成器"选项卡中,我看到了一个调用

In the 'Generator' tab I see the one single call

'#request# POST http://testhost/importdevice' 

不过,我在标题"部分没有看到 POST,也许是因为没有响应?

I don't see the POST in the 'headers' section though, maybe thats because there's no response?

在我的网络服务器中,虽然我看到 2 个调用间隔大约 22 秒.

In my webserver though I see 2 calls about 22 seconds apart.

[Sun Jan 13 03:08:45 2013] [debug] POST /importdevice (Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:17.0) Gecko/20100101 Firefox/17.0).

[Sun Jan 13 03:09:07 2013] [debug] POST /importdevice (Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:17.0) Gecko/20100101 Firefox/17.0).

我也可以在wireshark中看到这些相同的调用...

I can also see these same calls in wireshark...

这就是为什么我问这是否是正常行为,如果响应没有返回,尝试重新发送请求,以类似于 TCP 握手和 SYN 重传的方式.

This is why I was asking if this is normal behaviour to try to resend the request if a response doesn't come back, in a similar fashion to a TCP handshake and the SYN retransmission.

新更新

这似乎与我的 Ajax 调用无关.如果我创建一个带有简单 HREF 的按钮.

It doesn't seem to have anything to do with my Ajax call. If I create a button with a simple HREF.

<a href="/importdevice?device=serverA" class="btn btn-success">TestDirect</a>

然后在我的实时 HTTP 标头输出"中,我得到...只有一个实例.

Then in my 'Live HTTP headers output I get... just one instance.

#request# GET http://172.16.118.15/importdevice?device=serverA

但是我再次在我的服务器日志中得到了.

But once again in my server logs I get.

[Sun Jan 13 03:20:25 2013] [debug] GET /importdevice (Mozilla/5.0 (Macintosh; Intel Mac OS  X 10.8; rv:17.0) Gecko/20100101 Firefox/17.0).
[Sun Jan 13 03:20:48 2013] [debug] GET /importdevice (Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:17.0) Gecko/20100101 Firefox/17.0).

在服务器上的wireshark中,我也看到了两次......正如预期的那样,我的服务器代码也被调用了两次......这对我来说真的很困惑.

And in wireshark on the server I'm seeing it twice as well... and as expected my server code is called twice too... This is REALLY confusing to me.

推荐答案

查看这篇解释正在发生的事情的博客文章:http://geek.starbean.net/?p=393

Checkout this blog post that explains what is happening: http://geek.starbean.net/?p=393

根据 HTTP/1.1 RFC 8.2.4:

如果 HTTP/1.1 客户端发送的请求包含请求正文,但不包含具有100-continue"期望的 Expect 请求标头字段,并且如果客户端未直接连接到 HTTP/1.1 原始服务器,如果客户端在从服务器接收到任何状态之前看到连接关闭,客户端应该重试请求.

If an HTTP/1.1 client sends a request which includes a request body, but which does not include an Expect request-header field with the "100-continue" expectation, and if the client is not directly connected to an HTTP/1.1 origin server, and if the client sees the connection close before receiving any status from the server, the client SHOULD retry the request.

确保您设计的网络应用能够容忍这些额外的请求.如果您希望 ajax 请求只做一次某事,例如向数据库写入一些内容,那么您需要将请求设计为幂等的.请参阅:什么是幂等操作?

Make sure you design your web app to tolerate these extra requests. And if you want the ajax request to do something only once, such as writing something to a database, then you need to design the request as idempotent. See: What is an idempotent operation?

此外,这突出了 GET 和 POST 操作之间的差异:http://www.cs.tut.fi/~jkorpela/forms/methods.html

Also, this highlights the differences between GET and POST operations: http://www.cs.tut.fi/~jkorpela/forms/methods.html

作为一般设计实践:
-GET 操作应该设计为幂等的
-POST操作应该用于写入数据库等

As a general design practice:
-GET operations should be designed as idempotent
-POST operations should be used for writing to databases, etc.

这篇关于当请求没有收到响应时会发生什么?我看到重试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆