Created
March 29, 2016 10:40
-
-
Save nobonobo/bc3cba5bc97b1ff0f90c to your computer and use it in GitHub Desktop.
httpクライアントBodyの読み残し問題
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "crypto/tls" | |
| "encoding/json" | |
| "fmt" | |
| "io" | |
| "io/ioutil" | |
| "log" | |
| "net/http" | |
| "time" | |
| ) | |
| func withDrain(client *http.Client, url string) interface{} { | |
| resp, err := client.Get(url) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| defer func() { | |
| io.Copy(ioutil.Discard, resp.Body) | |
| resp.Body.Close() | |
| }() | |
| fmt.Println(url, resp.Status) | |
| var v interface{} | |
| if err := json.NewDecoder(resp.Body).Decode(&v); err != nil { | |
| log.Fatal(err) | |
| } | |
| return v | |
| } | |
| func withoutDrain(client *http.Client, url string) interface{} { | |
| resp, err := client.Get(url) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| defer func() { | |
| resp.Body.Close() | |
| }() | |
| fmt.Println(url, resp.Status) | |
| var v interface{} | |
| if err := json.NewDecoder(resp.Body).Decode(&v); err != nil { | |
| log.Fatal(err) | |
| } | |
| return v | |
| } | |
| func run(url string) { | |
| client := &http.Client{ | |
| CheckRedirect: func(req *http.Request, via []*http.Request) error { | |
| log.Println(via) | |
| return nil | |
| }, | |
| Transport: &http.Transport{ | |
| TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, | |
| }, | |
| } | |
| for i := 0; i < 2; i++ { | |
| begin := time.Now() | |
| withDrain(client, url) | |
| fmt.Println("with-dorain:", i+1, time.Since(begin)) | |
| } | |
| client = &http.Client{ | |
| CheckRedirect: func(req *http.Request, via []*http.Request) error { | |
| log.Println(via) | |
| return nil | |
| }, | |
| Transport: &http.Transport{ | |
| TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, | |
| }, | |
| } | |
| for i := 0; i < 2; i++ { | |
| begin := time.Now() | |
| withoutDrain(client, url) | |
| fmt.Println("without-dorain:", i+1, time.Since(begin)) | |
| } | |
| } | |
| func main() { | |
| run("https://api.github.com/repos/nobonobo/jsonrpc") | |
| } |
Author
Author
そして上記は取りやめになって https://go-review.googlesource.com/#/c/21291/3/src/net/http/transport.go httpのtransportが修正・マージされましたと。
Author
結果は golang/go@18072ad これ。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
それで https://go-review.googlesource.com/#/c/21290/ このパッチというわけか。