ruby rework with contact form
This commit is contained in:
parent
90402ae14a
commit
1ab09043ee
26 changed files with 932 additions and 221 deletions
31
lib/rate_limit.rb
Normal file
31
lib/rate_limit.rb
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
require 'thread'
|
||||
|
||||
class RateLimit
|
||||
def initialize(limit:, window:)
|
||||
@limit = limit
|
||||
@window = window
|
||||
@hits = Hash.new { |h, k| h[k] = [] }
|
||||
@mutex = Mutex.new
|
||||
end
|
||||
|
||||
def allow?(key)
|
||||
now = Time.now.to_i
|
||||
@mutex.synchronize do
|
||||
@hits[key].reject! { |t| t < now - @window }
|
||||
if @hits[key].size >= @limit
|
||||
false
|
||||
else
|
||||
@hits[key] << now
|
||||
true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def sweep!
|
||||
cutoff = Time.now.to_i - @window
|
||||
@mutex.synchronize do
|
||||
@hits.each_value { |arr| arr.reject! { |t| t < cutoff } }
|
||||
@hits.delete_if { |_, arr| arr.empty? }
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue