Permalink
Browse files

Added support for net-http-persistent. Also, the underlying client li…

…b is not global anymore, you can choose it per client instance. And lastly, I added a simple performance test to compare all the support libraries.
  • Loading branch information...
1 parent 89e3d7c commit de8de99969db906d2dc43c466b99e16c9a33a639 @treeder treeder committed Jun 29, 2012
View
@@ -7,18 +7,25 @@ PATH
GEM
remote: https://rubygems.org/
specs:
+ ffi (1.0.11)
mime-types (1.18)
+ net-http-persistent (2.7)
rake (0.9.2.2)
rest-client (1.6.7)
mime-types (>= 1.16)
test-unit (2.5.0)
+ typhoeus (0.4.2)
+ ffi (~> 1.0)
+ mime-types (~> 1.18)
uber_config (0.0.3)
PLATFORMS
ruby
DEPENDENCIES
+ net-http-persistent
rake
rest!
test-unit
+ typhoeus
uber_config
View
@@ -1,43 +0,0 @@
-Rest Wrapper
--------------
-
-Getting Started
-==============
-
-Install the gem:
-
- gem install rest
-
-Create an IronMQ client object:
-
- @rest = Rest::Client.new()
-
-Then use it.
-
-GET
-=========
-
- @rest.get(url, options...)
-
-options:
-
-- :params => query params for url
-- :headers => headers
-
-POST
-======
-
- @rest.post(url, options...)
-
-options:
-
-- :body => POST body
-- :headers => headers
-
-DELETE
-======
-
- @rest.delete(url, options...)
-
-
-
View
@@ -0,0 +1,66 @@
+Rest Wrapper
+-------------
+
+HTTP/REST client wrapper that provides a standard interface for making http requests using different http clients.
+If no client is specified it will choose the best one you have installed.
+
+Getting Started
+==============
+
+Install the gem:
+
+ gem install rest
+
+Create an Rest client:
+
+ @rest = Rest::Client.new()
+
+To choose a specific underlying http client lib:
+
+ @rest = Rest::Client.new(:gem=>:typhoeus)
+
+Supported http libraries are:
+
+* rest-client
+* net-http-persistent
+* typhoeus
+
+Then use it:
+
+GET
+=========
+
+ @rest.get(url, options...)
+
+options:
+
+- :params => query params for url
+- :headers => headers
+
+POST
+======
+
+ @rest.post(url, options...)
+
+options:
+
+- :body => POST body
+- :headers => headers
+
+PUT
+======
+
+ @rest.put(url, options...)
+
+options:
+
+- :body => POST body
+- :headers => headers
+
+DELETE
+======
+
+ @rest.delete(url, options...)
+
+
+
View
@@ -20,33 +20,12 @@ def initialize(msg=nil)
end
def self.puts(s)
- Kernel.puts(s)
- end
-
-
- def self.gem=(g)
- @gem = g
- end
-
- def self.gem
- @gem
- end
-
- begin
- require 'typhoeus'
- Rest.gem = :typhoeus
- Rest.puts "Using typhoeus gem."
- require_relative 'wrappers/typhoeus_wrapper'
- rescue LoadError => ex
- Rest.puts "Please install 'typhoeus' gem for best performance."
- require 'rest_client'
- Rest.gem = :rest_client
- require_relative 'wrappers/rest_client_wrapper'
+ Kernel.puts("rest gem: #{s}")
end
class Client
- attr_accessor :options
+ attr_accessor :options, :logger, :gem
# options:
# - :gem => specify gem explicitly
#
@@ -55,14 +34,43 @@ def initialize(options={})
@logger.level=Logger::INFO
@options = options
- Rest.gem = options[:gem] if options[:gem]
+ @gem = options[:gem] if options[:gem]
- if Rest.gem == :typhoeus
+ if @gem.nil?
+ choose_best_gem()
+ end
+
+ if @gem == :typhoeus
+ require_relative 'wrappers/typhoeus_wrapper'
@wrapper = Rest::Wrappers::TyphoeusWrapper.new
+ Rest.puts "Using typhoeus gem."
+ elsif @gem == :net_http_persistent
+ require_relative 'wrappers/net_http_persistent_wrapper'
+ @wrapper = Rest::Wrappers::NetHttpPersistentWrapper.new(self)
+ Rest.puts "Using net-http-persistent gem."
else
+ require_relative 'wrappers/rest_client_wrapper'
@wrapper = Rest::Wrappers::RestClientWrapper.new
+ Rest.puts "Using rest-client gem. Please install 'typhoeus' or net-http-persistent gem for best performance."
end
+ end
+ def choose_best_gem
+ begin
+ raise LoadError
+ require 'typhoeus'
+ @gem = :client
+ rescue LoadError => ex
+ begin
+ # try net-http-persistent
+ require 'net/http/persistent'
+ @gem = :net_http_persistent
+
+ rescue LoadError => ex
+ require 'rest_client'
+ @gem = :rest_client
+ end
+ end
end
def get(url, req_hash={})
@@ -84,11 +92,11 @@ def perform_op(&blk)
#p res.code
if res.code == 503
pow = (4 ** (current_retry)) * 100 # milliseconds
- #puts 'pow=' + pow.to_s
+ #puts 'pow=' + pow.to_s
s = Random.rand * pow
- #puts 's=' + s.to_s
+ #puts 's=' + s.to_s
sleep_secs = 1.0 * s / 1000.0
- #puts 'sleep for ' + sleep_secs.to_s
+ #puts 'sleep for ' + sleep_secs.to_s
current_retry += 1
@logger.debug "503 Error. Retrying #{current_retry} out of #{max_retries} max."
sleep sleep_secs
@@ -125,5 +133,10 @@ def delete(url, req_hash={})
end
return res
end
+
+ def close
+ @wrapper.close
+ end
+
end
end
@@ -0,0 +1,7 @@
+class BaseWrapper
+
+ # if wrapper has a close/shutdown, override this
+ def close
+
+ end
+end
Oops, something went wrong.

0 comments on commit de8de99

Please sign in to comment.