Organize backend into product executable and shared Go modules
This commit is contained in:
1 parent
981e84a4b4
commit
f0720ec66b
382 files changed
+1944
-6989
No files matched your search
@@ -0,0 +1,38 @@
|
||||
package ratelimit
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.sendico.io/tech/sxchnge/shared/errors"
|
||||
)
|
||||
|
||||
const (
|
||||
MinWindow = time.Second
|
||||
MaxWindow = 24 * time.Hour
|
||||
MaxLimit = 10000
|
||||
)
|
||||
|
||||
type WindowConfig struct {
|
||||
Limit int `yaml:"limit"`
|
||||
Window string `yaml:"window"`
|
||||
}
|
||||
|
||||
type Window struct {
|
||||
Limit int
|
||||
Window time.Duration
|
||||
}
|
||||
|
||||
func ResolveWindow(raw WindowConfig, field string) (Window, error) {
|
||||
if raw.Limit < 1 || raw.Limit > MaxLimit {
|
||||
return Window{}, merrors.InvalidArgument("rate limit must be between 1 and 10000", field+".limit")
|
||||
}
|
||||
window, err := time.ParseDuration(strings.TrimSpace(raw.Window))
|
||||
if err != nil {
|
||||
return Window{}, merrors.InvalidArgument("rate limit window must be a valid duration", field+".window")
|
||||
}
|
||||
if window < MinWindow || window > MaxWindow {
|
||||
return Window{}, merrors.InvalidArgument("rate limit window must be between 1s and 24h", field+".window")
|
||||
}
|
||||
return Window{Limit: raw.Limit, Window: window}, nil
|
||||
}
|
||||
Reference in new issue
Block a user