Context
A few weeks ago I tried to use the rate_limit
method so that
the count could be shared between several controllers, but when
I read the source code
I noticed that it didn’t seem possible to do that.
More precisely because of this line:
cache_key = ["rate-limit", controller_path, name, instance_exec(&by)].compact.join(":")
# ^^^^^^^^^^^^^^^
Here’s an example of what I’ve described above:
# app/controllers/api_controller.rb
class ApiController < ApplicationController
rate_limit to: 10, within: 3.minutes
end
# app/controllers/api/messages_controller.rb
class Api::MessagesController < ApiController
def index; end
end
# app/controllers/api/users_controller.rb
class Api::UsersController < ApiController
def index; end
end
It seems that currently there is no way to have rate limit count shared between several controllers since, the count is separately counted for each controller.
This is the repository for the reproduction of the problem described above.
Proposal
I therefore propose to add a new option called purpose:
, to the
rate_limit
method. This option could be used, for instance, to create
a shared count for classes inherited from the same class.
Here’s an example of a potential use:
# app/controllers/api_controller.rb
class ApiController < ApplicationController
rate_limit to: 10, within: 3.minutes, purpose: "api"
end
# app/controllers/api/messages_controller.rb
class Api::MessagesController < ApiController
def index; end
end
# app/controllers/api/users_controller.rb
class Api::UsersController < ApiController
def index; end
end
Now, using the purpose:
option, we’re able to limit all
classes that inherit of ApiController
to the same rate limit count.
Thank you in advance for your time and potential feedback.
(If this proposal is accepted, I would be happy to work on that.)