<commit_before>module Faraday
  module Adapter
    module Typhoeus
      extend Faraday::Connection::Options

      begin
        require 'typhoeus'

        def in_parallel?
          !!@parallel_manager
        end
        
        def in_parallel(options = {})
          setup_parallel_manager(options)
          yield
          run_parallel_requests
        end
        
        def setup_parallel_manager(options = {})
          @parallel_manager ||= ::Typhoeus::Hydra.new(options)
        end
        
        def run_parallel_requests
          @parallel_manager.run
          @parallel_manager = nil
        end
        
        def _post(uri, data, request_headers)
          _perform(:post, uri, :headers => request_headers, :params => data)
        end

        def _get(uri, request_headers)
          _perform(:get, uri, :headers => request_headers)
        end

        def _put(uri, data, request_headers)
          _perform(:put, uri, :headers => request_headers, :params => data)
        end

        def _delete(uri, request_headers)
          _perform(:delete, uri, :headers => request_headers)
        end

        def _perform method, uri, params
          response_class.new do |resp|
            is_async = in_parallel?
            setup_parallel_manager
            params[:method] = method
            req      = ::Typhoeus::Request.new(uri.to_s, params)
            req.on_complete do |response|
              raise Faraday::Error::ResourceNotFound if response.code == 404
              resp.process!(response.body)
              resp.headers = parse_response_headers(response.headers)
            end
            @parallel_manager.queue(req)
            if !is_async then run_parallel_requests end
          end
        rescue Errno::ECONNREFUSED
          raise Faraday::Error::ConnectionFailed, "connection refused"
        end

        def parse_response_headers(header_string)
          Hash[*header_string.split(/\r\n/).
            tap  { |a|      a.shift           }. # drop the HTTP status line
            map! { |h|      h.split(/:\s+/,2) }. # split key and value
            map! { |(k, v)| [k.downcase, v]   }.flatten!]
        end
      rescue LoadError => e
        self.load_error = e
      end
    end
  end
end
<commit_msg>Fix an intermittent live test error with Typhoeus.<commit_after>module Faraday
  module Adapter
    module Typhoeus
      extend Faraday::Connection::Options

      begin
        require 'typhoeus'

        def in_parallel?
          !!@parallel_manager
        end
        
        def in_parallel(options = {})
          setup_parallel_manager(options)
          yield
          run_parallel_requests
        end
        
        def setup_parallel_manager(options = {})
          @parallel_manager ||= ::Typhoeus::Hydra.new(options)
        end
        
        def run_parallel_requests
          @parallel_manager.run
          @parallel_manager = nil
        end
        
        def _post(uri, data, request_headers)
          _perform(:post, uri, :headers => request_headers, :params => data)
        end

        def _get(uri, request_headers)
          _perform(:get, uri, :headers => request_headers)
        end

        def _put(uri, data, request_headers)
          _perform(:put, uri, :headers => request_headers, :params => data)
        end

        def _delete(uri, request_headers)
          _perform(:delete, uri, :headers => request_headers)
        end

        def _perform method, uri, params
          response_class.new do |resp|
            is_async = in_parallel?
            setup_parallel_manager
            params[:method] = method
            req      = ::Typhoeus::Request.new(uri.to_s, params)
            req.on_complete do |response|
              raise Faraday::Error::ResourceNotFound if response.code == 404
              resp.process!(response.body)
              resp.headers = parse_response_headers(response.headers)
            end
            @parallel_manager.queue(req)
            if !is_async then run_parallel_requests end
          end
        rescue Errno::ECONNREFUSED
          raise Faraday::Error::ConnectionFailed, "connection refused"
        end

        def parse_response_headers(header_string)
          return {} unless header_string # XXX
          Hash[*header_string.split(/\r\n/).
            tap  { |a|      a.shift           }. # drop the HTTP status line
            map! { |h|      h.split(/:\s+/,2) }. # split key and value
            map! { |(k, v)| [k.downcase, v]   }.flatten!]
        end
      rescue LoadError => e
        self.load_error = e
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.0887830229536596, 'fraction_numerical': 0.0017323516673884798, 'mean_word_length': 1.7909365558912387, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3198874', 'n_tokens_mistral': 1291, 'n_tokens_neox': 1262, 'n_words': 303}