mirror of https://github.com/mastodon/mastodon
Add specific rate limits for posting and following (#13172)
parent
503eab1c1f
commit
339ce1c4e9
@ -0,0 +1,64 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class RateLimiter
|
||||
include Redisable
|
||||
|
||||
FAMILIES = {
|
||||
follows: {
|
||||
limit: 400,
|
||||
period: 24.hours.freeze,
|
||||
}.freeze,
|
||||
|
||||
statuses: {
|
||||
limit: 300,
|
||||
period: 3.hours.freeze,
|
||||
}.freeze,
|
||||
|
||||
media: {
|
||||
limit: 30,
|
||||
period: 30.minutes.freeze,
|
||||
}.freeze,
|
||||
}.freeze
|
||||
|
||||
def initialize(by, options = {})
|
||||
@by = by
|
||||
@family = options[:family]
|
||||
@limit = FAMILIES[@family][:limit]
|
||||
@period = FAMILIES[@family][:period].to_i
|
||||
end
|
||||
|
||||
def record!
|
||||
count = redis.get(key)
|
||||
|
||||
if count.nil?
|
||||
redis.set(key, 0)
|
||||
redis.expire(key, (@period - (last_epoch_time % @period) + 1).to_i)
|
||||
end
|
||||
|
||||
raise Mastodon::RateLimitExceededError if count.present? && count.to_i >= @limit
|
||||
|
||||
redis.incr(key)
|
||||
end
|
||||
|
||||
def rollback!
|
||||
redis.decr(key)
|
||||
end
|
||||
|
||||
def to_headers(now = Time.now.utc)
|
||||
{
|
||||
'X-RateLimit-Limit' => @limit.to_s,
|
||||
'X-RateLimit-Remaining' => (@limit - (redis.get(key) || 0).to_i).to_s,
|
||||
'X-RateLimit-Reset' => (now + (@period - now.to_i % @period)).iso8601(6),
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def key
|
||||
@key ||= "rate_limit:#{@by.id}:#{@family}:#{(last_epoch_time / @period).to_i}"
|
||||
end
|
||||
|
||||
def last_epoch_time
|
||||
@last_epoch_time ||= Time.now.to_i
|
||||
end
|
||||
end
|
@ -0,0 +1,36 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module RateLimitable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
def rate_limit=(value)
|
||||
@rate_limit = value
|
||||
end
|
||||
|
||||
def rate_limit?
|
||||
@rate_limit
|
||||
end
|
||||
|
||||
def rate_limiter(by, options = {})
|
||||
return @rate_limiter if defined?(@rate_limiter)
|
||||
|
||||
@rate_limiter = RateLimiter.new(by, options)
|
||||
end
|
||||
|
||||
class_methods do
|
||||
def rate_limit(options = {})
|
||||
after_create do
|
||||
by = public_send(options[:by])
|
||||
|
||||
if rate_limit? && by&.local?
|
||||
rate_limiter(by, options).record!
|
||||
@rate_limit_recorded = true
|
||||
end
|
||||
end
|
||||
|
||||
after_rollback do
|
||||
rate_limiter(public_send(options[:by]), options).rollback! if @rate_limit_recorded
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,5 @@
|
||||
- content_for :page_title do
|
||||
= t('errors.429')
|
||||
|
||||
- content_for :content do
|
||||
= t('errors.429')
|
Loading…
Reference in New Issue