本文使用两种方式测试HTTP 客户端读数据超时的情况,一种是直接使用golang自带的http库,另一种是使用martini web框架。
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...")
- }
-
-
使用curl 发起请求,最大等待时间3s
- curl -m 3 http://localhost:3000/
- curl: (28) Operation timed out after 3000 milliseconds with 0 bytes received
-
3s后,超时退出
- ./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。
使用martini web框架作为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,然后才返回。
使用curl 发起请求,最大等待时间3s
- curl -m 3 http://localhost:3000/
- curl: (28) Operation timed out after 3000 milliseconds with 0 bytes received
-
3s后,超时退出
- ./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。
当client断开连接,server端再向client发送数据,会收到RST。
如果client断开连接后,server连续两次向client发送数据,则第二次向client发送数据时,会报错broken pipe,因为收到RST,连接已经不存在。