duanpasi6287 2017-12-01 10:26 采纳率: 100%
浏览 125
已采纳

request.context不会关闭连接

I'm using context.Context to cancel a http request

I find although I got the "context cancel", the underlying socket connection is still available, and I can get the response after a few seconds. Is it designed this way to read response once request is set out?

This is the code

func SendRequest(ctx context.Context, url string) {
    req, err := http.NewRequest("GET", url, nil)
    if err != nil {
        fmt.Println(err)
    }
    req = req.WithContext(ctx)

    res, err := client.Do(req)
    select {
    case <-ctx.Done():
        fmt.Printf("%s Canceled
", url)
        //client.Transport.(*http.Transport).CancelRequest(req)
        //client.Transport.(*http.Transport).CloseIdleConnections()
    }
    if res != nil {
        defer res.Body.Close()
    }
    if err != nil {
        fmt.Printf("Failed: %v
", err)
    } else {
        io.Copy(ioutil.Discard, res.Body)
        fmt.Printf("return status: %d
", url, res.StatusCode)
    }
}

The URL I'm requesting will return after several seconds, so I can still read the response body, and the connection was closed after the process exits.

Here is a simple code to reproduce the issue

func client() {
    ctx, cancel := context.WithCancel(context.Background())
    client := http.DefaultClient
    request, _ := http.NewRequest("GET", "http://127.0.0.1:9090", nil)
    req := request.WithContext(ctx)

    go func() {
        client.Do(req)
    }()

    time.Sleep(time.Duration(1) * time.Second)
    cancel()

    <-time.After(time.Duration(10) * time.Second)

}

func sayhelloName(w http.ResponseWriter, r *http.Request) {
    time.Sleep(time.Duration(10) * time.Second)
    fmt.Fprintf(w, "Hello world!")
}

func server() {
    http.HandleFunc("/", sayhelloName)
    err := http.ListenAndServe(":9090", nil)
    if err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}

connection was closed after exactly 10 second
connection was closed after exactly 10 second

  • 写回答

1条回答 默认 最新

  • douduan7295 2017-12-01 11:23
    关注

    You don't need to do anything yourself to cancel a request when the context is cancelled. This is already handled by the standard http package as documented.

    All you should need is:

    func SendRequest(ctx context.Context, url string) {
        req, err := http.NewRequest("GET", url, nil)
        if err != nil {
            panic(err)
        }
        req = req.WithContext(ctx)
    
        res, err := client.Do(req)
        if err != nil {
            panic(err)
        }
        defer res.Body.Close()
        io.Copy(ioutil.Discard, res.Body)
        fmt.Printf("return status: %d
    ", url, res.StatusCode)
    }
    

    EDIT

    Your TCP dump confirms that everything is working as it should, and precisely as I have described above.

    To break it down:

    Your outbound request is indeed being cancelled after one second, as your TCP dump confirms:

    6 2017-12-04 06:10:21.864955 0.0993000      40 TCP       17503 → 9090 [FIN, ACK] Seq=96 Ack=1 Win=8192 Len=0
    7 2017-12-04 06:10:21.864955 0.0000000      40 TCP       9090 → 17503 [ACK] Seq=1 Ack=97 Win-7936 Len=0
    

    That FIN on line 6 means the HTTP client is telling the server "I'm done talking to you". The ACK on line 7 means the server responded to the request, and closed the connection.

    However, your HTTP handler ignores the cancelled request,and attempts to respond anyway, generating line 8:

    8 2017-12-04 06:10:30.868955 9.004000     169 HTTP      HTTP/1.1 200 OK  (text/plain)
    

    But then the server receives the message that the connection is invalid:

    9 2017-12-04 06:10:30.868955  0.000000      40 TCP       17503 → 8080 [RST, ACK] Seq=97 Ack=130 Win=0 Len=0
    

    The RST means the connection has been reset, and is currently invalid. This is because the connection was terminated 9 seconds earlier.

    So you see, your request is cancelled immediately, exactly as it should.

    The only improvement you could add to your code is to have your HTTP handler in the server actually detect and honor such cancellations, by exiting early when the incoming request has been cancelled.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 模电中二极管,三极管和电容的应用
  • ¥15 关于模型导入UNITY的.FBX: Check external application preferences.警告。
  • ¥15 气象网格数据与卫星轨道数据如何匹配
  • ¥100 java ee ssm项目 悬赏,感兴趣直接联系我
  • ¥15 微软账户问题不小心注销了好像
  • ¥15 x264库中预测模式字IPM、运动向量差MVD、量化后的DCT系数的位置
  • ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused
  • ¥20 关于web前端如何播放二次加密m3u8视频的问题
  • ¥15 使用百度地图api 位置函数报错?
  • ¥15 metamask如何添加TRON自定义网络