Skip to content

Commit f9b0830

Browse files
committed
lib: remove incorrect period scaling
A dimensional analysis would have shown this to be incorrect; per is the period over which the rate value is applied and so should be in s, but this makes it in s², giving a rate in s¯².
1 parent 2ecd187 commit f9b0830

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

‎lib/limit.go‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,6 @@ func limitPolicy(h http.Header, prefix string, canonical, delta bool, window tim
489489
m["error"] = fmt.Sprintf("could not parse %q as number or timestamp", reset)
490490
return m
491491
}
492-
per *= window.Seconds()
493492

494493
m["next"] = rate.Limit(lim / window.Seconds())
495494
m["rate"] = rate.Limit(rem / per)

‎testdata/limit_general.txt‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ cmp stdout want.txt
4545
"X-RateLimit-Remaining": ["100"],
4646
"X-RateLimit-Reset": [string(int(timestamp("9999-12-31T23:59:59.999999999Z")))]
4747
}.as(h, rate_limit(h, 'X-RateLimit', false, false, duration('1s'), 100)),
48+
{
49+
"X-RateLimit-Limit": ["5000"],
50+
"X-RateLimit-Remaining": ["100"],
51+
"X-RateLimit-Reset": [string(int(timestamp("9999-12-31T23:59:59.999999999Z")))]
52+
}.as(h, rate_limit(h, 'X-RateLimit', false, false, duration('1m'), 100)),
4853
]
4954
-- want.txt --
5055
[
@@ -103,5 +108,12 @@ cmp stdout want.txt
103108
"next": 5000,
104109
"rate": 1.0842021724855044e-8,
105110
"reset": "9999-12-31T23:59:59Z"
111+
},
112+
{
113+
"burst": 100,
114+
"headers": "X-RateLimit-Limit=\"5000\" X-RateLimit-Remaining=\"100\" X-RateLimit-Reset=\"253402300799\"",
115+
"next": 83.33333333333333,
116+
"rate": 1.0842021724855044e-8,
117+
"reset": "9999-12-31T23:59:59Z"
106118
}
107119
]

0 commit comments

Comments
 (0)