History

Add rate limiter

Thibaut Courouble committed 8 years ago

Commit ad9276e1 parent 914100a1 file · +41 −0Browse files

Unified · Split

lib/docs/core/scrapers/url_scraper.rb+41−0
@@ -13,6 +13,8 @@ module Docs · 12 unmodified lines
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 @@ module Docs · 5 unmodified lines
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 @@ module Docs · 135 unmodified lines
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

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t