Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
caddy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
caddy
Commits
4d76ccb1
Commit
4d76ccb1
authored
Aug 08, 2016
by
Carter
Committed by
Matt Holt
Aug 08, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Rounding the latency in certain scenarios (#1005)
* Rounding the latency in certain scenarios * run gofmt
parent
de7bf4f2
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
55 additions
and
1 deletion
+55
-1
caddyhttp/httpserver/replacer.go
caddyhttp/httpserver/replacer.go
+34
-1
caddyhttp/httpserver/replacer_test.go
caddyhttp/httpserver/replacer_test.go
+21
-0
No files found.
caddyhttp/httpserver/replacer.go
View file @
4d76ccb1
...
...
@@ -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
}
...
...
caddyhttp/httpserver/replacer_test.go
View file @
4d76ccb1
...
...
@@ -6,6 +6,7 @@ import (
"os"
"strings"
"testing"
"time"
)
func
TestNewReplacer
(
t
*
testing
.
T
)
{
...
...
@@ -140,3 +141,23 @@ func TestSet(t *testing.T) {
t
.
Error
(
"Expected variable replacement failed"
)
}
}
func
TestRound
(
t
*
testing
.
T
)
{
var
tests
=
map
[
time
.
Duration
]
time
.
Duration
{
// 599.935µs -> 560µs
559935
*
time
.
Nanosecond
:
560
*
time
.
Microsecond
,
// 1.55ms -> 2ms
1550
*
time
.
Microsecond
:
2
*
time
.
Millisecond
,
// 1.5555s -> 1.556s
1555500
*
time
.
Microsecond
:
1556
*
time
.
Millisecond
,
// 1m2.0035s -> 1m2.004s
62003500
*
time
.
Microsecond
:
62004
*
time
.
Millisecond
,
}
for
dur
,
expected
:=
range
tests
{
rounded
:=
roundDuration
(
dur
)
if
rounded
!=
expected
{
t
.
Errorf
(
"Expected %v, Got %v"
,
expected
,
rounded
)
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment