Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/gocolly/colly
Clone or Download
http_trace.go 1.04 KB
Copy Edit Raw Blame History
package colly
import (
"net/http"
"net/http/httptrace"
"time"
)
// HTTPTrace provides a datastructure for storing an http trace.
type HTTPTrace struct {
start, connect time.Time
ConnectDuration time.Duration
FirstByteDuration time.Duration
}
// trace returns a httptrace.ClientTrace object to be used with an http
// request via httptrace.WithClientTrace() that fills in the HttpTrace.
func (ht *HTTPTrace) trace() *httptrace.ClientTrace {
trace := &httptrace.ClientTrace{
ConnectStart: func(network, addr string) { ht.connect = time.Now() },
ConnectDone: func(network, addr string, err error) {
ht.ConnectDuration = time.Since(ht.connect)
},
GetConn: func(hostPort string) { ht.start = time.Now() },
GotFirstResponseByte: func() {
ht.FirstByteDuration = time.Since(ht.start)
},
}
return trace
}
// WithTrace returns the given HTTP Request with this HTTPTrace added to its
// context.
func (ht *HTTPTrace) WithTrace(req *http.Request) *http.Request {
return req.WithContext(httptrace.WithClientTrace(req.Context(), ht.trace()))
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化