mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2024-05-20 09:18:13 +00:00
bee8458a2d
* 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>
29 lines
530 B
Go
29 lines
530 B
Go
package common
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/ulule/limiter/v3"
|
|
)
|
|
|
|
// GetContextFromState generate a new limiter.Context from given state.
|
|
func GetContextFromState(now time.Time, rate limiter.Rate, expiration time.Time, count int64) limiter.Context {
|
|
limit := rate.Limit
|
|
remaining := int64(0)
|
|
reached := true
|
|
|
|
if count <= limit {
|
|
remaining = limit - count
|
|
reached = false
|
|
}
|
|
|
|
reset := expiration.Unix()
|
|
|
|
return limiter.Context{
|
|
Limit: limit,
|
|
Remaining: remaining,
|
|
Reset: reset,
|
|
Reached: reached,
|
|
}
|
|
}
|