-
Notifications
You must be signed in to change notification settings - Fork 6
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
1 parent
af8c8ee
commit 2d57967
Showing
10 changed files
with
98 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
class ApplicationJob < ActiveJob::Base | ||
discard_on ActiveJob::DeserializationError # most likely a record that's not there anymore | ||
|
||
include RateLimitable | ||
end |
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,28 @@ | ||
module RateLimitable | ||
extend ActiveSupport::Concern | ||
|
||
class Limit < StandardError | ||
attr_reader :duration | ||
def initialize(duration: nil) | ||
@duration = duration | ||
super | ||
end | ||
end | ||
|
||
class_methods do | ||
def rate_limit(key = name, to:, within:) | ||
around_perform do |job, block| | ||
unless Lock.acquire(key, limit: to, duration: within) { block } | ||
raise Limit.new(duration:) | ||
end | ||
end | ||
end | ||
end | ||
|
||
included do | ||
rescue_from RateLimitable::Limit do |limit| | ||
retry_job wait: limit.duration | ||
logger.info "#{self.class.name} #{job_id} was rate limited for at least #{limit.duration.inspect}" | ||
end | ||
end | ||
end |
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,38 @@ | ||
class Lock < ApplicationRecord | ||
scope :expired, -> { where "expiration <= ?", Time.now } | ||
|
||
class << self | ||
def acquire(key, limit: 1, duration: nil, &block) | ||
lock = nil | ||
transaction do | ||
lock = find_by(key:) || create!(key:, expiration: duration&.from_now) | ||
if lock.capacity == limit | ||
return false | ||
end | ||
end | ||
|
||
lock.acquire | ||
|
||
begin | ||
yield block | ||
ensure | ||
lock.release | ||
end | ||
end | ||
end | ||
|
||
def acquire(capacity = 1) | ||
increment!(:capacity, capacity) | ||
end | ||
|
||
def release(quantity = 1) | ||
transaction do | ||
reload | ||
if capacity == 1 | ||
destroy! | ||
else | ||
decrement!(:capacity, quantity) | ||
end | ||
end | ||
end | ||
end |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
Rails.application.config.after_initialize do | ||
# AWS SES rate-limits to 14/second, so we'll set it to 10 to be safe | ||
ActionMailer::MailDeliveryJob.limits_concurrency key: "mail", to: 10, duration: 1.second | ||
ActionMailer::MailDeliveryJob.include(RateLimitable) | ||
.rate_limit to: 10, within: 1.second | ||
end |
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,11 @@ | ||
class CreateLocks < ActiveRecord::Migration[8.1] | ||
def change | ||
create_table :locks do |t| | ||
t.string :key, null: false, index: {unique: true} | ||
t.integer :capacity, null: false, default: 0 | ||
t.datetime :expiration, index: true | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.