summaryrefslogtreecommitdiff
path: root/vendor/github.com/ulule/limiter/v3/rate.go
diff options
context:
space:
mode:
authorLibravatar nya1 <nya1git@imap.cc>2022-08-31 12:06:14 +0200
committerLibravatar GitHub <noreply@github.com>2022-08-31 12:06:14 +0200
commitbee8458a2d12bdd42079fcb2c4ca88ebeafe305b (patch)
treec114acf28a460c1ebaa85965c89f2e7fb540eecc /vendor/github.com/ulule/limiter/v3/rate.go
parent[feature] Sort follow requests, followers, and following by updated_at (#774) (diff)
downloadgotosocial-bee8458a2d12bdd42079fcb2c4ca88ebeafe305b.tar.xz
[feature] add rate limit middleware (#741)
* feat: add rate limit middleware * chore: update vendor dir * chore: update readme with new dependency * chore: add rate limit infos to swagger.md file * refactor: add ipv6 mask limiter option Add IPv6 CIDR /64 mask * refactor: increase rate limit to 1000 Address https://github.com/superseriousbusiness/gotosocial/pull/741#discussion_r945584800 Co-authored-by: tobi <31960611+tsmethurst@users.noreply.github.com>
Diffstat (limited to 'vendor/github.com/ulule/limiter/v3/rate.go')
-rw-r--r--vendor/github.com/ulule/limiter/v3/rate.go53
1 files changed, 53 insertions, 0 deletions
diff --git a/vendor/github.com/ulule/limiter/v3/rate.go b/vendor/github.com/ulule/limiter/v3/rate.go
new file mode 100644
index 000000000..46f656dd1
--- /dev/null
+++ b/vendor/github.com/ulule/limiter/v3/rate.go
@@ -0,0 +1,53 @@
+package limiter
+
+import (
+ "strconv"
+ "strings"
+ "time"
+
+ "github.com/pkg/errors"
+)
+
+// Rate is the rate.
+type Rate struct {
+ Formatted string
+ Period time.Duration
+ Limit int64
+}
+
+// NewRateFromFormatted returns the rate from the formatted version.
+func NewRateFromFormatted(formatted string) (Rate, error) {
+ rate := Rate{}
+
+ values := strings.Split(formatted, "-")
+ if len(values) != 2 {
+ return rate, errors.Errorf("incorrect format '%s'", formatted)
+ }
+
+ periods := map[string]time.Duration{
+ "S": time.Second, // Second
+ "M": time.Minute, // Minute
+ "H": time.Hour, // Hour
+ "D": time.Hour * 24, // Day
+ }
+
+ limit, period := values[0], strings.ToUpper(values[1])
+
+ p, ok := periods[period]
+ if !ok {
+ return rate, errors.Errorf("incorrect period '%s'", period)
+ }
+
+ l, err := strconv.ParseInt(limit, 10, 64)
+ if err != nil {
+ return rate, errors.Errorf("incorrect limit '%s'", limit)
+ }
+
+ rate = Rate{
+ Formatted: formatted,
+ Period: p,
+ Limit: l,
+ }
+
+ return rate, nil
+}