mirror of
https://github.com/caddyserver/caddy.git
synced 2026-05-21 06:16:31 -04:00
Rounding the latency in certain scenarios (#1005)
* Rounding the latency in certain scenarios * run gofmt
This commit is contained in:
@@ -144,7 +144,10 @@ func (r *replacer) Replace(s string) string {
|
||||
if r.responseRecorder != nil {
|
||||
r.replacements["{status}"] = func() string { return strconv.Itoa(r.responseRecorder.status) }
|
||||
r.replacements["{size}"] = func() string { return strconv.Itoa(r.responseRecorder.size) }
|
||||
r.replacements["{latency}"] = func() string { return time.Since(r.responseRecorder.start).String() }
|
||||
r.replacements["{latency}"] = func() string {
|
||||
dur := time.Since(r.responseRecorder.start)
|
||||
return roundDuration(dur).String()
|
||||
}
|
||||
}
|
||||
|
||||
// Include custom placeholders, overwriting existing ones if necessary
|
||||
@@ -187,6 +190,36 @@ func (r *replacer) Replace(s string) string {
|
||||
return s
|
||||
}
|
||||
|
||||
func roundDuration(d time.Duration) time.Duration {
|
||||
if d >= time.Millisecond {
|
||||
return round(d, time.Millisecond)
|
||||
} else if d >= time.Microsecond {
|
||||
return round(d, time.Microsecond)
|
||||
}
|
||||
|
||||
return d
|
||||
}
|
||||
|
||||
// round rounds d to the nearest r
|
||||
func round(d, r time.Duration) time.Duration {
|
||||
if r <= 0 {
|
||||
return d
|
||||
}
|
||||
neg := d < 0
|
||||
if neg {
|
||||
d = -d
|
||||
}
|
||||
if m := d % r; m+m < r {
|
||||
d = d - m
|
||||
} else {
|
||||
d = d + r - m
|
||||
}
|
||||
if neg {
|
||||
return -d
|
||||
}
|
||||
return d
|
||||
}
|
||||
|
||||
// Set sets key to value in the r.customReplacements map.
|
||||
func (r *replacer) Set(key, value string) {
|
||||
r.customReplacements["{"+key+"}"] = func() string { return value }
|
||||
|
||||
Reference in New Issue
Block a user