This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
module Typhoeus
|
||||
class Hydra
|
||||
|
||||
# This module handles the request adding on
|
||||
# hydra.
|
||||
#
|
||||
# @api private
|
||||
module Addable
|
||||
|
||||
# Adds request to multi.
|
||||
#
|
||||
# @example Add request.
|
||||
# hydra.add(request)
|
||||
#
|
||||
# @param [ Typhoeus::Request ] request to add.
|
||||
#
|
||||
# @return [ void ]
|
||||
def add(request)
|
||||
multi.add(EasyFactory.new(request, self).get)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,31 @@
|
||||
module Typhoeus
|
||||
class Hydra
|
||||
|
||||
# This module provides a way to hook into before
|
||||
# a request gets queued in hydra. This is very powerful
|
||||
# and you should be careful because when you accidently
|
||||
# return a falsy value the request won't be executed.
|
||||
#
|
||||
# @api private
|
||||
module Before
|
||||
|
||||
# Overrride add in order to execute callbacks in
|
||||
# Typhoeus.before. Will break and return when a
|
||||
# callback returns nil, false or a response. Calls super
|
||||
# otherwise.
|
||||
#
|
||||
# @example Add the request.
|
||||
# hydra.add(request)
|
||||
def add(request)
|
||||
Typhoeus.before.each do |callback|
|
||||
value = callback.call(request)
|
||||
if value.nil? || value == false || value.is_a?(Response)
|
||||
dequeue
|
||||
return value
|
||||
end
|
||||
end
|
||||
super
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,35 @@
|
||||
module Typhoeus
|
||||
class Hydra
|
||||
|
||||
# This module handles the blocked connection request mode on
|
||||
# the hydra side, where only stubbed requests
|
||||
# are allowed.
|
||||
# Connection blocking needs to be turned on:
|
||||
# Typhoeus.configure do |config|
|
||||
# config.block_connection = true
|
||||
# end
|
||||
#
|
||||
# When trying to do real requests a NoStub error
|
||||
# is raised.
|
||||
#
|
||||
# @api private
|
||||
module BlockConnection
|
||||
|
||||
# Overrides add in order to check before if block connection
|
||||
# is turned on. If thats the case a NoStub error is
|
||||
# raised.
|
||||
#
|
||||
# @example Add the request.
|
||||
# hydra.add(request)
|
||||
#
|
||||
# @param [ Request ] request The request to enqueue.
|
||||
def add(request)
|
||||
if request.blocked?
|
||||
raise Typhoeus::Errors::NoStub.new(request)
|
||||
else
|
||||
super
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,15 @@
|
||||
module Typhoeus
|
||||
class Hydra
|
||||
module Cacheable
|
||||
def add(request)
|
||||
if request.cacheable? && response = request.cached_response
|
||||
response.cached = true
|
||||
request.finish(response)
|
||||
dequeue
|
||||
else
|
||||
super
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,56 @@
|
||||
module Typhoeus
|
||||
class Hydra
|
||||
|
||||
# This module handles the GET request memoization
|
||||
# on the hydra side. Memoization needs to be turned
|
||||
# on:
|
||||
# Typhoeus.configure do |config|
|
||||
# config.memoize = true
|
||||
# end
|
||||
#
|
||||
# @api private
|
||||
module Memoizable
|
||||
|
||||
# Return the memory.
|
||||
#
|
||||
# @example Return the memory.
|
||||
# hydra.memory
|
||||
#
|
||||
# @return [ Hash ] The memory.
|
||||
def memory
|
||||
@memory ||= {}
|
||||
end
|
||||
|
||||
# Overrides add in order to check before if request
|
||||
# is memoizable and already in memory. If thats the case,
|
||||
# super is not called, instead the response is set and
|
||||
# the on_complete callback called.
|
||||
#
|
||||
# @example Add the request.
|
||||
# hydra.add(request)
|
||||
#
|
||||
# @param [ Request ] request The request to add.
|
||||
#
|
||||
# @return [ Request ] The added request.
|
||||
def add(request)
|
||||
if request.memoizable? && memory.has_key?(request)
|
||||
response = memory[request]
|
||||
request.finish(response, true)
|
||||
dequeue
|
||||
else
|
||||
super
|
||||
end
|
||||
end
|
||||
|
||||
# Overrides run to make sure the memory is cleared after
|
||||
# each run.
|
||||
#
|
||||
# @example Run hydra.
|
||||
# hydra.run
|
||||
def run
|
||||
super
|
||||
memory.clear
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,83 @@
|
||||
module Typhoeus
|
||||
class Hydra
|
||||
|
||||
# This module handles the request queueing on
|
||||
# hydra.
|
||||
#
|
||||
# @api private
|
||||
module Queueable
|
||||
|
||||
# Return the queued requests.
|
||||
#
|
||||
# @example Return queued requests.
|
||||
# hydra.queued_requests
|
||||
#
|
||||
# @return [ Array<Typhoeus::Request> ] The queued requests.
|
||||
def queued_requests
|
||||
@queued_requests ||= []
|
||||
end
|
||||
|
||||
# Abort the current hydra run as good as
|
||||
# possible. This means that it only
|
||||
# clears the queued requests and can't do
|
||||
# anything about already running requests.
|
||||
#
|
||||
# @example Abort hydra.
|
||||
# hydra.abort
|
||||
def abort
|
||||
queued_requests.clear
|
||||
end
|
||||
|
||||
# Enqueues a request in order to be performed
|
||||
# by the hydra. This can even be done while
|
||||
# the hydra is running. Also sets hydra on
|
||||
# request.
|
||||
#
|
||||
# @example Queue request.
|
||||
# hydra.queue(request)
|
||||
def queue(request)
|
||||
request.hydra = self
|
||||
queued_requests << request
|
||||
end
|
||||
|
||||
# Pushes a request to the front of the queue,
|
||||
# to be performed by the hydra. Also sets hydra
|
||||
# on request
|
||||
#
|
||||
# @example Queue reques.
|
||||
# hydra.queue_front(request)
|
||||
def queue_front(request)
|
||||
request.hydra = self
|
||||
queued_requests.unshift request
|
||||
end
|
||||
|
||||
# Removes a request from queued_requests and
|
||||
# adds it to the hydra in order to be
|
||||
# performed next.
|
||||
#
|
||||
# @example Dequeue request.
|
||||
# hydra.dequeue
|
||||
#
|
||||
# @since 0.6.4
|
||||
def dequeue
|
||||
add(queued_requests.shift) unless queued_requests.empty?
|
||||
end
|
||||
|
||||
# Removes requests from queued_requests and
|
||||
# adds them to the hydra until max_concurrency
|
||||
# is reached.
|
||||
#
|
||||
# @example Dequeue requests.
|
||||
# hydra.dequeue_many
|
||||
#
|
||||
# @since 0.6.8
|
||||
def dequeue_many
|
||||
number = multi.easy_handles.count
|
||||
until number == max_concurrency || queued_requests.empty?
|
||||
add(queued_requests.shift)
|
||||
number += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,19 @@
|
||||
module Typhoeus
|
||||
class Hydra
|
||||
|
||||
# This module contains logic to run a hydra.
|
||||
module Runnable
|
||||
|
||||
# Start the hydra run.
|
||||
#
|
||||
# @example Start hydra run.
|
||||
# hydra.run
|
||||
#
|
||||
# @return [ Symbol ] Return value from multi.perform.
|
||||
def run
|
||||
dequeue_many
|
||||
multi.perform
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,28 @@
|
||||
module Typhoeus
|
||||
class Hydra
|
||||
|
||||
# This module handles stubbing on the hydra side.
|
||||
# It plays well with the block_connection configuration,
|
||||
# which raises when you make a request which is not stubbed.
|
||||
#
|
||||
# @api private
|
||||
module Stubbable
|
||||
|
||||
# Override add in order to check for matching expecations.
|
||||
# When an expecation is found, super is not called. Instead a
|
||||
# canned response is assigned to the request.
|
||||
#
|
||||
# @example Add the request.
|
||||
# hydra.add(request)
|
||||
def add(request)
|
||||
if response = Expectation.response_for(request)
|
||||
request.execute_headers_callbacks(response)
|
||||
request.on_body.each{ |callback| callback.call(response.body, response) }
|
||||
request.finish(response)
|
||||
else
|
||||
super
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user