class DiasporaFederation::Federation::Sender::HydraWrapper
A wrapper for [Typhoeus::Hydra]
Uses parallel http requests to send out the salmon-messages
Public Class Methods
Hydra default opts @return [Hash] hydra opts
# File lib/diaspora_federation/federation/sender/hydra_wrapper.rb, line 16 def self.hydra_opts @hydra_opts ||= { followlocation: false, timeout: DiasporaFederation.http_timeout, method: :post, verbose: DiasporaFederation.http_verbose, cainfo: DiasporaFederation.certificate_authorities, forbid_reuse: true } end
# File lib/diaspora_federation/federation/sender/hydra_wrapper.rb, line 34 def self.json_headers @json_headers ||= { "Content-Type" => "application/json", "User-Agent" => DiasporaFederation.http_user_agent } end
Create a new instance for a message
@param [String] sender_id sender diaspora-ID @param [String] obj_str object string representation for logging (e.g. type@guid)
# File lib/diaspora_federation/federation/sender/hydra_wrapper.rb, line 45 def initialize(sender_id, obj_str) @sender_id = sender_id @obj_str = obj_str @urls_to_retry = [] end
# File lib/diaspora_federation/federation/sender/hydra_wrapper.rb, line 27 def self.xml_headers @xml_headers ||= { "Content-Type" => "application/magic-envelope+xml", "User-Agent" => DiasporaFederation.http_user_agent } end
Public Instance Methods
Prepares and inserts a private encrypted MagicEnvelope job into the hydra queue @param [String] url the receive-url for the message @param [String] json encrypted MagicEnvelope json
# File lib/diaspora_federation/federation/sender/hydra_wrapper.rb, line 61 def insert_enc_magic_env_request(url, json) insert_job(url, HydraWrapper.hydra_opts.merge(body: json, headers: HydraWrapper.json_headers)) end
Prepares and inserts a public MagicEnvelope job into the hydra queue @param [String] url the receive-url for the xml @param [String] xml MagicEnvelope xml
# File lib/diaspora_federation/federation/sender/hydra_wrapper.rb, line 54 def insert_magic_env_request(url, xml) insert_job(url, HydraWrapper.hydra_opts.merge(body: xml, headers: HydraWrapper.xml_headers)) end
Sends all queued messages @return [Array<String>] urls to retry
# File lib/diaspora_federation/federation/sender/hydra_wrapper.rb, line 67 def send hydra.run @urls_to_retry end
Private Instance Methods
@return [Typhoeus::Hydra] hydra
# File lib/diaspora_federation/federation/sender/hydra_wrapper.rb, line 84 def hydra @hydra ||= Typhoeus::Hydra.new(max_concurrency: DiasporaFederation.http_concurrency) end
Prepares and inserts job into the hydra queue @param [String] url the receive-url for the message @param [Hash] options request options
# File lib/diaspora_federation/federation/sender/hydra_wrapper.rb, line 77 def insert_job(url, options) request = Typhoeus::Request.new(url, options) prepare_request(request) hydra.queue(request) end
Logic for after complete @param [Typhoeus::Request] request
# File lib/diaspora_federation/federation/sender/hydra_wrapper.rb, line 90 def prepare_request(request) request.on_complete do |response| DiasporaFederation.callbacks.trigger(:update_pod, request.url, status_from_response(response)) log_line = "success=#{response.success?} sender=#{@sender_id} obj=#{@obj_str} url=#{request.url} " \ "message=#{response.return_code} code=#{response.response_code} time=#{response.total_time}" if response.success? logger.info(log_line) else logger.warn(log_line) @urls_to_retry << request.url end end end
# File lib/diaspora_federation/federation/sender/hydra_wrapper.rb, line 105 def status_from_response(response) response.return_code == :ok ? response.response_code : response.return_code end