Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

todo #18981 : added timeout tuning (:timeout / :to)

  • Loading branch information...
commit a8e7dc8b79e15249ff75890ba373c6214dbad5ea 1 parent 9a7742c
John Mettraux jmettraux authored
4 CHANGELOG.txt
View
@@ -1,6 +1,10 @@
= rufus-verbs CHANGELOG.txt
+== rufus-verbs - 0.10 not yet released
+
+- todo #18981 : added timeout tuning (:timeout / :to)
+
== rufus-verbs - 0.9 released 2008/04/10
6 README.txt
View
@@ -288,6 +288,12 @@ the resource (or the middle) of a full resource path (see :base)
* <b>:ssl_verify_peer</b> (boolean, RE)
by default, rufus-verbs doesn't verify ssl certificates. With this option set to true, it will.
+* <b>:timeout</b> (integer, RE)
+sets the timeout (both open_ and read_timeout) for the request (and the endpoint), expects a value expressed in seconds.
+
+* <b>:to</b> (integer, RE)
+shortcut for :timeout
+
* <b>:u</b> (uri, string, RE)
the short version of :uri
4 lib/rufus/verbs.rb
View
@@ -8,10 +8,10 @@
# 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
73 lib/rufus/verbs/endpoint.rb
View
@@ -8,10 +8,10 @@
# 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
@@ -49,12 +49,12 @@ module Verbs
USER_AGENT = "Ruby rufus-verbs #{VERSION}"
#
- # An EndPoint can be used to share common options among a set of
+ # An EndPoint can be used to share common options among a set of
# requests.
#
# ep = EndPoint.new(
- # :host => "restful.server",
- # :port => 7080,
+ # :host => "restful.server",
+ # :port => 7080,
# :resource => "inventory/tools")
#
# res = ep.get :id => 1
@@ -63,7 +63,7 @@ module Verbs
# res = ep.get :id => 0
# # where did the hammer go ?
#
- # When a request gets prepared, the option values will be looked up
+ # When a request gets prepared, the option values will be looked up
# in (1) its local (request) options, then (2) in the EndPoint options.
#
class EndPoint
@@ -83,7 +83,7 @@ def initialize (opts)
compute_target @opts
- @opts[:http_basic_authentication] =
+ @opts[:http_basic_authentication] =
opts[:http_basic_authentication] || opts[:hba]
@opts[:user_agent] ||= USER_AGENT
@@ -126,7 +126,7 @@ def options (*args)
#
# This is the method called by the module methods verbs.
#
- # For example,
+ # For example,
#
# RufusVerbs.get(args)
#
@@ -264,10 +264,10 @@ def compute_target (opts)
elsif u
u = URI.parse u.to_s unless u.is_a?(URI)
- [ u.scheme,
- u.host,
- u.port,
- u.path,
+ [ u.scheme,
+ u.host,
+ u.port,
+ u.path,
query_to_h(u.query) ]
else
@@ -279,18 +279,18 @@ def compute_target (opts)
opts[:port] = r[2] || @opts[:port]
opts[:path] = r[3] || @opts[:path]
- opts[:query] =
- r[4] ||
+ opts[:query] =
+ r[4] ||
opts[:params] || opts[:query] ||
- @opts[:query] || @opts[:params] ||
+ @opts[:query] || @opts[:params] ||
{}
opts.delete :path if opts[:path] == ""
- opts[:c_uri] = [
- opts[:scheme],
- opts[:host],
- opts[:port],
+ opts[:c_uri] = [
+ opts[:scheme],
+ opts[:host],
+ opts[:port],
opts[:path],
opts[:query] ].inspect
#
@@ -303,7 +303,7 @@ def compute_target (opts)
# Creates the Net::HTTP request instance.
#
# If :fake_put is set, will use Net::HTTP::Post
- # and make sure the query string contains '_method=put' (or
+ # and make sure the query string contains '_method=put' (or
# '_method=delete').
#
# This call will also advertise this rufus-verbs as
@@ -311,7 +311,7 @@ def compute_target (opts)
#
def create_request (method, opts)
- if (o(opts, :fake_put) and
+ if (o(opts, :fake_put) and
(method == :put or method == :delete))
opts[:query][:_method] = method.to_s
@@ -362,7 +362,7 @@ def add_authentication (req, opts)
end
#
- # In that base class, it's empty.
+ # In that base class, it's empty.
# It's implemented in ConditionalEndPoint.
#
# Only called for a GET.
@@ -381,10 +381,12 @@ def prepare_http (opts)
compute_proxy opts
http = Net::HTTP.new(
- opts[:host], opts[:port],
+ opts[:host], opts[:port],
opts[:proxy_host], opts[:proxy_port],
opts[:proxy_user], opts[:proxy_pass])
+ set_timeout http, opts
+
return http unless opts[:scheme] == 'https'
require 'net/https'
@@ -406,6 +408,21 @@ def prepare_http (opts)
end
#
+ # Sets both the open_timeout and the read_timeout for the http
+ # instance
+ #
+ def set_timeout (http, opts)
+
+ to = o(opts, :timeout) || o(opts, :to)
+ to = to.to_i
+
+ return if to == 0
+
+ http.open_timeout = to
+ http.read_timeout = to
+ end
+
+ #
# Makes sure the request opts hold the proxy information.
#
# If the option :proxy is set to false, no proxy will be used.
@@ -466,7 +483,7 @@ def query_to_h (q)
return nil unless q
- q.split("&").inject({}) do |r, e|
+ q.split("&").inject({}) do |r, e|
s = e.split("=")
r[s[0]] = s[1]
r
@@ -474,11 +491,11 @@ def query_to_h (q)
end
#
- # { "a" => "A", "b" => "B" } -> "a=A&b=B"
+ # { "a" => "A", "b" => "B" } -> "a=A&b=B"
#
def h_to_query (h, opts)
- h.entries.collect { |k, v|
+ h.entries.collect { |k, v|
unless o(opts, :no_escape)
k = URI.escape k.to_s
v = URI.escape v.to_s
@@ -502,7 +519,7 @@ def add_payload (req, opts, &block)
req.set_form_data fd, sep
elsif block
req.body = block.call req
- else
+ else
req.body = ""
end
end
@@ -544,7 +561,7 @@ def handle_response (method, res, opts)
opts[:path], opts[:query] = location.split "?"
end
- if (authentication_is_on?(opts) and
+ if (authentication_is_on?(opts) and
[ opts[:scheme], opts[:host] ] != prev_host)
raise(
12 test/items.rb
View
@@ -276,6 +276,17 @@ def get_session (req, res)
end
#
+# a servlet that doesn't reply (for timeout testing)
+#
+class LostServlet < WEBrick::HTTPServlet::AbstractServlet
+
+ def do_GET (req, res)
+
+ sleep 200
+ end
+end
+
+#
# Serving items, a dummy resource...
# Also serving things, which just redirect to items...
#
@@ -300,6 +311,7 @@ class << @server
@server.mount "/items", ItemServlet
@server.mount "/things", ThingServlet
@server.mount "/cookie", CookieServlet
+ @server.mount "/lost", LostServlet
[ 'INT', 'TERM' ].each do |signal|
trap(signal) { shutdown }
2  test/test.rb
View
@@ -24,3 +24,5 @@
require 'uri_test'
require 'fopen_test'
+require 'timeout_test'
+
38 test/timeout_test.rb
View
@@ -0,0 +1,38 @@
+
+#
+# Testing rufus-verbs
+#
+# jmettraux@gmail.com
+#
+# Mon May 26 17:04:25 JST 2008
+#
+
+require 'rubygems'
+
+require 'test/unit'
+require 'testbase'
+
+require 'rufus/verbs'
+
+
+class TimeoutTest < Test::Unit::TestCase
+ include TestBaseMixin
+
+ include Rufus::Verbs
+
+
+ def test_0
+
+ error = nil
+ t = Time.now
+
+ begin
+ get :uri => "http://localhost:7777/lost", :to => 1
+ rescue Timeout::Error => e
+ error = e
+ end
+
+ assert_not_nil error
+ assert (Time.now - t < 2)
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.