Typhoeus

http://github.com/pauldix/typhoeus/tree/master

the mailing list

Thanks to my employer kgbweb for allowing me to release this as open source. Btw, we’re hiring and we work on cool stuff like this every day. Get a hold of me if you rock at rails/js/html/css or if you have experience in search, information retrieval, and machine learning.

I also wanted to thank Todd A. Fisher. I ripped a good chunk of the c libcurl-multi code from his update to Curb. Awesome stuff Todd!

Summary

Like a modern code version of the mythical beast with 100 serpent heads, Typhoeus runs HTTP requests in parallel while cleanly encapsulating handling logic. To be a little more specific, it’s a library for accessing web services in Ruby. It’s specifically designed for building RESTful service oriented architectures in Ruby that need to be fast enough to process calls to multiple services within the client’s HTTP request/response life cycle.

Some of the awesome features are parallel request execution, memoization of request responses (so you don’t make the same request multiple times in a single group), built in support for caching responses to memcached (or whatever), and mocking capability baked in. It uses libcurl and libcurl-multi to work this speedy magic. I wrote the c bindings myself so it’s yet another Ruby libcurl library, but with some extra awesomeness added in.

Installation

Typhoeus requires you to have a current version of libcurl installed. I’ve tested this with 7.19.4 and higher.


gem install typhoeus <del>-source http://gemcutter.org
If you’re on Debian or Ubuntu and getting errors while trying to install, it could be because you don’t have the latest version of libcurl installed. Do this to fix:

sudo apt-get install libcurl4-gnutls-dev
There’s also something built in so that if you have a super old version of curl that you can’t get rid of for some reason, you can install in a user directory and specify that during installation like so:

gem install typhoeus —source http://gemcutter.org -</del> —with-curl=/usr/local/curl/7.19.7/

Another problem could be if you are running Mac Ports and you have libcurl installed through there. You need to uninstall it for Typhoeus to work! The version in Mac Ports is old and doesn’t play nice. You should download curl and build from source. Then you’ll have to install the gem again.

If you’re still having issues, please let me know on the mailing list.

There’s one other thing you should know. The Easy object (which is just a libcurl thing) allows you to set timeout values in milliseconds. However, for this to work you need to build libcurl with c-ares support built in.

Usage

Deprecation Warning! The old version of Typhoeus used a module that you included in your class to get functionality. That interface has been deprecated. Here is the new interface.

The primary interface for Typhoeus is comprised of three classes: Request, Response, and Hydra. Request represents an HTTP request object, response represents an HTTP response, and Hydra manages making parallel HTTP connections.


require 'rubygems'
require 'typhoeus'
require 'json'

# the request object
request = Typhoeus::Request.new("http://www.pauldix.net",
                                :body          => "this is a request body",
                                :method        => :post,
                                :headers       => {:Accepts => "text/html"},
                                :timeout       => 100, # milliseconds
                                :cache_timeout => 60, # seconds
                                :params        => {:field1 => "a field"})
# we can see from this that the first argument is the url. the second is a set of options.
# the options are all optional. The default for :method is :get. Timeout is measured in milliseconds.
# cache_timeout is measured in seconds.

# the response object will be set after the request is run
response = request.response
response.code    # http status code
response.time    # time in seconds the request took
response.headers # the http headers
response.headers_hash # http headers put into a hash
response.body    # the response body

Making Quick Requests The request object has some convenience methods for performing single HTTP requests. The arguments are the same as those you pass into the request constructor.


response = Typhoeus::Request.get("http://www.pauldix.net")
response = Typhoeus::Request.head("http://www.pauldix.net")
response = Typhoeus::Request.put("http://localhost:3000/posts/1", :body => "whoo, a body")
response = Typhoeus::Request.post("http://localhost:3000/posts", :params => {:title => "test post", :content => "this is my test"})
response = Typhoeus::Request.delete("http://localhost:3000/posts/1")

Making Parallel Requests


# Generally, you should be running requests through hydra. Here is how that looks
hydra = Typhoeus::Hydra.new

first_request = Typhoeus::Request.new("http://localhost:3000/posts/1.json")
first_request.on_complete do |response|
  post = JSON.parse(response.body)
  third_request = Typhoeus::Request.new(post.links.first) # get the first url in the post
  third_request.on_complete do |response|
    # do something with that
  end
  hydra.queue third_request
  return post
end
second_request = Typhoeus::Request.new("http://localhost:3000/users/1.json")
second_request.on_complete do |response|
  JSON.parse(response.body)
end
hydra.queue first_request
hydra.queue second_request
hydra.run # this is a blocking call that returns once all requests are complete

first_request.handled_resposne # the value returned from the on_complete block
second_request.handled_resposne # the value returned from the on_complete block (parsed JSON)

The execution of that code goes something like this. The first and second requests are built and queued. When hydra is run the first and second requests run in parallel. When the first request completes, the third request is then built and queued up. The moment it is queued Hydra starts executing it. Meanwhile the second request would continue to run (or it could have completed before the first). Once the third request is done, hydra.run returns.

Specifying Max Concurrency

Hydra will also handle how many requests you can make in parallel. Things will get flakey if you try to make too many requests at the same time. The built in limit is 200. When more requests than that are queued up, hydra will save them for later and start the requests as others are finished. You can raise or lower the concurrency limit through the Hydra constructor.


hydra = Typhoeus::Hydra.new(:max_concurrency => 20) # keep from killing some servers

Memoization Hydra memoizes requests within a single run call. You can also disable memoization.


