dongzhao3040 2015-04-21 16:48
浏览 141
已采纳

Go的http.DefaultClient是否会阻塞到正文的最后一个字节?

For example if I had the code

t := time.Now()
http.Get("google.com")
fmt.Println(time.Now().Sub(t))

Would the printed duration be after the last byte of the response's body or after the last byte of the response's headers has been received?

  • 写回答

1条回答 默认 最新

  • dongzhenjian5195 2015-04-21 17:15
    关注

    The documentation doesn't clearly state that this, but it returns as soon as the response headers have been read.

    As soon as you get a little experience with Go you'll realize that almost every time you get returned an io.Reader (or in this case resp.Body is an io.ReadCloser) it's a streaming reader that doesn't have all the data yet. Just like calling os.Open returns something that can be used as an io.Reader but doesn't read the whole file.

    In many cases with Go, if the documentation is not clear, you can look at the standard package code for enlightenment. Admittedly, in this specific case (as with encoding/json) the code is doing a lot so can be hard to follow at first glance. Most of the standard packages are much easier to follow and learn from.

    Failing that, you can just run a minor extension of the code you gave (on your own machine, the playground doesn't support making TCP connections) to make it pretty clear:

    (note I just picked these URLs somewhat at random, better would be to pick some URL with a large payload that you know no one will mind you fetching for a test)

    package main
    
    import (
        "fmt"
        "io"
        "io/ioutil"
        "net/http"
        "time"
    )
    
    func measure(url string) (t1, t2 time.Duration, n int64, err error) {
        start := time.Now()
        resp, err := http.Get(url)
        if err != nil {
            return
        }
        defer resp.Body.Close()
        t1 = time.Since(start)
        n, err = io.Copy(ioutil.Discard, resp.Body)
        t2 = time.Since(start)
        return
    }
    
    func main() {
        for _, url := range []string{
            "http://google.com/",
            "http://en.wikipedia.org/",
            "http://upload.wikimedia.org/wikipedia/commons/7/76/PIA02863_-_Jupiter_surface_motion_animation.gif",
        } {
            fmt.Printf("fetching %q ", url)
            t1, t2, n, err := measure(url)
            if err != nil {
                fmt.Println("error:", err)
                continue
            }
            fmt.Printf("got %d bytes in %v / %v
    ", n, t1, t2)
        }
    }
    

    E.g. on a somewhat slow connection the final URL measured above says: 597.055907ms / 29.269763851s. (That is, the initial Get call returned quickly whereas reading all the data took ~60 times longer.)

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

报告相同问题?

悬赏问题

  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择