我正在尝试设置一个计时器来计算我的服务器完成请求所需的时间,我希望计时器在响应的最后一个字节发送后停止.
我发现http服务器只会在处理函数返回后发送响应.
有没有办法在发送响应后添加回调?
或者有更好的方法来计算从请求的第一个字节到响应的最后一个字节字节发送的时间吗?
1> John S Peray..:
更容易但不那么准确的方法是使用中间件来包装你的处理函数.
func timer(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
startTime := time.Now()
h.ServeHTTP(w, r)
duration := time.Now().Sub(startTime)
})
}
然后
http.Handle("/route",timer(yourHandler))
这更准确地说是处理请求和形成响应所花费的时间,而不是写入之间的时间.
net/http
这将是围绕这里.
go c.serve(ctx)
for {
rw, e := l.Accept()
if e != nil {
if ne, ok := e.(net.Error); ok && ne.Temporary() {
if tempDelay == 0 {
tempDelay = 5 * time.Millisecond
} else {
tempDelay *= 2
}
if max := 1 * time.Second; tempDelay > max {
tempDelay = max
}
srv.logf("http: Accept error: %v; retrying in %v", e, tempDelay)
time.Sleep(tempDelay)
continue
}
return e
}
tempDelay = 0
c := srv.newConn(rw)
c.setState(c.rwc, StateNew) // before Serve can return
go func(){
startTime := time.Now()
c.serve(ctx)
duration := time.Now().Sub(startTime)
}()
}
net.Connl.Accept()