http_configuration 1.0.2 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/MIT-LICENSE +1 -1
- data/{README → README.rodc} +0 -21
- data/Rakefile +30 -28
- data/VERSION +1 -0
- data/http_configuration.gemspec +53 -0
- data/lib/http_configuration.rb +27 -51
- data/spec/http_configuration_spec.rb +1 -23
- metadata +66 -23
- data/init.rb +0 -1
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
pkg
|
data/MIT-LICENSE
CHANGED
data/{README → README.rodc}
RENAMED
@@ -33,24 +33,3 @@ When a configuration is applied to a block, you can also pass in some override v
|
|
33
33
|
If your code has to live in an environment that requires a proxy server, you may have run into problems with externally developed code where the author didn't have to worry about proxies. With this code installed, you don't have to worry about breaking open the code and hacking it up to add proxy support.
|
34
34
|
|
35
35
|
Similarly, if you are building a high traffic web site that makes web service calls to external hosts, you may want to set HTTP timeouts to lower values so that if the external server is running slow, your server doesn't end up eating up all its threads waiting on that server. For example, suppose you have a site that handles 10 requests per second and 1% of those requests make a web service request to another server and to handle that traffic, you have 50 mongrel servers available. If the external service goes crazy for some reason and starts taking 60 seconds to serve a response that normally takes less than 1 second, you will start loosing a mongrel server every 10 seconds and your site will be completely down in about 10 minutes. You could significantly lower this risk by setting the open and read timeouts on HTTP to be a much smaller value. You'll still end up with errors, but the site won't go down.
|
36
|
-
|
37
|
-
== Fix For Timeouts
|
38
|
-
|
39
|
-
One potential issue you can have with the Net::HTTP is that it handles timeouts by throwing interrupts. This is very effective however, interrupts have the unfortunate side effect of killing the current thread. This can be quite a problem. Consider this code:
|
40
|
-
|
41
|
-
loop do
|
42
|
-
begin
|
43
|
-
begin_really_important_transaction()
|
44
|
-
Net::HTTP.get('http://www.example.com/really_important_request')
|
45
|
-
rescue => e
|
46
|
-
logger.warn(e)
|
47
|
-
Notifications.new(:critical).send("The really important request failed; this must be fixed immediately.")
|
48
|
-
ensure
|
49
|
-
finish_really_important_transaction()
|
50
|
-
end
|
51
|
-
sleep(300)
|
52
|
-
end
|
53
|
-
|
54
|
-
If the HTTP request times out, the code will not execute they way you'd think. It will receive an interrupt and immediately exit. The log warning and notification will not be sent. Further, the ensure block will not even be executed which could be really bad.
|
55
|
-
|
56
|
-
This code simply sets the default exception to be thrown by timeouts in the Net module to Net::NetworkTimeoutError instead of TimeoutError. Since Net::NetworkTimeoutError doesn't extend from Interrupt, your thread can go on its merry way.
|
data/Rakefile
CHANGED
@@ -1,43 +1,45 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'rake'
|
3
3
|
require 'rake/rdoctask'
|
4
|
-
require 'rake/gempackagetask'
|
5
|
-
require 'spec/rake/spectask'
|
6
4
|
|
7
5
|
desc 'Default: run unit tests.'
|
8
6
|
task :default => :test
|
9
7
|
|
10
|
-
|
11
|
-
|
12
|
-
|
8
|
+
begin
|
9
|
+
require 'spec/rake/spectask'
|
10
|
+
desc 'Test the gem.'
|
11
|
+
Spec::Rake::SpecTask.new(:test) do |t|
|
12
|
+
t.spec_files = FileList.new('spec/**/*_spec.rb')
|
13
|
+
end
|
14
|
+
rescue LoadError
|
15
|
+
tast :test do
|
16
|
+
STDERR.puts "You must have rspec >= 1.3.0 to run the tests"
|
17
|
+
end
|
13
18
|
end
|
14
19
|
|
15
|
-
desc 'Generate documentation for
|
20
|
+
desc 'Generate documentation for the gem.'
|
16
21
|
Rake::RDocTask.new(:rdoc) do |rdoc|
|
17
22
|
rdoc.rdoc_dir = 'rdoc'
|
18
|
-
rdoc.options << '--title' << 'HTTP Configuration' << '--line-numbers' << '--inline-source' << '--main' << 'README'
|
19
|
-
rdoc.rdoc_files.include('README')
|
23
|
+
rdoc.options << '--title' << 'HTTP Configuration' << '--line-numbers' << '--inline-source' << '--main' << 'README.rdoc'
|
24
|
+
rdoc.rdoc_files.include('README.rdoc')
|
20
25
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
21
26
|
end
|
22
27
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
28
|
+
begin
|
29
|
+
require 'jeweler'
|
30
|
+
Jeweler::Tasks.new do |gem|
|
31
|
+
gem.name = "http_configuration"
|
32
|
+
gem.summary = %Q{Gem that provides the ability to set defaults for proxies and timeouts for Net::HTTP.}
|
33
|
+
gem.description = %Q(Gem that provides the ability to set defaults for proxies and timeouts for Net::HTTP. Simplifies integration of HTTP calls into any environment and provides a unified interface for setting values.)
|
34
|
+
gem.email = "brian@embellishedvisions.com"
|
35
|
+
gem.homepage = "http://github.com/bdurand/acts_as_revisionable"
|
36
|
+
gem.authors = ["Brian Durand"]
|
37
|
+
gem.rdoc_options = ["--charset=UTF-8", "--main", "README.rdoc"]
|
38
|
+
|
39
|
+
gem.add_development_dependency('rspec', '>= 1.3.0')
|
40
|
+
gem.add_development_dependency('jeweler')
|
41
|
+
end
|
42
|
+
|
43
|
+
Jeweler::GemcutterTasks.new
|
44
|
+
rescue LoadError
|
39
45
|
end
|
40
|
-
|
41
|
-
Rake::GemPackageTask.new(spec) do |pkg|
|
42
|
-
pkg.need_tar = true
|
43
|
-
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.3
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{http_configuration}
|
8
|
+
s.version = "1.0.3"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Brian Durand"]
|
12
|
+
s.date = %q{2010-06-22}
|
13
|
+
s.description = %q{Gem that provides the ability to set defaults for proxies and timeouts for Net::HTTP. Simplifies integration of HTTP calls into any environment and provides a unified interface for setting values.}
|
14
|
+
s.email = %q{brian@embellishedvisions.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"README.rodc"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
".gitignore",
|
20
|
+
"MIT-LICENSE",
|
21
|
+
"README.rodc",
|
22
|
+
"Rakefile",
|
23
|
+
"VERSION",
|
24
|
+
"http_configuration.gemspec",
|
25
|
+
"lib/http_configuration.rb",
|
26
|
+
"spec/http_configuration_spec.rb"
|
27
|
+
]
|
28
|
+
s.homepage = %q{http://github.com/bdurand/acts_as_revisionable}
|
29
|
+
s.rdoc_options = ["--charset=UTF-8", "--main", "README.rdoc"]
|
30
|
+
s.require_paths = ["lib"]
|
31
|
+
s.rubygems_version = %q{1.3.7}
|
32
|
+
s.summary = %q{Gem that provides the ability to set defaults for proxies and timeouts for Net::HTTP.}
|
33
|
+
s.test_files = [
|
34
|
+
"spec/http_configuration_spec.rb"
|
35
|
+
]
|
36
|
+
|
37
|
+
if s.respond_to? :specification_version then
|
38
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
39
|
+
s.specification_version = 3
|
40
|
+
|
41
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
42
|
+
s.add_development_dependency(%q<rspec>, [">= 1.3.0"])
|
43
|
+
s.add_development_dependency(%q<jeweler>, [">= 0"])
|
44
|
+
else
|
45
|
+
s.add_dependency(%q<rspec>, [">= 1.3.0"])
|
46
|
+
s.add_dependency(%q<jeweler>, [">= 0"])
|
47
|
+
end
|
48
|
+
else
|
49
|
+
s.add_dependency(%q<rspec>, [">= 1.3.0"])
|
50
|
+
s.add_dependency(%q<jeweler>, [">= 0"])
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
data/lib/http_configuration.rb
CHANGED
@@ -2,33 +2,7 @@ require 'net/protocol'
|
|
2
2
|
require 'net/http'
|
3
3
|
|
4
4
|
module Net
|
5
|
-
|
6
|
-
class Protocol
|
7
|
-
|
8
|
-
private
|
9
|
-
|
10
|
-
# Default error type to a non-interrupt exception
|
11
|
-
def timeout (secs, errorType = NetworkTimeoutError)
|
12
|
-
super(secs, errorType)
|
13
|
-
end
|
14
|
-
|
15
|
-
end
|
16
|
-
|
17
|
-
class BufferedIO
|
18
5
|
|
19
|
-
private
|
20
|
-
|
21
|
-
# Default error type to a non-interrupt exception
|
22
|
-
def timeout (secs, errorType = NetworkTimeoutError)
|
23
|
-
super(secs, errorType)
|
24
|
-
end
|
25
|
-
|
26
|
-
end
|
27
|
-
|
28
|
-
# Error thrown by network timeouts
|
29
|
-
class NetworkTimeoutError < StandardError
|
30
|
-
end
|
31
|
-
|
32
6
|
class HTTP
|
33
7
|
|
34
8
|
class << self
|
@@ -106,37 +80,39 @@ module Net
|
|
106
80
|
end
|
107
81
|
end
|
108
82
|
|
109
|
-
|
110
|
-
|
83
|
+
class << self
|
84
|
+
def no_proxy? (host, options)
|
85
|
+
return false unless options[:no_proxy].kind_of?(Array)
|
111
86
|
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
87
|
+
host = host.downcase
|
88
|
+
options[:no_proxy].each do |pattern|
|
89
|
+
pattern = pattern.downcase
|
90
|
+
return true if host[-pattern.length, pattern.length] == pattern
|
91
|
+
end
|
117
92
|
|
118
|
-
|
119
|
-
|
93
|
+
return false
|
94
|
+
end
|
120
95
|
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
96
|
+
# Set the options for a global configuration used for all HTTP requests. The global configuration can be cleared
|
97
|
+
# by setting nil
|
98
|
+
def set_global (options)
|
99
|
+
if options
|
100
|
+
@global = Configuration.new(options)
|
101
|
+
else
|
102
|
+
@global = nil
|
103
|
+
end
|
128
104
|
end
|
129
|
-
end
|
130
105
|
|
131
|
-
|
132
|
-
|
133
|
-
|
106
|
+
def global
|
107
|
+
@global
|
108
|
+
end
|
134
109
|
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
110
|
+
# Get the current configuration that is in scope.
|
111
|
+
def current
|
112
|
+
stack = Thread.current[:net_http_configuration]
|
113
|
+
config = stack.last if stack
|
114
|
+
config || global
|
115
|
+
end
|
140
116
|
end
|
141
117
|
|
142
118
|
private
|
@@ -2,7 +2,7 @@ require 'rubygems'
|
|
2
2
|
require 'spec'
|
3
3
|
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'http_configuration'))
|
4
4
|
|
5
|
-
describe
|
5
|
+
describe Net::HTTP::Configuration do
|
6
6
|
|
7
7
|
it "should set the default configuration options" do
|
8
8
|
config = Net::HTTP::Configuration.new(:proxy_host => 'localhost', :proxy_port => 8080, :no_proxy => ['local1', 'local2'])
|
@@ -145,25 +145,3 @@ describe "Net::HTTP" do
|
|
145
145
|
end
|
146
146
|
|
147
147
|
end
|
148
|
-
|
149
|
-
describe "Net::BufferedIO" do
|
150
|
-
|
151
|
-
it "should timeout without an interrupt" do
|
152
|
-
io = Net::BufferedIO.new(StringIO.new)
|
153
|
-
lambda do
|
154
|
-
io.send(:timeout, 1){sleep(2)}
|
155
|
-
end.should raise_error(Net::NetworkTimeoutError)
|
156
|
-
end
|
157
|
-
|
158
|
-
end
|
159
|
-
|
160
|
-
describe "Net::Protocol" do
|
161
|
-
|
162
|
-
it "should timeout without an interrupt" do
|
163
|
-
http = Net::HTTP.new('localhost')
|
164
|
-
lambda do
|
165
|
-
http.send(:timeout, 1){sleep(2)}
|
166
|
-
end.should raise_error(Net::NetworkTimeoutError)
|
167
|
-
end
|
168
|
-
|
169
|
-
end
|
metadata
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: http_configuration
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 17
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- 3
|
10
|
+
version: 1.0.3
|
5
11
|
platform: ruby
|
6
12
|
authors:
|
7
13
|
- Brian Durand
|
@@ -9,54 +15,91 @@ autorequire:
|
|
9
15
|
bindir: bin
|
10
16
|
cert_chain: []
|
11
17
|
|
12
|
-
date:
|
18
|
+
date: 2010-06-22 00:00:00 -05:00
|
13
19
|
default_executable:
|
14
|
-
dependencies:
|
15
|
-
|
16
|
-
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rspec
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 27
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 3
|
33
|
+
- 0
|
34
|
+
version: 1.3.0
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: jeweler
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 3
|
46
|
+
segments:
|
47
|
+
- 0
|
48
|
+
version: "0"
|
49
|
+
type: :development
|
50
|
+
version_requirements: *id002
|
51
|
+
description: Gem that provides the ability to set defaults for proxies and timeouts for Net::HTTP. Simplifies integration of HTTP calls into any environment and provides a unified interface for setting values.
|
17
52
|
email: brian@embellishedvisions.com
|
18
53
|
executables: []
|
19
54
|
|
20
55
|
extensions: []
|
21
56
|
|
22
57
|
extra_rdoc_files:
|
23
|
-
- README
|
58
|
+
- README.rodc
|
24
59
|
files:
|
25
|
-
-
|
26
|
-
- init.rb
|
60
|
+
- .gitignore
|
27
61
|
- MIT-LICENSE
|
62
|
+
- README.rodc
|
28
63
|
- Rakefile
|
29
|
-
-
|
64
|
+
- VERSION
|
65
|
+
- http_configuration.gemspec
|
66
|
+
- lib/http_configuration.rb
|
67
|
+
- spec/http_configuration_spec.rb
|
30
68
|
has_rdoc: true
|
31
|
-
homepage: http://
|
69
|
+
homepage: http://github.com/bdurand/acts_as_revisionable
|
70
|
+
licenses: []
|
71
|
+
|
32
72
|
post_install_message:
|
33
73
|
rdoc_options:
|
34
|
-
- --
|
35
|
-
- HTTP Configuration
|
36
|
-
- --line-numbers
|
37
|
-
- --inline-source
|
74
|
+
- --charset=UTF-8
|
38
75
|
- --main
|
39
|
-
- README
|
76
|
+
- README.rdoc
|
40
77
|
require_paths:
|
41
78
|
- lib
|
42
79
|
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
none: false
|
43
81
|
requirements:
|
44
82
|
- - ">="
|
45
83
|
- !ruby/object:Gem::Version
|
84
|
+
hash: 3
|
85
|
+
segments:
|
86
|
+
- 0
|
46
87
|
version: "0"
|
47
|
-
version:
|
48
88
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
49
90
|
requirements:
|
50
91
|
- - ">="
|
51
92
|
- !ruby/object:Gem::Version
|
93
|
+
hash: 3
|
94
|
+
segments:
|
95
|
+
- 0
|
52
96
|
version: "0"
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
rubygems_version: 1.0.1
|
97
|
+
requirements: []
|
98
|
+
|
99
|
+
rubyforge_project:
|
100
|
+
rubygems_version: 1.3.7
|
58
101
|
signing_key:
|
59
|
-
specification_version:
|
60
|
-
summary:
|
102
|
+
specification_version: 3
|
103
|
+
summary: Gem that provides the ability to set defaults for proxies and timeouts for Net::HTTP.
|
61
104
|
test_files:
|
62
105
|
- spec/http_configuration_spec.rb
|
data/init.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
require 'http_configuration'
|