rubytyphoeus

How to abort current request in on_body callback using Typhoeus


I'm using Typhoeus with an hydra queue of a dozen of requests.

I'm using an on_body callback to stream the response.

What is the best way to abort the current request inside this callback (ie. reached a max file size...) without aborting all the other requests in the queue of course ?

hydra = Typhoeus::Hydra.hydra

urls.each do |url|
  request = Typhoeus::Request.new(url, followlocation: true, timeout: 5, connecttimeout: 5)
  request.on_body do |chunk, response|
    #
    # How to conditionally abort the request here ?
    #        
  end

  hydra.queue request
end

hydra.run

Solution

  • There is no way at the moment. According to http://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLOPTWRITEFUNCTION it would be enough to return an invalid size. But that is not an option yet in Typhoeus since the callback itself is wrapped and it always returns the correct size: https://github.com/typhoeus/ethon/blob/master/lib/ethon/easy/callbacks.rb#L37-48.

    EDIT: Ethon 0.7.1 is able to abort requests in the on_body callback: https://github.com/typhoeus/ethon/commit/6117872004c9ed1dac0ac15542dffc10177d8eae.