typhoeus 0.1.9 → 0.1.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/typhoeus/hydra.rb +34 -16
  2. data/lib/typhoeus.rb +7 -7
  3. metadata +1 -1
@@ -1,48 +1,57 @@
1
1
  module Typhoeus
2
2
  class Hydra
3
- def initialize(initial_pool_size = 10)
3
+ def initialize(options = {})
4
4
  @memoize_requests = true
5
5
  @multi = Multi.new
6
6
  @easy_pool = []
7
+ initial_pool_size = options[:initial_pool_size] || 10
8
+ @max_concurrency = options[:max_concurrency] || 200
7
9
  initial_pool_size.times { @easy_pool << Easy.new }
8
10
  @stubs = []
9
11
  @memoized_requests = {}
10
12
  @retrieved_from_cache = {}
13
+ @queued_requests = []
14
+ @running_requests = 0
11
15
  end
12
16
 
13
17
  def self.hydra
14
18
  @hydra ||= new
15
19
  end
16
-
20
+
17
21
  def self.hydra=(val)
18
22
  @hydra = val
19
23
  end
20
-
24
+
21
25
  def clear_stubs
22
26
  @stubs = []
23
27
  end
24
-
28
+
25
29
  def fire_and_forget
30
+ @queued_requests.each {|r| queue(r, false)}
26
31
  @multi.fire_and_forget
27
32
  end
28
33
 
29
- def queue(request)
34
+ def queue(request, obey_concurrency_limit = true)
30
35
  return if assign_to_stub(request)
31
36
 
32
- if request.method == :get
33
- if @memoize_requests && @memoized_requests.has_key?(request.url)
34
- if response = @retrieved_from_cache[request.url]
35
- request.response = response
36
- request.call_handlers
37
+ if @running_requests >= @max_concurrency && obey_concurrency_limit
38
+ @queued_requests << request
39
+ else
40
+ if request.method == :get
41
+ if @memoize_requests && @memoized_requests.has_key?(request.url)
42
+ if response = @retrieved_from_cache[request.url]
43
+ request.response = response
44
+ request.call_handlers
45
+ else
46
+ @memoized_requests[request.url] << request
47
+ end
37
48
  else
38
- @memoized_requests[request.url] << request
49
+ @memoized_requests[request.url] = [] if @memoize_requests
50
+ get_from_cache_or_queue(request)
39
51
  end
40
52
  else
41
- @memoized_requests[request.url] = [] if @memoize_requests
42
53
  get_from_cache_or_queue(request)
43
54
  end
44
- else
45
- get_from_cache_or_queue(request)
46
55
  end
47
56
  end
48
57
 
@@ -57,7 +66,7 @@ module Typhoeus
57
66
  @memoized_requests = {}
58
67
  @retrieved_from_cache = {}
59
68
  end
60
-
69
+
61
70
  def disable_memoization
62
71
  @memoize_requests = false
63
72
  end
@@ -105,6 +114,7 @@ module Typhoeus
105
114
  private :get_from_cache_or_queue
106
115
 
107
116
  def get_easy_object(request)
117
+ @running_requests += 1
108
118
  easy = @easy_pool.pop || Easy.new
109
119
  easy.url = request.url
110
120
  easy.method = request.method
@@ -113,10 +123,12 @@ module Typhoeus
113
123
  easy.request_body = request.body if request.body
114
124
  easy.timeout = request.timeout if request.timeout
115
125
  easy.on_success do |easy|
126
+ queue_next
116
127
  handle_request(request, response_from_easy(easy, request))
117
128
  release_easy_object(easy)
118
129
  end
119
130
  easy.on_failure do |easy|
131
+ queue_next
120
132
  handle_request(request, response_from_easy(easy, request))
121
133
  release_easy_object(easy)
122
134
  end
@@ -125,6 +137,12 @@ module Typhoeus
125
137
  end
126
138
  private :get_easy_object
127
139
 
140
+ def queue_next
141
+ @running_requests -= 1
142
+ queue(@queued_requests.pop) unless @queued_requests.empty?
143
+ end
144
+ private :queue_next
145
+
128
146
  def release_easy_object(easy)
129
147
  easy.reset
130
148
  @easy_pool.push easy
@@ -175,7 +193,7 @@ module Typhoeus
175
193
  def and_return(val)
176
194
  @response = val
177
195
  end
178
-
196
+
179
197
  def matches?(request)
180
198
  if url.kind_of?(String)
181
199
  request.method == method && request.url == url
data/lib/typhoeus.rb CHANGED
@@ -14,23 +14,23 @@ require 'typhoeus/request'
14
14
  require 'typhoeus/hydra'
15
15
 
16
16
  module Typhoeus
17
- VERSION = "0.1.9"
18
-
17
+ VERSION = "0.1.10"
18
+
19
19
  def self.easy_object_pool
20
20
  @easy_objects ||= []
21
21
  end
22
-
22
+
23
23
  def self.init_easy_object_pool
24
24
  20.times do
25
25
  easy_object_pool << Typhoeus::Easy.new
26
26
  end
27
27
  end
28
-
28
+
29
29
  def self.release_easy_object(easy)
30
30
  easy.reset
31
31
  easy_object_pool << easy
32
32
  end
33
-
33
+
34
34
  def self.get_easy_object
35
35
  if easy_object_pool.empty?
36
36
  Typhoeus::Easy.new
@@ -38,12 +38,12 @@ module Typhoeus
38
38
  easy_object_pool.pop
39
39
  end
40
40
  end
41
-
41
+
42
42
  def self.add_easy_request(easy_object)
43
43
  Thread.current[:curl_multi] ||= Typhoeus::Multi.new
44
44
  Thread.current[:curl_multi].add(easy_object)
45
45
  end
46
-
46
+
47
47
  def self.perform_easy_requests
48
48
  multi = Thread.current[:curl_multi]
49
49
  start_time = Time.now
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: typhoeus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9
4
+ version: 0.1.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Dix