hydra = Typhoeus::Hydra.new
2.times do
  r = Typhoeus::Request.new("http://localhost/3000/users/1")
  hydra.queue r
end
hydra.run # this will result in a single request being issued. However, the on_complete handlers of both will be called.
hydra.disable_memoization
2.times do
  r = Typhoeus::Request.new("http://localhost/3000/users/1")
  hydra.queue r
end
hydra.run # this will result in a two requests.

Caching Hydra includes built in support for creating cache getters and setters. In the following example, if there is a cache hit, the cached object is passed to the on_complete handler of the request object.


hydra = Typhoeus::Hydra.new
hydra.cache_setter do |request|
  @cache.set(request.cache_key, request.response, request.cache_timeout)
end

hydra.cache_getter do |request|
  @cache.get(request.cache_key) rescue nil
end

Stubbing Hydra allows you to stub out specific urls and patters to avoid hitting remote servers while testing.


hydra = Typhoeus::Hydra.new
response = Response.new(:code => 200, :headers => "", :body => "{'name' : 'paul'}", :time => 0.3)
hydra.stub(:get, "http://localhost:3000/users/1").and_return(response)

request = Typhoeus::Request.new("http://localhost:3000/users/1")
request.on_complete do |response|
  JSON.parse(response.body)
end
hydra.queue request
hydra.run

The queued request will hit the stub. The on_complete handler will be called and will be passed the response object. You can also specify a regex to match urls.


hydra.stub(:get, /http\:\/\/localhost\:3000\/users\/.*/).and_return(response)
# any requests for a user will be stubbed out with the pre built response.

The Singleton All of the quick requests are done using the singleton hydra object. If you want to enable caching or stubbing on the quick requests, set those options on the singleton.


hydra = Typhoeus::Hydra.hydra
hydra.stub(:get, "http://localhost:3000/users")

Basic Authentication


require 'base64'
response = Typhoeus::Request.get("http://twitter.com/statuses/followers.json",
                                 :headers => {"Authorization" => "Basic #{Base64.b64encode("#{username}:#{password}")}"})

SSL SSL comes built in to libcurl so it’s in Typhoeus as well. If you pass in a url with “https” it should just work assuming that you have your cert bundle in order and the server is verifiable. You must also have libcurl built with SSL support enabled. You can check that by doing this:


Typhoeus::Easy.new.curl_version # output should include OpenSSL/...

Now, even if you have libcurl built with OpenSSL you may still have a messed up cert bundle or if you’re hitting a non-verifiable SSL server then you’ll have to disable peer verification to make SSL work. Like this:


Typhoeus::Request.get("https://mail.google.com/mail", :disable_ssl_peer_verification => true)

LibCurl Typhoeus also has a more raw libcurl interface. These are the Easy and Multi objects. If you’re into accessing just the raw libcurl style, those are your best bet.

SSL Certs can be provided to the Easy interface :


e = Typhoeus::Easy.new
e.url = "https://example.com/action"
s.ssl_cacert = "ca_file.cer"
e.ssl_cert = "acert.crt"
e.ssl_key = "akey.key"
[...]
e.perform

or directly to a Typhoeus::Request :


e = Typhoeus::Request.get("https://example.com/action",
  :ssl_cacert => "ca_file.cer",
  :ssl_cert => "acert.crt",
  :ssl_key => "akey.key",
  [...]
end

NTLM authentication

Thanks for the authentication piece and this description go to Oleg Ivanov (morhekil). The major reason to start this fork was the need to perform NTLM authentication in Ruby. Now you can do it via Typhoeus::Easy interface using the following API.


e = Typhoeus::Easy.new
e.auth = {
  :username => 'username',
  :password => 'password',
  :method => Typhoeus::Easy::AUTH_TYPES[:CURLAUTH_NTLM]
}
e.url = "http://example.com/auth_ntlm"
e.method = :get
e.perform

Other authentication types

The following authentication types are available:

  • CURLAUTH_BASIC
  • CURLAUTH_DIGEST
  • CURLAUTH_GSSNEGOTIATE
  • CURLAUTH_NTLM
  • CURLAUTH_DIGEST_IE

Query of available auth types

After the initial request you can get the authentication types available on the server via Typhoues::Easy#auth_methods call. It will return a number that you’ll need to decode yourself, please refer to easy.rb source code to see the numeric values of different auth types.

Verbose debug output

Sometime it’s useful to see verbose output from curl. You may now enable it:


e = Typhoeus::Easy.new
e.verbose = 1

Please note that libcurl prints it’s output to the console, so you’ll need to run your scripts from the console to see the debug info.

Benchmarks

I set up a benchmark to test how the parallel performance works vs Ruby’s built in NET::HTTP. The setup was a local evented HTTP server that would take a request, sleep for 500 milliseconds and then issued a blank response. I set up the client to call this 20 times. Here are the results:


  net::http  0.030000   0.010000   0.040000 ( 10.054327)
  typhoeus   0.020000   0.070000   0.090000 (  0.508817)

We can see from this that NET::HTTP performs as expected, taking 10 seconds to run 20 500ms requests. Typhoeus only takes 500ms (the time of the response that took the longest.) One other thing to note is that Typhoeus keeps a pool of libcurl Easy handles to use. For this benchmark I warmed the pool first. So if you test this out it may be a bit slower until the Easy handle pool has enough in it to run all the simultaneous requests. For some reason the easy handles can take quite some time to allocate.

Next Steps

  • Add in ability to keep-alive requests and reuse them within hydra.
  • Add support for automatic retry, exponential back-off, and queuing for later.

LICENSE

(The MIT License)

Copyright © 2009:

Paul Dix

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.