mirror of
https://github.com/freeCodeCamp/devdocs
synced 2024-11-16 19:48:10 +01:00
Add rate limiter
This commit is contained in:
parent
914100a657
commit
ad9276e901
1 changed files with 41 additions and 0 deletions
|
@ -13,6 +13,8 @@ module Docs
|
|||
end
|
||||
end
|
||||
|
||||
@@rate_limiter = nil
|
||||
|
||||
self.params = {}
|
||||
self.headers = { 'User-Agent' => 'DevDocs' }
|
||||
self.force_gzip = false
|
||||
|
@ -24,6 +26,15 @@ module Docs
|
|||
end
|
||||
|
||||
def request_all(urls, &block)
|
||||
if options[:rate_limit]
|
||||
if @@rate_limiter
|
||||
@@rate_limiter.limit = options[:rate_limit]
|
||||
else
|
||||
@@rate_limiter = RateLimiter.new(options[:rate_limit])
|
||||
Typhoeus.before(&@@rate_limiter.to_proc)
|
||||
end
|
||||
end
|
||||
|
||||
Requester.run urls, request_options: request_options, &block
|
||||
end
|
||||
|
||||
|
@ -165,5 +176,35 @@ module Docs
|
|||
super.merge! redirections: self.class.redirections
|
||||
end
|
||||
end
|
||||
|
||||
class RateLimiter
|
||||
attr_accessor :limit
|
||||
|
||||
def initialize(limit)
|
||||
@limit = limit
|
||||
@minute = nil
|
||||
@counter = 0
|
||||
end
|
||||
|
||||
def call(*)
|
||||
if @minute != Time.now.min
|
||||
@minute = Time.now.min
|
||||
@counter = 0
|
||||
end
|
||||
|
||||
@counter += 1
|
||||
|
||||
if @counter >= @limit
|
||||
wait = Time.now.end_of_minute.to_i - Time.now.to_i + 1
|
||||
sleep wait
|
||||
end
|
||||
|
||||
true
|
||||
end
|
||||
|
||||
def to_proc
|
||||
method(:call).to_proc
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue