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

Leaky Bucket Queue? #101

Open
robacarp opened this issue Nov 1, 2022 · 0 comments
Open

Leaky Bucket Queue? #101

robacarp opened this issue Nov 1, 2022 · 0 comments

Comments

@robacarp
Copy link
Collaborator

robacarp commented Nov 1, 2022

Re: #100

A leaky bucket rate limited is best implemented at the queue layer, but mosquito doesn't yet have the notion of configurable queue classes. If it did, this might work:

require "./queue"

module Mosquito::LeakyBucket
  class Bucket < Mosquito::Queue
    def metadata : Metadata
      Metadata.new @config_key
    end

    def can_enqueue? : Bool
      # todo max size needs configurable
      size < 15
    end

    def will_drip? : Bool
      # TODO drip interval needs configurable
      last_drip = metadata["last_drip"]?
      return true if last_drip.nil?
      last_drip = Time.unix_ms last_drip.to_i
      Time.utc - last_drip > 10.seconds
    end

    def drip!
      metadata["last_drip"] = Time.utc.to_unix_ms.to_s
    end

    def dequeue : Task?
      unless will_drip?
        puts "not dripping"
        return
      end
      Log.info { "time to drip!" }
      drip!
      super
    end
  end

  module Limiter
    module ClassMethods
      def queue
        Mosquito::LeakyBucket::Bucket.new queue_name
      end
    end

    macro included
      extend ClassMethods
    end

    def rescheduleable? : Bool
      false
    end

    # def enqueue : Task
    #   if self.class.queue.can_enqueue?
    #     previous_def
    #   else
    #     raise "No room left in bucket"
    #   end
    # end
  end
end
@robacarp robacarp mentioned this issue Nov 1, 2022
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant