Skip to content

Commit ad9276e

Browse files
committed
Add rate limiter
1 parent 914100a commit ad9276e

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

lib/docs/core/scrapers/url_scraper.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ def inherited(subclass)
1313
end
1414
end
1515

16+
@@rate_limiter = nil
17+
1618
self.params = {}
1719
self.headers = { 'User-Agent' => 'DevDocs' }
1820
self.force_gzip = false
@@ -24,6 +26,15 @@ def request_one(url)
2426
end
2527

2628
def request_all(urls, &block)
29+
if options[:rate_limit]
30+
if @@rate_limiter
31+
@@rate_limiter.limit = options[:rate_limit]
32+
else
33+
@@rate_limiter = RateLimiter.new(options[:rate_limit])
34+
Typhoeus.before(&@@rate_limiter.to_proc)
35+
end
36+
end
37+
2738
Requester.run urls, request_options: request_options, &block
2839
end
2940

@@ -165,5 +176,35 @@ def additional_options
165176
super.merge! redirections: self.class.redirections
166177
end
167178
end
179+
180+
class RateLimiter
181+
attr_accessor :limit
182+
183+
def initialize(limit)
184+
@limit = limit
185+
@minute = nil
186+
@counter = 0
187+
end
188+
189+
def call(*)
190+
if @minute != Time.now.min
191+
@minute = Time.now.min
192+
@counter = 0
193+
end
194+
195+
@counter += 1
196+
197+
if @counter >= @limit
198+
wait = Time.now.end_of_minute.to_i - Time.now.to_i + 1
199+
sleep wait
200+
end
201+
202+
true
203+
end
204+
205+
def to_proc
206+
method(:call).to_proc
207+
end
208+
end
168209
end
169210
end

0 commit comments

Comments
 (0)