Added request start time to ServerCtx

This commit is contained in:
Aliaksandr Valialkin
2015-10-21 16:09:35 +03:00
parent 45d00cad56
commit d0deaebb43
2 changed files with 10 additions and 7 deletions
+6 -3
View File
@@ -43,10 +43,12 @@ type ServerCtx struct {
Request Request
Response Response
// Unique id of the context.
// Used by ServerCtx.Logger().
// Unique id of the request.
ID uint64
// Start time for the request processing.
Time time.Time
logger ctxLogger
s *Server
c remoteAddrer
@@ -286,6 +288,8 @@ func (s *Server) serveConn(c io.ReadWriter, ctxP **ServerCtx) error {
}
break
}
ctx.ID++
ctx.Time = time.Now()
s.Handler(ctx)
shadow := atomic.LoadPointer(&ctx.shadow)
if shadow != nil {
@@ -308,7 +312,6 @@ func (s *Server) serveConn(c io.ReadWriter, ctxP **ServerCtx) error {
break
}
}
ctx.ID++
}
return err
}
+4 -4
View File
@@ -172,10 +172,10 @@ func TestServerLogger(t *testing.T) {
verifyResponse(t, br, 200, "text/html", "requestURI=/foo1, body=\"\", remoteAddr=1.2.3.4:8765")
verifyResponse(t, br, 200, "text/html", "requestURI=/foo2, body=\"abcde\", remoteAddr=1.2.3.4:8765")
expectedLogOut := `#0000000100000000 - 1.2.3.4:8765 - GET http://google.com/foo1 - begin
#0000000100000000 - 1.2.3.4:8765 - GET http://google.com/foo1 - end
#0000000100000001 - 1.2.3.4:8765 - POST http://aaa.com/foo2 - begin
#0000000100000001 - 1.2.3.4:8765 - POST http://aaa.com/foo2 - end
expectedLogOut := `#0000000100000001 - 1.2.3.4:8765 - GET http://google.com/foo1 - begin
#0000000100000001 - 1.2.3.4:8765 - GET http://google.com/foo1 - end
#0000000100000002 - 1.2.3.4:8765 - POST http://aaa.com/foo2 - begin
#0000000100000002 - 1.2.3.4:8765 - POST http://aaa.com/foo2 - end
`
if cl.out != expectedLogOut {
t.Fatalf("Unexpected logger output: %q. Expected %q", cl.out, expectedLogOut)