-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtracker.go
83 lines (72 loc) · 1.77 KB
/
tracker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"fmt"
"net/http"
"sort"
"sync"
"sync/atomic"
"text/tabwriter"
"time"
)
func newHTTPTracker() *httpTracker {
return &httpTracker{reqs: make(map[*httpRequest]struct{})}
}
type httpTracker struct {
mu sync.RWMutex
reqs map[*httpRequest]struct{}
}
func (t *httpTracker) ServeHTTP(w http.ResponseWriter, r *http.Request) {
t.mu.RLock()
reqs := make([]*httpRequest, 0, len(t.reqs))
for req := range t.reqs {
reqs = append(reqs, req)
}
t.mu.RUnlock()
sort.Slice(reqs, func(i, j int) bool {
return reqs[i].start.Before(reqs[j].start)
})
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
tw := tabwriter.NewWriter(w, 0, 2, 1, ' ', 0)
now := time.Now()
for _, req := range reqs {
fmt.Fprintf(tw, "%v\t%d bytes\t%v\t%q\n", now.Sub(req.start), atomic.LoadUint64(&req.bytesWritten), req.http.RemoteAddr, req.http.Header.Get("User-agent"))
}
tw.Flush()
}
func (t *httpTracker) Wrap(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
req := &httpRequest{
http: r,
start: time.Now(),
}
t.mu.Lock()
t.reqs[req] = struct{}{}
t.mu.Unlock()
defer func() {
t.mu.Lock()
delete(t.reqs, req)
t.mu.Unlock()
}()
w = &byteCountingResponseWriter{w, &req.bytesWritten}
h.ServeHTTP(w, r)
})
}
type httpRequest struct {
http *http.Request
start time.Time
bytesWritten uint64 // Accessed atomically.
}
type byteCountingResponseWriter struct {
http.ResponseWriter
bytesWritten *uint64
}
func (w *byteCountingResponseWriter) Write(b []byte) (int, error) {
n, err := w.ResponseWriter.Write(b)
atomic.AddUint64(w.bytesWritten, uint64(n))
return n, err
}
func (w *byteCountingResponseWriter) Flush() {
if f, ok := w.ResponseWriter.(http.Flusher); ok {
f.Flush()
}
}