Created
November 22, 2019 20:47
-
-
Save bradfitz/260851776f08e4bc4dacedd82afa7aea to your computer and use it in GitHub Desktop.
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 ( | |
"bytes" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"time" | |
) | |
var tr = http.DefaultTransport.(*http.Transport).Clone() | |
func main() { | |
tr.IdleConnTimeout = 5 * time.Second | |
for { | |
req() | |
time.Sleep(time.Second) | |
} | |
} | |
func req() { | |
req, _ := http.NewRequest("GET", "https://http1.golang.org/reqinfo", nil) | |
res, err := tr.RoundTrip(req) | |
if err != nil { | |
log.Printf("error: %v", err) | |
return | |
} | |
slurp, err := ioutil.ReadAll(res.Body) | |
if i := bytes.Index(slurp, []byte("RequestURI:")); i != -1 { | |
slurp = slurp[:i] | |
} | |
log.Printf("got: %v, %q", res.Status, slurp) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please also be prepared to handle the error from slurp on line 28: