Sha256: 9a6ea05871da61b5fb4aae021a6209953a8a7b3b022dd0d25e912e70fb49ee90

Contents?: true

Size: 1.54 KB

Versions: 7

Compression:

Stored size: 1.54 KB

Contents

class PersistentConnectionAdapter < HTTParty::ConnectionAdapter

  # basically just reconnect if you get an EOFError
  class PersistentHTTP < Net::HTTP
    def request_with_retry(req, body = nil, &block)
      begin
        request_without_retry req, body, &block
      rescue EOFError => e # means the server closed the connection most likely
        @socket.close if @socket and not @socket.closed?
        connect
        request_without_retry req, body, &block
      end
    end
    alias request_without_retry request
    alias request request_with_retry
  end

  # copied from HTTParty::ConnectionAdapter
  def connection
    PersistentHTTP.new(clean_host(uri.host), uri.port, options[:http_proxyaddr], options[:http_proxyport], options[:http_proxyuser], options[:http_proxypass]).tap do |http|
      http.use_ssl = ssl_implied?(uri)

      attach_ssl_certificates(http, options)

      if options[:timeout] && (options[:timeout].is_a?(Integer) || options[:timeout].is_a?(Float))
        http.open_timeout = options[:timeout]
        http.read_timeout = options[:timeout]
      end

      if options[:debug_output]
        http.set_debug_output(options[:debug_output])
      end

      if options[:ciphers]
        http.ciphers = options[:ciphers]
      end
    end
  end

  def self.start(uri, options)
    @conn ||= new(uri, options).connection.tap { |c| c.start }
  end

  def self.close
    if @conn
      @conn.finish if @conn.started?
      @conn = nil
    end
  end

  def self.conn
    @conn
  end

  def self.call(uri, options)
    self.start uri, options
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
sessionm-resthome-0.8.11 lib/resthome/httparty/persistent_connection_adapter.rb
sessionm-resthome-0.8.10 lib/resthome/httparty/persistent_connection_adapter.rb
sessionm-resthome-0.8.9 lib/resthome/httparty/persistent_connection_adapter.rb
sessionm-resthome-0.8.8 lib/resthome/httparty/persistent_connection_adapter.rb
sessionm-resthome-0.8.7 lib/resthome/httparty/persistent_connection_adapter.rb
sessionm-resthome-0.8.6 lib/resthome/httparty/persistent_connection_adapter.rb
sessionm-resthome-0.8.5 lib/resthome/httparty/persistent_connection_adapter.rb