Skip to content

Instantly share code, notes, and snippets.

@ioquatix
Created February 22, 2020 01:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ioquatix/5e0877ab735ada54746f9f284f068e66 to your computer and use it in GitHub Desktop.
Save ioquatix/5e0877ab735ada54746f9f284f068e66 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'async'
require 'async/http/internet'
Async do |task|
internet = Async::HTTP::Internet.new
# Effectively a connection timeout:
response = task.with_timeout(10) do
internet.get("https://www.google.com/search?q=things")
end
# Effectively a read timeout:
task.with_timeout(1) do |timer|
while chunk = response.read
timer.reset
# Process chunk.
puts chunk.inspect
end
end
ensure
internet.close
end
@BMorearty
Copy link

Something like this?

      def optional_timeout(task, timeout)
        if timeout
          task.timeout(timeout) { yield }
        else
          yield
        end
      end

      response = optional_timeout(task, connect_timeout) { request_client.call(request) }
      body = optional_timeout(task, env.timeout) { response.read }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment