Class: Rubord::RateLimiter::GlobalLimiter

Inherits:
Object
  • Object
show all
Defined in:
lib/rubord/structs/rate_limiter.rb

Instance Method Summary collapse

Constructor Details

#initializeGlobalLimiter

Returns a new instance of GlobalLimiter.



72
73
74
75
# File 'lib/rubord/structs/rate_limiter.rb', line 72

def initialize
  @reset_at = Time.now
  @mutex = Mutex.new
end

Instance Method Details

#block_for(seconds) ⇒ Object



81
82
83
84
85
# File 'lib/rubord/structs/rate_limiter.rb', line 81

def block_for(seconds)
  @mutex.synchronize do
    @reset_at = Time.now + seconds
  end
end

#blocked?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/rubord/structs/rate_limiter.rb', line 77

def blocked?
  @mutex.synchronize { Time.now < @reset_at }
end

#waitObject



87
88
89
90
91
92
93
94
# File 'lib/rubord/structs/rate_limiter.rb', line 87

def wait
  @mutex.synchronize do
    if Time.now < @reset_at
      sleep_time = @reset_at - Time.now
      sleep(sleep_time) if sleep_time > 0
    end
  end
end