class Droonga::Client::RateLimiter
Constants
- DEFAULT_LIMIT
- NO_LIMIT
Public Class Methods
new(client, messages_per_second)
click to toggle source
# File lib/droonga/client/rate-limiter.rb, line 22 def initialize(client, messages_per_second) @client = client @messages_per_second = messages_per_second @diff_in_seconds_per_message = 1.0 / @messages_per_second end
Public Instance Methods
method_missing(name, *args, &block)
click to toggle source
Calls superclass method
# File lib/droonga/client/rate-limiter.rb, line 34 def method_missing(name, *args, &block) if @client.respond_to?(name) @client.__send__(name, *args, &block) else super end end
send(*args, &block)
click to toggle source
# File lib/droonga/client/rate-limiter.rb, line 28 def send(*args, &block) limit do @client.send(*args, &block) end end
Private Instance Methods
limit() { || ... }
click to toggle source
# File lib/droonga/client/rate-limiter.rb, line 43 def limit return yield if @messages_per_second == NO_LIMIT @previous_time ||= Time.now diff_in_seconds = Time.now - @previous_time sleep_time = @diff_in_seconds_per_message - diff_in_seconds if sleep_time > 0 sleep(sleep_time) end @previous_time = Time.now yield end