最近在开发一个功能时,须要经过 http 协议上报大量的日志内容,可是在 Go 标准库里的 http client 的 API 是这样的:git
http.NewRequest(method, url string, body io.Reader)
body 是经过io.Reader
接口来传递,并无暴露一个io.Writer
接口来提供写入的办法,先来看看正常状况下怎么写入一个body
,示例:github
buf := bytes.NewBuffer([]byte("hello")) http.Post("localhost:8099/report","text/pain",buf)
须要先把要写入的数据放在Buffer
中,放内存缓存着,可是我须要写入大量
的数据,若是都放内存里确定要 OOM 了,http client 并无提供流式写入
的方法,我这么大的数据量直接用Buffer
确定是不行的,最后在 google 了一番以后找到了解决办法。golang
调用io.pipe()
方法会返回Reader
和Writer
接口实现对象,经过Writer
写数据,Reader
就能够读到,利用这个特性就能够实现流式的写入,开一个协程来写,而后把Reader
传递到方法中,就能够实现 http client body 的流式写入了。缓存
pr, rw := io.Pipe() // 开协程写入大量数据 go func(){ for i := 0; i < 100000; i++ { rw.Write([]byte(fmt.Sprintf("line:%d\r\n", i))) } rw.Close() }() // 传递Reader http.Post("localhost:8099/report","text/pain",buf)
了解 go 中 http client 对于 body 的传输是如何处理的。性能
在构建 Request 的时候,会断言 body 参数的类型,当类型为*bytes.Buffer
、*bytes.Reader
、*strings.Reader
的时候,能够直接经过Len()
方法取出长度,用于Content-Length
请求头,相关代码net/http/request.go#L872-L914:优化
if body != nil { switch v := body.(type) { case *bytes.Buffer: req.ContentLength = int64(v.Len()) buf := v.Bytes() req.GetBody = func() (io.ReadCloser, error) { r := bytes.NewReader(buf) return ioutil.NopCloser(r), nil } case *bytes.Reader: req.ContentLength = int64(v.Len()) snapshot := *v req.GetBody = func() (io.ReadCloser, error) { r := snapshot return ioutil.NopCloser(&r), nil } case *strings.Reader: req.ContentLength = int64(v.Len()) snapshot := *v req.GetBody = func() (io.ReadCloser, error) { r := snapshot return ioutil.NopCloser(&r), nil } default: } if req.GetBody != nil && req.ContentLength == 0 { req.Body = NoBody req.GetBody = func() (io.ReadCloser, error) { return NoBody, nil } } }
在连接创建的时候,会经过body
和上一步中获得的ContentLength
来进行判断,若是body!=nil
而且ContentLength==0
时,可能就会启用Chunked
编码进行传输,相关代码net/http/transfer.go#L82-L96:google
case *Request: if rr.ContentLength != 0 && rr.Body == nil { return nil, fmt.Errorf("http: Request.ContentLength=%d with nil Body", rr.ContentLength) } t.Method = valueOrDefault(rr.Method, "GET") t.Close = rr.Close t.TransferEncoding = rr.TransferEncoding t.Header = rr.Header t.Trailer = rr.Trailer t.Body = rr.Body t.BodyCloser = rr.Body // 当body为非nil,而且ContentLength==0时,这里返回-1 t.ContentLength = rr.outgoingLength() // TransferEncoding没有手动设置,而且请求方法为PUT、POST、PATCH时,会启用chunked编码传输 if t.ContentLength < 0 && len(t.TransferEncoding) == 0 && t.shouldSendChunkedRequestBody() { t.TransferEncoding = []string{"chunked"} }
按照对源码的理解,能够得知在使用io.pipe()
方法进行流式传输时,会使用chunked
编码进行传输,经过如下代码进行验证:编码
func main(){ http.HandleFunc("/report", func(writer http.ResponseWriter, request *http.Request) { }) http.ListenAndServe(":8099", nil) }
func main(){ pr, rw := io.Pipe() go func(){ for i := 0; i < 100; i++ { rw.Write([]byte(fmt.Sprintf("line:%d\r\n", i))) } rw.Close() }() http.Post("localhost:8099/report","text/pain",buf) }
先运行服务端,而后运行客户端,而且使用WireShake
进行抓包分析,结果以下:url
能够看到和预想的结果同样。spa
在数据量大的时候chunked
编码会增长额外的开销,包括编解码和额外的报文开销,能不能不用chunked
编码来进行流式传输
呢?经过源码能够得知,当ContentLength
不为 0 时,若是能预先计算出待传输的body size
,是否是就能避免chunked
编码呢?思路就到这,接着就是写代码验证:
func main(){ http.HandleFunc("/report", func(writer http.ResponseWriter, request *http.Request) { }) http.ListenAndServe(":8099", nil) }
count := 100 line := []byte("line\r\n") pr, rw := io.Pipe() go func() { for i := 0; i < count; i++ { rw.Write(line) } rw.Close() }() // 构造request对象 request, err := http.NewRequest("POST", "http://localhost:8099/report", pr) if err != nil { log.Fatal(err) } // 提早计算出ContentLength request.ContentLength = int64(len(line) * count) // 发起请求 http.DefaultClient.Do(request)
抓包结果:
能够看到确实直接使用的Content-Length
进行传输,没有进行chunked
编码了。
本文的目的主要是记录 go 语言中http client
如何进行流式的写入,并经过阅读源码了解http client
内部对 body 的写入是如何进行处理的,经过两个验证能够得知,若是能提早计算出ContentLength
而且对性能要求比较苛刻的状况下,能够经过手动设置ContentLength
来优化性能。