Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

are you interested in adding a ratelimit behavior? #15

Open
dreyks opened this issue Jan 9, 2025 · 0 comments
Open

are you interested in adding a ratelimit behavior? #15

dreyks opened this issue Jan 9, 2025 · 0 comments

Comments

@dreyks
Copy link
Collaborator

dreyks commented Jan 9, 2025

I plan to implement a behavior that prevents getting into the rate limit. I think it can be added to the gem as an opt-in

something like this

  • Monobank::Methods::Base will wrap its calls into MonoBank.rate_limiter.call passing the token and the endpoint
  • the developer supplies the rate limiter which can track the token/endpoint combination the way it wants (redis/memory/etc)

example

class MyRateLimiter
  def initialize(redis:)
    @redis = redis
  end

  def call(token:, endpoint:)
    if @redis.exists?("#{token}:#{endpoint}")
      raise StandardError.new # or whatever else the developer wants
    end

    begin    
      yield # back to the calling method
    ensure
      @redis.set("#{token}:#{endpoint}", '', ex: 60)
    end
  end
end

Monobank.configure(rate_limiter: MyRateLimiter.new(redis: $redis)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant