ruby rework with contact form
All checks were successful
Build and Deploy to Production / build (push) Successful in 51s
Build and Deploy to Production / deploy (push) Successful in 34s

This commit is contained in:
Sergei Poljanski 2026-05-05 00:45:27 +04:00
commit 1ab09043ee
Signed by: asxpi
GPG key ID: 4F8851660FA4121B
26 changed files with 932 additions and 221 deletions

31
lib/rate_limit.rb Normal file
View 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