Skip to content

Redirects and Timeouts

igrigorik edited this page Apr 13, 2011 · 4 revisions

Redirects and Timeouts

Timeouts

em-http supports two types of timeouts: connection timeouts, and inactivity timeouts.

  • Connect timeouts (default: 5s) are invoked during the initial connection setup. If the server is slow to respond or overloaded, EventMachine will cancel the request and the errback function will be invoked.
  • Inactivity timeouts (default: 10s) are invoked after a successful connection has been made, but no data has been exchanged by either side. In cases where you have a long-lived connection (ex: stream of data from a firehose API), you may want to set this timeout to 0, which will effectively disable it.
EventMachine.run do
  http = EventMachine::HttpRequest.new('http://www.google.com/', :connect_timeout => 5, :inactivity_timeout => 10).get
  http.callback { }
  http.errback  { }
end

Redirects

em-http can automatically handle 3XX redirects on your behalf. By default, the library will return the 3XX response along with the headers. However, you can provide a :redirects => depth key when you make the request to tell em-http to follow redirects up to a certain depth.

EventMachine.run do
  http = EventMachine::HttpRequest.new('http://www.google.com/').get :redirects => 1
  http.callback { p http.last_effective_url }
end

If the redirects key is provided, em-http will transparently follow all 3XX redirect codes. To check what the final URL you arrived at is, check http.last_effective_url.

Following selective redirects

Sometimes you want to restrict which redirects you follow. For example, let’s say we want to follow bit.ly links to their destinations, but we want to avoid making any requests to yahoo.com. In this case, we can setup a headers callback and inspect the headers as em-http makes the requests – anytime em-http finishes parsing the requests, this function is called. Within the headers callback, we can inspect the Location header and selective abort the connection!

EM.run do
  c = EM::HttpRequest.new('http://bit.ly/epqosH').get :redirects => 1
  c.headers do
    if c.response_header['LOCATION'] !~ /gist/
      puts "Not pointing to a gist, aborting connection"
      c.close
    end
  end

  c.callback { puts 'done' }
  c.errback  { puts 'err' }
end