Skip to content

Instantly share code, notes, and snippets.

@mndrix
Created November 18, 2014 20:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mndrix/1525dcbe50bfe5867100 to your computer and use it in GitHub Desktop.
Save mndrix/1525dcbe50bfe5867100 to your computer and use it in GitHub Desktop.
Golang abort HTTP request after header
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