コード例 #1
0
ファイル: httptrace.go プロジェクト: kyokomi-sandbox/sandbox
func getTrace() {
	req, err := http.NewRequest("GET", "https://aphro-api.appspot.com/v1/articles", nil)
	if err != nil {
		log.Fatal(err)
	}

	// Set hooks
	trace := httptrace.ClientTrace{
		GetConn: func(h string) {
			log.Println("GetConn:", h)
		},
	}

	ctx := httptrace.WithClientTrace(context.Background(), &trace)
	req = req.WithContext(ctx)

	client := http.DefaultClient
	res, err := client.Do(req)
	if err != nil {
		log.Fatal(err)
	}
	defer res.Body.Close()

	j, err := simplejson.NewFromReader(res.Body)
	if err != nil {
		log.Fatal(err)
	}
	pp.Println(j)
}
コード例 #2
0
ファイル: example_test.go プロジェクト: hurkgu/go
func ExampleTrace() {
	req, _ := http.NewRequest("GET", "http://example.com", nil)
	trace := &httptrace.ClientTrace{
		GotConn: func(connInfo httptrace.GotConnInfo) {
			fmt.Printf("Got Conn: %+v\n", connInfo)
		},
		DNSDone: func(dnsInfo httptrace.DNSDoneInfo) {
			fmt.Printf("DNS Info: %+v\n", dnsInfo)
		},
	}
	req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
	http.DefaultClient.Do(req)
}
コード例 #3
0
ファイル: httptrace.go プロジェクト: soarpenguin/go-scripts
func main() {
	traceCtx := httptrace.WithClientTrace(context.Background(), &httptrace.ClientTrace{
		GetConn: func(hostPort string) {
			fmt.Printf("Prepare to get a connection for %s.\n", hostPort)
		},
		GotConn: func(info httptrace.GotConnInfo) {
			fmt.Printf("Got a connection: reused: %v, from the idle pool: %v.\n",
				info.Reused, info.WasIdle)
		},
		PutIdleConn: func(err error) {
			if err == nil {
				fmt.Println("Put a connection to the idle pool: ok.")
			} else {
				fmt.Println("Put a connection to the idle pool:", err.Error())
			}
		},
		ConnectStart: func(network, addr string) {
			fmt.Printf("Dialing... (%s:%s).\n", network, addr)
		},
		ConnectDone: func(network, addr string, err error) {
			if err == nil {
				fmt.Printf("Dial is done. (%s:%s)\n", network, addr)
			} else {
				fmt.Printf("Dial is done with error: %s. (%s:%s)\n", err, network, addr)
			}
		},
		WroteRequest: func(info httptrace.WroteRequestInfo) {
			if info.Err == nil {
				fmt.Println("Wrote a request: ok.")
			} else {
				fmt.Println("Wrote a request:", info.Err.Error())
			}
		},
		GotFirstResponseByte: func() {
			fmt.Println("Got the first response byte.")
		},
	})

	req, err := http.NewRequest("GET", "http://www.baidu.com/", nil)
	if err != nil {
		log.Fatal("Fatal error:", err)
	}

	req = req.WithContext(traceCtx)
	_, err = http.DefaultClient.Do(req)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Request error: %v\n", err)
		os.Exit(1)
	}
}
コード例 #4
0
ファイル: example_test.go プロジェクト: achanda/go
func Example() {
	req, _ := http.NewRequest("GET", "http://example.com", nil)
	trace := &httptrace.ClientTrace{
		GotConn: func(connInfo httptrace.GotConnInfo) {
			fmt.Printf("Got Conn: %+v\n", connInfo)
		},
		DNSDone: func(dnsInfo httptrace.DNSDoneInfo) {
			fmt.Printf("DNS Info: %+v\n", dnsInfo)
		},
	}
	req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
	_, err := http.DefaultTransport.RoundTrip(req)
	if err != nil {
		log.Fatal(err)
	}
}