/
logger.go
134 lines (118 loc) · 2.81 KB
/
logger.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright 2014, Hǎiliàng Wáng. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package getgo
import (
"fmt"
"net"
"net/http"
"sync"
"time"
)
const avgCounterCap = 50
// HTTPLogger wraps an HTTP client and logs the request and network speed.
type HTTPLogger struct {
client Doer
startTime time.Time
mu sync.Mutex
totalReqCount int
totalByteCount int
avgByteCounter *avgCounter
}
// NewHTTPLogger creates an HTTPLogger by inspecting the connection's Read
// method of an http.Client.
func NewHTTPLogger(client *http.Client) *HTTPLogger {
httpLogger := &HTTPLogger{
client: client,
startTime: time.Now(),
avgByteCounter: newAvgCounter()}
if client.Transport == nil {
client.Transport = &http.Transport{}
}
if transport, ok := client.Transport.(*http.Transport); ok {
transport.Dial = httpLogger.wrappedDial
}
return httpLogger
}
func (l *HTTPLogger) wrappedDial(network, address string) (net.Conn, error) {
conn, err := net.Dial(network, address)
if err == nil {
return &readFilter{conn, l.gotBytes}, err
}
return conn, err
}
func (l *HTTPLogger) gotBytes(b []byte) {
l.mu.Lock()
defer l.mu.Unlock()
l.totalByteCount += len(b)
l.avgByteCounter.Add(len(b), time.Now())
}
// Do implements the Doer interface.
func (l *HTTPLogger) Do(req *http.Request) (resp *http.Response, err error) {
resp, err = l.client.Do(req)
l.measure(resp, err)
l.log(req)
return resp, err
}
func (l *HTTPLogger) measure(resp *http.Response, err error) {
l.mu.Lock()
defer l.mu.Unlock()
if err == nil {
l.totalReqCount++
}
}
func (l *HTTPLogger) log(req *http.Request) {
sec := time.Now().Sub(l.startTime).Seconds()
reqSpeed := int(float64(l.totalReqCount) / sec)
kbSpeed := int(float64(l.totalByteCount) / sec / 1000)
fmt.Printf("[%dKB, %d, %dKB] %s\n",
int(l.avgByteCounter.PerSecond()/1000), reqSpeed, kbSpeed, req.URL)
}
type readFilter struct {
net.Conn
read func(b []byte)
}
func (l *readFilter) Read(b []byte) (n int, err error) {
n, err = l.Conn.Read(b)
if err == nil {
l.read(b[:n])
}
return n, err
}
type nt struct {
n int
t time.Time
}
type avgCounter struct {
ring []nt
tail int
total int
mu sync.Mutex
}
func newAvgCounter() *avgCounter {
ring := make([]nt, avgCounterCap)
ring[len(ring)-1].t = time.Now()
return &avgCounter{ring: ring}
}
func (c *avgCounter) Add(n int, t time.Time) {
c.mu.Lock()
defer c.mu.Unlock()
c.total -= c.ring[c.tail].n
c.total += n
c.ring[c.tail] = nt{n, t}
c.tail++
if c.tail >= len(c.ring) {
c.tail = 0
}
}
func (c *avgCounter) PerSecond() float64 {
head, tail := c.tail-1, c.tail
if head == -1 {
head = len(c.ring) - 1
}
sec := c.ring[head].t.Sub(c.ring[tail].t).Seconds()
if sec == 0 {
return 0
}
return float64(c.total) / sec
}