Created
November 18, 2014 20:21
-
-
Save mndrix/1525dcbe50bfe5867100 to your computer and use it in GitHub Desktop.
Golang abort HTTP request after header
This file contains 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" | |
"fmt" | |
"net/http" | |
"time" | |
) | |
// Make a GET request for a large (24 MB) file. We only care about a single header field ("ETag") | |
// and want to abort the download as soon as the header is available. This may be necessary | |
// for servers that don't support "If-None-Match". | |
// | |
// In my testing, I can fetch the header in about 250 ms and close the body immediately (<1 ms) | |
// thereafter. | |
func main() { | |
transport := &http.Transport{ | |
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, | |
} | |
httpclient := &http.Client{Transport: transport} | |
req, err := http.NewRequest("GET", "https://resources.lendingclub.com/SecondaryMarketAllNotes.csv", nil) | |
// how long does it take to fetch the header? | |
start := time.Now() | |
resp, err := httpclient.Do(req) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println(resp.Header.Get("etag")) | |
fmt.Printf("Header took: %s\n", time.Now().Sub(start)) | |
// how long to close (discard) the body | |
start = time.Now() | |
resp.Body.Close() | |
fmt.Printf("Close took: %s\n", time.Now().Sub(start)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment