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

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.



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

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

Class Method Details

.hydraObject



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

def self.hydra
  @hydra ||= new
end

.hydra=(val) ⇒ Object



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

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…



42
43
44
# File 'lib/typhoeus/hydra.rb', line 42

def abort
  @queued_requests.clear
end

#cache_getter(&block) ⇒ Object



107
108
109
# File 'lib/typhoeus/hydra.rb', line 107

def cache_getter(&block)
  @cache_getter = block
end

#cache_setter(&block) ⇒ Object



111
112
113
# File 'lib/typhoeus/hydra.rb', line 111

def cache_setter(&block)
  @cache_setter = block
end

#clear_cache_callbacksObject



46
47
48
49
# File 'lib/typhoeus/hydra.rb', line 46

def clear_cache_callbacks
  @cache_setter = nil
  @cache_getter = nil
end

#disable_memoizationObject



103
104
105
# File 'lib/typhoeus/hydra.rb', line 103

def disable_memoization
  @memoize_requests = false
end

#fire_and_forgetObject



51
52
53
54
# File 'lib/typhoeus/hydra.rb', line 51

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

#on_complete(&block) ⇒ Object



115
116
117
# File 'lib/typhoeus/hydra.rb', line 115

def on_complete(&block)
  @on_complete = block
end

#on_complete=(proc) ⇒ Object



119
120
121
# File 'lib/typhoeus/hydra.rb', line 119

def on_complete=(proc)
  @on_complete = proc
end

#queue(request, obey_concurrency_limit = true) ⇒ Object



56
57
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
# File 'lib/typhoeus/hydra.rb', line 56

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



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

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