Source File
stats.go
Belonging Package
golang.org/x/pkgsite/internal/middleware
package middleware
import (
)
type statsKey struct{}
func () Middleware {
return func( http.Handler) http.Handler {
return http.HandlerFunc(func( http.ResponseWriter, *http.Request) {
:= newStatsResponseWriter()
:= context.WithValue(.Context(), statsKey{}, .stats.Other)
.ServeHTTP(, .WithContext())
.WriteStats(, )
})
}
}
type statsResponseWriter struct {
header http.Header // required for a ResponseWriter; ignored
start time.Time // start time of request
hasher hash.Hash64
stats PageStats
}
type PageStats struct {
MillisToFirstByte int64
MillisToLastByte int64
Hash uint64 // hash of page contents
Size int // total size of data written
StatusCode int // HTTP status
Other map[string]interface{}
}
func () *statsResponseWriter {
return &statsResponseWriter{
header: http.Header{},
start: time.Now(),
hasher: fnv.New64a(),
stats: PageStats{Other: map[string]interface{}{}},
}
}
func ( *statsResponseWriter) () http.Header { return .header }
func ( *statsResponseWriter) ( int) {
.stats.StatusCode =
}
func ( *statsResponseWriter) ( []byte) (int, error) {
if .stats.Size == 0 {
.stats.MillisToFirstByte = time.Since(.start).Milliseconds()
}
if .stats.StatusCode == 0 {
.WriteHeader(http.StatusOK)
}
.stats.Size += len()
.hasher.Write()
return len(), nil
}
func ( *statsResponseWriter) ( context.Context, http.ResponseWriter) {
.stats.MillisToLastByte = time.Since(.start).Milliseconds()
.stats.Hash = .hasher.Sum64()
, := json.Marshal(.stats)
if != nil {
http.Error(, .Error(), http.StatusInternalServerError)
} else {
_, _ = .Write()
}
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |