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.)

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

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?