Class: Typhoeus::Hydra

Inherits:
Object
  • Object
show all
Defined in:
lib/typhoeus/hydra.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Hydra

Returns a new instance of Hydra.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/typhoeus/hydra.rb', line 3

def initialize(options = {})
  @memoize_requests = true
  @multi       = Multi.new
  @easy_pool   = []
  initial_pool_size = options[:initial_pool_size] || 10
  @max_concurrency = options[:max_concurrency] || 200
  initial_pool_size.times { @easy_pool << Easy.new }
  @stubs       = []
  @memoized_requests = {}
  @retrieved_from_cache = {}
  @queued_requests = []
  @running_requests = 0
  @stubbed_request_count = 0
end

Class Method Details

.hydraObject



18
19
20
# File 'lib/typhoeus/hydra.rb', line 18

def self.hydra
  @hydra ||= new
end

.hydra=(val) ⇒ Object



22
23
24
# File 'lib/typhoeus/hydra.rb', line 22

def self.hydra=(val)
  @hydra = val
end

Instance Method Details

#cache_getter(&block) ⇒ Object



84
85
86
# File 'lib/typhoeus/hydra.rb', line 84

def cache_getter(&block)
  @cache_getter = block
end

#cache_setter(&block) ⇒ Object



88
89
90
# File 'lib/typhoeus/hydra.rb', line 88

def cache_setter(&block)
  @cache_setter = block
end

#clear_cache_callbacksObject



26
27
28
29
# File 'lib/typhoeus/hydra.rb', line 26

def clear_cache_callbacks
  @cache_setter = nil
  @cache_getter = nil
end

#clear_stubsObject



31
32
33
# File 'lib/typhoeus/hydra.rb', line 31

def clear_stubs
  @stubs = []
end

#disable_memoizationObject



80
81
82
# File 'lib/typhoeus/hydra.rb', line 80

def disable_memoization
  @memoize_requests = false
end

#fire_and_forgetObject



35
36
37
38
# File 'lib/typhoeus/hydra.rb', line 35

def fire_and_forget
  @queued_requests.each {|r| queue(r, false)}
  @multi.fire_and_forget
end

#on_complete(&block) ⇒ Object



92
93
94
# File 'lib/typhoeus/hydra.rb', line 92

def on_complete(&block)
  @on_complete = block
end

#on_complete=(proc) ⇒ Object



96
97
98
# File 'lib/typhoeus/hydra.rb', line 96

def on_complete=(proc)
  @on_complete = proc
end

#queue(request, obey_concurrency_limit = true) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/typhoeus/hydra.rb', line 40

def queue(request, obey_concurrency_limit = true)
  return if assign_to_stub(request)

  if @running_requests >= @max_concurrency && obey_concurrency_limit
    @queued_requests << request
  else
    if request.method == :get
      if @memoize_requests && @memoized_requests.has_key?(request.url)
        if response = @retrieved_from_cache[request.url]
          request.response = response
          request.call_handlers
        else
          @memoized_requests[request.url] << request
        end
      else
        @memoized_requests[request.url] = [] if @memoize_requests
        get_from_cache_or_queue(request)
      end
    else
      get_from_cache_or_queue(request)
    end
  end
end

#runObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/typhoeus/hydra.rb', line 64

def run
  while @stubbed_request_count > 0
    @stubs.each do |m|
      while request = m.requests.shift
        @stubbed_request_count -= 1
        m.response.request = request
        handle_request(request, m.response)
      end
    end
  end

  @multi.perform
  @memoized_requests = {}
  @retrieved_from_cache = {}
end

#stub(method, url) ⇒ Object



100
101
102
103
# File 'lib/typhoeus/hydra.rb', line 100

def stub(method, url)
  @stubs << HydraMock.new(url, method)
  @stubs.last
end