Skip to content

Commit 0995d17

Browse files
committed
small fixes
1 parent 626f980 commit 0995d17

File tree

2 files changed

+9
-20
lines changed

2 files changed

+9
-20
lines changed

conn.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,9 @@ func closeOldConnections(old []*connection, delay time.Duration) {
9797
t := time.NewTimer(delay)
9898
defer t.Stop()
9999

100+
<-t.C
100101
for _, c := range old {
101-
select {
102-
case <-c.done:
103-
case <-t.C:
104-
close(c.done)
105-
}
102+
close(c.done)
106103
}
107104
}
108105

rate.go

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,40 +45,32 @@ func (r *rateTrack) Take() {
4545

4646
// ReportResponseTime provides the response time for a request.
4747
func (r *rateTrack) ReportRTT(rtt time.Duration) {
48-
var average, count float64
48+
r.Lock()
49+
defer r.Unlock()
4950

5051
if rtt > minInterval {
5152
rtt = minInterval
5253
}
5354

54-
r.Lock()
5555
r.count++
56-
count = float64(r.count)
57-
average = float64(r.avg.Milliseconds())
56+
count := float64(r.count)
57+
average := float64(r.avg.Milliseconds())
5858
average = ((average * (count - 1)) + float64(rtt.Milliseconds())) / count
5959
r.avg = time.Duration(math.Round(average)) * time.Millisecond
6060
first := r.first
6161

62-
var update bool
6362
if first {
64-
update = true
63+
r.update()
6564
r.first = false
6665
r.updateTime = time.Now()
6766
} else if r.count >= minUpdateSampleSize && time.Since(r.updateTime) >= rateUpdateInterval {
68-
update = true
69-
r.updateTime = time.Now()
70-
}
71-
r.Unlock()
72-
73-
if update {
7467
r.update()
68+
r.updateTime = time.Now()
7569
}
7670
}
7771

72+
// update the QPS rate limiter and reset counters
7873
func (r *rateTrack) update() {
79-
r.Lock()
80-
defer r.Unlock()
81-
// update the QPS rate limiter and reset counters
8274
r.limiter.SetLimit(rate.Every(r.avg))
8375
r.avg = 0
8476
r.count = 0

0 commit comments

Comments
 (0)