-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
216 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type RateLimitConfig struct { | ||
Interval Duration `toml:"interval"` | ||
Limit int `toml:"limit"` | ||
PerIP bool `toml:"per_ip,omitempty"` | ||
} | ||
|
||
type Duration time.Duration | ||
|
||
func (d *Duration) UnmarshalText(text []byte) error { | ||
if n, err := strconv.ParseFloat(string(text), 64); err == nil { | ||
*d = Duration(time.Duration(n*1000) * time.Millisecond) | ||
return nil | ||
} | ||
|
||
if n, err := time.ParseDuration(string(text)); err == nil { | ||
*d = Duration(n) | ||
return nil | ||
} | ||
return fmt.Errorf("cannot unmarshal %q into a Duration", text) | ||
} | ||
|
||
func (d Duration) Duration() time.Duration { | ||
return time.Duration(d) | ||
} | ||
|
||
func ParseRateLimit(s string) (*RateLimitConfig, error) { | ||
parts := strings.Split(s, ":") | ||
if len(parts) != 2 && len(parts) != 3 { | ||
return nil, fmt.Errorf("invalid rate limit string: '%s', expected format: `interval:limit[:tag]`", s) | ||
} | ||
|
||
perip := false | ||
if len(parts) == 3 { | ||
switch strings.ToLower(parts[2]) { | ||
case "p", "perip": | ||
perip = true | ||
default: | ||
} | ||
} | ||
|
||
var interval Duration | ||
if err := interval.UnmarshalText([]byte(parts[0])); err != nil { | ||
return nil, err | ||
} | ||
limit, err := strconv.Atoi(parts[1]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &RateLimitConfig{interval, limit, perip}, nil | ||
} | ||
|
||
func ParseCmdlineRateLimits(ss []string) ([]*RateLimitConfig, error) { | ||
var ret []*RateLimitConfig | ||
for _, s := range ss { | ||
// format "interval:limit" | ||
// or "interval:limit:perip"/"interval:limit:p" or "interval:limit:global"/"interval:limit:g", default global | ||
// or "interval:limit;interval:limit" for multiple rate limits in one string | ||
rs := strings.Split(s, ";") | ||
for _, r := range rs { | ||
r = strings.TrimSpace(r) | ||
if r == "" { | ||
continue | ||
} | ||
|
||
r, err := ParseRateLimit(r) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ret = append(ret, r) | ||
} | ||
} | ||
return ret, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package utils | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestParseRateLimit(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
expected *RateLimitConfig | ||
err bool | ||
}{ | ||
{ | ||
input: "10:10", | ||
expected: &RateLimitConfig{ | ||
Interval: Duration(time.Second * 10), | ||
Limit: 10, | ||
PerIP: false, | ||
}, | ||
}, | ||
{ | ||
input: "1m:10", | ||
expected: &RateLimitConfig{ | ||
Interval: Duration(time.Second * 60), | ||
Limit: 10, | ||
PerIP: false, | ||
}, | ||
}, | ||
{ | ||
input: "10s:10", | ||
expected: &RateLimitConfig{ | ||
Interval: Duration(time.Second * 10), | ||
Limit: 10, | ||
PerIP: false, | ||
}, | ||
}, | ||
{ | ||
input: "10s:10:p", | ||
expected: &RateLimitConfig{ | ||
Interval: Duration(time.Second * 10), | ||
Limit: 10, | ||
PerIP: true, | ||
}, | ||
}, | ||
{ | ||
input: "10s:10:perip", | ||
expected: &RateLimitConfig{ | ||
Interval: Duration(time.Second * 10), | ||
Limit: 10, | ||
PerIP: true, | ||
}, | ||
}, | ||
{ | ||
input: "invalid:10", | ||
expected: nil, | ||
err: true, | ||
}, | ||
{ | ||
input: "10s:invalid", | ||
expected: nil, | ||
err: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
actual, err := ParseRateLimit(tt.input) | ||
assert.Equal(t, tt.expected, actual) | ||
assert.Equal(t, tt.err, err != nil, "expect error: %v, got: %v", tt.err, err) | ||
} | ||
} | ||
|
||
func TestParseCmdlineRateLimits(t *testing.T) { | ||
tests := []struct { | ||
input []string | ||
expected []*RateLimitConfig | ||
err bool | ||
}{ | ||
{ | ||
input: []string{"10s:10"}, | ||
expected: []*RateLimitConfig{{Interval: Duration(10 * time.Second), Limit: 10}}, | ||
}, | ||
{ | ||
input: []string{"10s:10:perip", "10s:20:global"}, | ||
expected: []*RateLimitConfig{ | ||
{Interval: Duration(10 * time.Second), Limit: 10, PerIP: true}, | ||
{Interval: Duration(10 * time.Second), Limit: 20}, | ||
}, | ||
}, | ||
{ | ||
input: []string{"10s:10;10s:20"}, | ||
expected: []*RateLimitConfig{ | ||
{Interval: Duration(10 * time.Second), Limit: 10}, | ||
{Interval: Duration(10 * time.Second), Limit: 20}, | ||
}, | ||
}, | ||
{ | ||
input: []string{"invalid"}, | ||
err: true, | ||
}, | ||
{ | ||
input: []string{"10s:invalid"}, | ||
err: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run("", func(t *testing.T) { | ||
got, err := ParseCmdlineRateLimits(tt.input) | ||
assert.Equal(t, tt.expected, got) | ||
assert.Equal(t, tt.err, err != nil, "expect error: %v, got: %v", tt.err, err) | ||
}) | ||
} | ||
} |