Class: Typhoeus::Hydra

Inherits:
Object
  • Object
show all
Extended by:
Callbacks
Includes:
ConnectOptions, Stubbing
Defined in:
lib/typhoeus/hydra.rb,
lib/typhoeus/hydra/stubbing.rb,
lib/typhoeus/hydra/callbacks.rb,
lib/typhoeus/hydra/connect_options.rb

Defined Under Namespace

Modules: Callbacks, ConnectOptions, Stubbing Classes: NetConnectNotAllowedError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Callbacks

after_request_before_on_complete, clear_global_hooks, extended, run_global_hooks_for

Methods included from Stubbing

included

Methods included from Stubbing::SharedMethods

#clear_stubs, extended, #find_stub_from_request, #stub

Methods included from ConnectOptions

included

Constructor Details

#initialize(options = {}) ⇒ Hydra

Returns a new instance of Hydra.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/typhoeus/hydra.rb', line 13

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 }
  @memoized_requests = {}
  @retrieved_from_cache = {}
  @queued_requests = []
  @running_requests = 0

  self.stubs = []
  @active_stubs = []
end

Instance Attribute Details

#max_concurrencyObject

Returns the value of attribute max_concurrency.



11
12
13
# File 'lib/typhoeus/hydra.rb', line 11

def max_concurrency
  @max_concurrency
end

Class Method Details

.hydraObject



29
30
31
# File 'lib/typhoeus/hydra.rb', line 29

def self.hydra
  @hydra ||= new
end

.hydra=(val) ⇒ Object



33
34
35
# File 'lib/typhoeus/hydra.rb', line 33

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

Instance Method Details

#abortObject

Abort the run on a best-effort basis.

It won’t abort the current burst of @max_concurrency requests, however it won’t fire the rest of the queued requests so the run will be aborted as soon as possible…



44
45
46
# File 'lib/typhoeus/hydra.rb', line 44

def abort
  @queued_requests.clear
end

#cache_getter(&block) ⇒ Object



109
110
111
# File 'lib/typhoeus/hydra.rb', line 109

def cache_getter(&block)
  @cache_getter = block
end

#cache_setter(&block) ⇒ Object



113
114
115
# File 'lib/typhoeus/hydra.rb', line 113

def cache_setter(&block)
  @cache_setter = block
end

#clear_cache_callbacksObject



48
49
50
51
# File 'lib/typhoeus/hydra.rb', line 48

def clear_cache_callbacks
  @cache_setter = nil
  @cache_getter = nil
end

#disable_memoizationObject



105
106
107
# File 'lib/typhoeus/hydra.rb', line 105

def disable_memoization
  @memoize_requests = false
end

#fire_and_forgetObject



53
54
55
56
# File 'lib/typhoeus/hydra.rb', line 53

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

#on_complete(&block) ⇒ Object



117
118
119
# File 'lib/typhoeus/hydra.rb', line 117

def on_complete(&block)
  @on_complete = block
end

#on_complete=(proc) ⇒ Object



121
122
123
# File 'lib/typhoeus/hydra.rb', line 121

def on_complete=(proc)
  @on_complete = proc
end

#queue(request, obey_concurrency_limit = true) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/typhoeus/hydra.rb', line 58

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

  # At this point, we are running over live HTTP. Make sure we haven't
  # disabled live requests.
  check_allow_net_connect!(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



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/typhoeus/hydra.rb', line 86

def run
  while !@active_stubs.empty?
    m = @active_stubs.first
    while request = m.requests.shift
      response = m.response
      response.request = request
      handle_request(request, response)
    end
    @active_stubs.delete(m)
  end

  @multi.perform
ensure
  @multi.reset_easy_handles{|easy| release_easy_object(easy)}
  @memoized_requests = {}
  @retrieved_from_cache = {}
  @running_requests = 0
end