翔云

Just try, don't shy. 最新文章请点击
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

HTTP 客户端接收数据超时

Posted on 2019-04-13 21:37  翔云123456  阅读(1115)  评论(0编辑  收藏  举报

本文使用两种方式测试HTTP 客户端读数据超时的情况,一种是直接使用golang自带的http库,另一种是使用martini web框架。

1. 测试1--直接使用http库

1.1.启动server

server端的请求响应函数,会睡眠10s,再返回结果。这样,可以模拟server端处理慢的情况。

package  main

import (
        "log"
        "net/http"
        "time"
)


func main() {
        http.HandleFunc("/", handler)

        log.Fatal(http.ListenAndServe(":3000", nil))
}


func handler(w http.ResponseWriter, r *http.Request){
        log.Println("start...")
        time.Sleep(10*time.Second)
        w.Write([]byte("Hello world"))
        log.Println("end...")
}

1.2.client发起请求

使用curl 发起请求,最大等待时间3s

curl -m 3 http://localhost:3000/
curl: (28) Operation timed out after 3000 milliseconds with 0 bytes received

3s后,超时退出

1.3.server端输出

 ./server
2019/04/11 10:17:28 start...
2019/04/11 10:17:38 end...

handler()等待10s后返回。
没有其他报错。

连接建立后,3s时,client断开连接,server端处理函数继续执行,等10s后,返回结果。

通过tcpdump抓包发现,当client断开连接,server端再向client发送数据,会收到RST。

2.测试2--使用框架

使用martini web框架作为HTTP server。

2.1.启动 HTTP server

server 端请求响应函数逻辑同上。

server代码如下:

package main

import (
        "log"
        "time"
        "github.com/go-martini/martini"
)

func main() {
  m := martini.Classic()
  m.Get("/", handler)
  m.Run()
}



func handler() string{
        log.Println("start...")

        time.Sleep(10*time.Second)

        log.Println("end...")
        return "Hello world!"
}


请求处理函数handler()睡眠10s,然后才返回。

2.2.client发起请求

使用curl 发起请求,最大等待时间3s

curl -m 3 http://localhost:3000/
curl: (28) Operation timed out after 3000 milliseconds with 0 bytes received

3s后,超时退出

2.3.server 端输出

./server
[martini] listening on :3000 (development)
[martini] Started GET / for [::1]:50271
2019/04/11 09:55:33 start...
2019/04/11 09:55:43 end...
[martini] Completed 200 OK in 10.003468104s

handler()等待10s后返回。
没有其他报错。

连接建立后,3s时,client断开连接,server端处理函数继续执行,等10s后,返回结果。

通过tcpdump抓包发现,当client断开连接,server端再向client发送数据,会收到RST。

3.问题:连接已经断开,为什么server返回结果时没有报错?

当client断开连接,server端再向client发送数据,会收到RST。

如果client断开连接后,server连续两次向client发送数据,则第二次向client发送数据时,会报错broken pipe,因为收到RST,连接已经不存在。