ingamer_rhc 1.2.5 → 1.2.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/History.txt ADDED
@@ -0,0 +1,62 @@
1
+ == 0.0.1 2007-05-15
2
+ * 1 major enhancement:
3
+ * Initial release
4
+
5
+ == 0.1.2 2007-06-27
6
+
7
+ * No major changes.
8
+
9
+ == 0.1.3 2007-07-09
10
+
11
+ * No change.
12
+
13
+ == 0.1.4 2007-08-10
14
+
15
+ * r1442, todd, 2007-08-07 15:45:24
16
+ * # 373, Add support in right_http_connection for bailing out to a block while
17
+ reading the HTTP response (to support GET streaming...)
18
+
19
+ * r1411, todd, 2007-08-03 15:14:45
20
+ * # 373, Stream uploads (PUTs) if the source is a file, stream, or anything
21
+ read()-able
22
+
23
+ == 1.1.0 2007-08-15
24
+ Initial public release
25
+
26
+ == 1.2.0 2007-10-05
27
+
28
+ * r1867, konstantin, 2007-10-05 06:19:45
29
+ * # 220, (re)open connection to server if none exists or connection params
30
+ have changed
31
+
32
+ == 1.2.1
33
+
34
+ * r2648, konstantin, 01-24-08 11:12:00
35
+ * net_fix.rb moved from right_aws gem to fix the problem with uploading the streamable
36
+ objects to S3
37
+
38
+ * r2764, konstantin, 02-08-08 00:05:00 +03:00
39
+ * "RightAws: incompatible Net::HTTP monkey-patch" exception is raised if our net_fix
40
+ patch was overriden (by attachment_fu for example, to avoid this load attachment_fu
41
+ before loading the right_http_connection gem).
42
+
43
+ == 1.2.2
44
+
45
+ * r3524, konstantin, 2008-04-17 11:35:42 +0400
46
+ * Fixed a problem with incorrect error handling (connection retries always failed).
47
+
48
+ == 1.2.3
49
+
50
+ - Added support for setting retry & timeout parameters in the constructor
51
+ - Improve handling of data streams during upload: if there is a failure and a retry, reset
52
+ the seek pointer for the subsequent re-request
53
+
54
+ == 1.2.4
55
+
56
+ * r4984, konstantin, 2008-08-11 14:49:18 +0400
57
+ * fixed a bug: <NoMethodError: You have a nil object when you didn't expect it!
58
+ The error occurred while evaluating nil.body_stream>
59
+
60
+ == 1.2.5 (not released yet)
61
+
62
+ - ActiveSupport dependency removal
data/Manifest.txt ADDED
@@ -0,0 +1,8 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ lib/net_fix.rb
6
+ lib/right_http_connection.rb
7
+ lib/support.rb
8
+ setup.rb
data/README.txt ADDED
@@ -0,0 +1,54 @@
1
+ RightScale::HttpConnection
2
+ by RightScale, Inc.
3
+ www.RightScale.com
4
+
5
+ == DESCRIPTION:
6
+
7
+ Rightscale::HttpConnection is a robust HTTP/S library. It implements a retry
8
+ algorithm for low-level network errors.
9
+
10
+ == FEATURES:
11
+
12
+ - provides put/get streaming
13
+ - does configurable retries on connect and read timeouts, DNS failures, etc.
14
+ - HTTPS certificate checking
15
+
16
+ == SYNOPSIS:
17
+
18
+
19
+ == REQUIREMENTS:
20
+
21
+ - 2/11/08: If you use RightScale::HttpConnection in conjunction with attachment_fu, the
22
+ HttpConnection gem must be included (using the require statement) AFTER
23
+ attachment_fu.
24
+ This is due to a conflict between the HttpConnection gem and another
25
+ gem required by attachment_fu.
26
+
27
+
28
+
29
+ == INSTALL:
30
+
31
+ sudo gem install right_http_connection
32
+
33
+ == LICENSE:
34
+
35
+ Copyright (c) 2007-2008 RightScale, Inc.
36
+
37
+ Permission is hereby granted, free of charge, to any person obtaining
38
+ a copy of this software and associated documentation files (the
39
+ 'Software'), to deal in the Software without restriction, including
40
+ without limitation the rights to use, copy, modify, merge, publish,
41
+ distribute, sublicense, and/or sell copies of the Software, and to
42
+ permit persons to whom the Software is furnished to do so, subject to
43
+ the following conditions:
44
+
45
+ The above copyright notice and this permission notice shall be
46
+ included in all copies or substantial portions of the Software.
47
+
48
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
49
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
50
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
51
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
52
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
53
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
54
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,103 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/clean'
4
+ require 'rake/testtask'
5
+ require 'rake/packagetask'
6
+ require 'rake/gempackagetask'
7
+ require 'rake/rdoctask'
8
+ require 'rake/contrib/rubyforgepublisher'
9
+ require 'fileutils'
10
+ require 'hoe'
11
+ include FileUtils
12
+ require File.join(File.dirname(__FILE__), 'lib', 'right_http_connection')
13
+
14
+ AUTHOR = 'RightScale' # can also be an array of Authors
15
+ EMAIL = "rubygems@rightscale.com"
16
+ DESCRIPTION = "RightScale's robust HTTP/S connection module"
17
+ GEM_NAME = 'right_http_connection' # what ppl will type to install your gem
18
+ RUBYFORGE_PROJECT = 'rightscale' # The unix name for your project
19
+ HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
20
+ DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
21
+
22
+ NAME = "right_http_connection"
23
+ REV = nil # UNCOMMENT IF REQUIRED: File.read(".svn/entries")[/committed-rev="(d+)"/, 1] rescue nil
24
+ VERS = RightHttpConnection::VERSION::STRING + (REV ? ".#{REV}" : "")
25
+ CLEAN.include ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store']
26
+ RDOC_OPTS = ['--quiet', '--title', 'right_http_connection documentation',
27
+ "--opname", "index.html",
28
+ "--line-numbers",
29
+ "--main", "README",
30
+ "--inline-source"]
31
+
32
+ # Suppress Hoe's self-inclusion as a dependency for our Gem. This also keeps
33
+ # Rake & rubyforge out of the dependency list. Users must manually install
34
+ # these gems to run tests, etc.
35
+ # TRB 2/19/09: also do this for the extra_dev_deps array present in newer hoes.
36
+ # Older versions of RubyGems will try to install developer-dependencies as
37
+ # required runtime dependencies....
38
+ class Hoe
39
+ def extra_deps
40
+ @extra_deps.reject do |x|
41
+ Array(x).first == 'hoe'
42
+ end
43
+ end
44
+ def extra_dev_deps
45
+ @extra_dev_deps.reject do |x|
46
+ Array(x).first == 'hoe'
47
+ end
48
+ end
49
+ end
50
+
51
+ # Generate all the Rake tasks
52
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
53
+ hoe = Hoe.new(GEM_NAME, VERS) do |p|
54
+ p.author = AUTHOR
55
+ p.description = DESCRIPTION
56
+ p.email = EMAIL
57
+ p.summary = DESCRIPTION
58
+ p.url = HOMEPATH
59
+ p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
60
+ p.test_globs = ["test/**/test_*.rb"]
61
+ p.clean_globs = CLEAN #An array of file patterns to delete on clean.
62
+ p.remote_rdoc_dir = "right_http_gem_doc"
63
+
64
+ # == Optional
65
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
66
+ #p.extra_deps = [] # An array of rubygem dependencies [name, version], e.g. [ ['active_support', '>= 1.3.1'] ]
67
+ #p.spec_extras = {} # A hash of extra values to set in the gemspec.
68
+ end
69
+
70
+
71
+ desc 'Generate website files'
72
+ task :website_generate do
73
+ Dir['website/**/*.txt'].each do |txt|
74
+ sh %{ ruby scripts/txt2html #{txt} > #{txt.gsub(/txt$/,'html')} }
75
+ end
76
+ end
77
+
78
+ desc 'Upload website files to rubyforge'
79
+ task :website_upload do
80
+ config = YAML.load(File.read(File.expand_path("~/.rubyforge/user-config.yml")))
81
+ host = "#{config["username"]}@rubyforge.org"
82
+ remote_dir = "/var/www/gforge-projects/#{RUBYFORGE_PROJECT}/"
83
+ # remote_dir = "/var/www/gforge-projects/#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
84
+ local_dir = 'website'
85
+ sh %{rsync -av #{local_dir}/ #{host}:#{remote_dir}}
86
+ end
87
+
88
+ desc 'Generate and upload website files'
89
+ task :website => [:website_generate, :website_upload]
90
+
91
+ desc 'Release the website and new gem version'
92
+ task :deploy => [:check_version, :website, :release]
93
+
94
+ task :check_version do
95
+ unless ENV['VERSION']
96
+ puts 'Must pass a VERSION=x.y.z release version'
97
+ exit
98
+ end
99
+ unless ENV['VERSION'] == VERS
100
+ puts "Please update your version.rb to match the release version, currently #{VERS}"
101
+ exit
102
+ end
103
+ end
data/lib/net_fix.rb ADDED
@@ -0,0 +1,160 @@
1
+ #
2
+ # Copyright (c) 2008 RightScale Inc
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #
23
+ #
24
+
25
+ # Net::HTTP and Net::HTTPGenericRequest fixes to support 100-continue on
26
+ # POST and PUT. The request must have 'expect' field set to '100-continue'.
27
+
28
+
29
+ module Net
30
+
31
+ class BufferedIO #:nodoc:
32
+ # Monkey-patch Net::BufferedIO to read > 1024 bytes from the socket at a time
33
+
34
+ # Default size (in bytes) of the max read from a socket into the user space read buffers for socket IO
35
+ DEFAULT_SOCKET_READ_SIZE = 16*1024
36
+
37
+ @@socket_read_size = DEFAULT_SOCKET_READ_SIZE
38
+
39
+ def self.socket_read_size=(readsize)
40
+ if(readsize <= 0)
41
+ return
42
+ end
43
+ @@socket_read_size = readsize
44
+ end
45
+
46
+ def self.socket_read_size?()
47
+ @@socket_read_size
48
+ end
49
+
50
+ def rbuf_fill
51
+ timeout(@read_timeout) {
52
+ @rbuf << @io.sysread(@@socket_read_size)
53
+ }
54
+ end
55
+ end
56
+
57
+
58
+ #-- Net::HTTPGenericRequest --
59
+
60
+ class HTTPGenericRequest
61
+ # Monkey-patch Net::HTTPGenericRequest to read > 1024 bytes from the local data
62
+ # source at a time (used in streaming PUTs)
63
+
64
+ # Default size (in bytes) of the max read from a local source (File, String,
65
+ # etc.) to the user space write buffers for socket IO.
66
+ DEFAULT_LOCAL_READ_SIZE = 16*1024
67
+
68
+ @@local_read_size = DEFAULT_LOCAL_READ_SIZE
69
+
70
+ def self.local_read_size=(readsize)
71
+ if(readsize <= 0)
72
+ return
73
+ end
74
+ @@local_read_size = readsize
75
+ end
76
+
77
+ def self.local_read_size?()
78
+ @@local_read_size
79
+ end
80
+
81
+ def exec(sock, ver, path, send_only=nil) #:nodoc: internal use only
82
+ if @body
83
+ send_request_with_body sock, ver, path, @body, send_only
84
+ elsif @body_stream
85
+ send_request_with_body_stream sock, ver, path, @body_stream, send_only
86
+ else
87
+ write_header(sock, ver, path)
88
+ end
89
+ end
90
+
91
+ private
92
+
93
+ def send_request_with_body(sock, ver, path, body, send_only=nil)
94
+ self.content_length = body.length
95
+ delete 'Transfer-Encoding'
96
+ supply_default_content_type
97
+ write_header(sock, ver, path) unless send_only == :body
98
+ sock.write(body) unless send_only == :header
99
+ end
100
+
101
+ def send_request_with_body_stream(sock, ver, path, f, send_only=nil)
102
+ unless content_length() or chunked?
103
+ raise ArgumentError,
104
+ "Content-Length not given and Transfer-Encoding is not `chunked'"
105
+ end
106
+ supply_default_content_type
107
+ write_header(sock, ver, path) unless send_only == :body
108
+ unless send_only == :header
109
+ if chunked?
110
+ while s = f.read(@@local_read_size)
111
+ sock.write(sprintf("%x\r\n", s.length) << s << "\r\n")
112
+ end
113
+ sock.write "0\r\n\r\n"
114
+ else
115
+ while s = f.read(@@local_read_size)
116
+ sock.write s
117
+ end
118
+ end
119
+ end
120
+ end
121
+ end
122
+
123
+
124
+ #-- Net::HTTP --
125
+
126
+ class HTTP
127
+ def request(req, body = nil, &block) # :yield: +response+
128
+ unless started?
129
+ start {
130
+ req['connection'] ||= 'close'
131
+ return request(req, body, &block)
132
+ }
133
+ end
134
+ if proxy_user()
135
+ unless use_ssl?
136
+ req.proxy_basic_auth proxy_user(), proxy_pass()
137
+ end
138
+ end
139
+ # set body
140
+ req.set_body_internal body
141
+ begin_transport req
142
+ # if we expect 100-continue then send a header first
143
+ send_only = ((req.is_a?(Post)||req.is_a?(Put)) && (req['expect']=='100-continue')) ? :header : nil
144
+ req.exec @socket, @curr_http_version, edit_path(req.path), send_only
145
+ begin
146
+ res = HTTPResponse.read_new(@socket)
147
+ # if we expected 100-continue then send a body
148
+ if res.is_a?(HTTPContinue) && send_only && req['content-length'].to_i > 0
149
+ req.exec @socket, @curr_http_version, edit_path(req.path), :body
150
+ end
151
+ end while res.kind_of?(HTTPContinue)
152
+ res.reading_body(@socket, req.response_body_permitted?) {
153
+ yield res if block_given?
154
+ }
155
+ end_transport req, res
156
+ res
157
+ end
158
+ end
159
+
160
+ end
@@ -0,0 +1,461 @@
1
+ #
2
+ # Copyright (c) 2007-2008 RightScale Inc
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #
23
+
24
+ require "net/https"
25
+ require "uri"
26
+ require "time"
27
+ require "logger"
28
+
29
+ $:.unshift(File.dirname(__FILE__))
30
+ require 'support'
31
+ require "net_fix"
32
+
33
+
34
+ module RightHttpConnection #:nodoc:
35
+ module VERSION #:nodoc:
36
+ MAJOR = 1 unless defined?(MAJOR)
37
+ MINOR = 2 unless defined?(MINOR)
38
+ TINY = 5 unless defined?(TINY)
39
+
40
+ STRING = [MAJOR, MINOR, TINY].join('.') unless defined?(STRING)
41
+ end
42
+ end
43
+
44
+
45
+ module Rightscale
46
+
47
+ =begin rdoc
48
+ HttpConnection maintains a persistent HTTP connection to a remote
49
+ server. Each instance maintains its own unique connection to the
50
+ HTTP server. HttpConnection makes a best effort to receive a proper
51
+ HTTP response from the server, although it does not guarantee that
52
+ this response contains a HTTP Success code.
53
+
54
+ On low-level errors (TCP/IP errors) HttpConnection invokes a reconnect
55
+ and retry algorithm. Note that although each HttpConnection object
56
+ has its own connection to the HTTP server, error handling is shared
57
+ across all connections to a server. For example, if there are three
58
+ connections to www.somehttpserver.com, a timeout error on one of those
59
+ connections will cause all three connections to break and reconnect.
60
+ A connection will not break and reconnect, however, unless a request
61
+ becomes active on it within a certain amount of time after the error
62
+ (as specified by HTTP_CONNECTION_RETRY_DELAY). An idle connection will not
63
+ break even if other connections to the same server experience errors.
64
+
65
+ A HttpConnection will retry a request a certain number of times (as
66
+ defined by HTTP_CONNNECTION_RETRY_COUNT). If all the retries fail,
67
+ an exception is thrown and all HttpConnections associated with a
68
+ server enter a probationary period defined by HTTP_CONNECTION_RETRY_DELAY.
69
+ If the user makes a new request subsequent to entering probation,
70
+ the request will fail immediately with the same exception thrown
71
+ on probation entry. This is so that if the HTTP server has gone
72
+ down, not every subsequent request must wait for a connect timeout
73
+ before failing. After the probation period expires, the internal
74
+ state of the HttpConnection is reset and subsequent requests have
75
+ the full number of potential reconnects and retries available to
76
+ them.
77
+ =end
78
+
79
+ class HttpConnection
80
+
81
+ # Number of times to retry the request after encountering the first error
82
+ HTTP_CONNECTION_RETRY_COUNT = 3 unless defined?(HTTP_CONNECTION_RETRY_COUNT)
83
+ # Throw a Timeout::Error if a connection isn't established within this number of seconds
84
+ HTTP_CONNECTION_OPEN_TIMEOUT = 5 unless defined?(HTTP_CONNECTION_OPEN_TIMEOUT)
85
+ # Throw a Timeout::Error if no data have been read on this connnection within this number of seconds
86
+ HTTP_CONNECTION_READ_TIMEOUT = 120 unless defined?(HTTP_CONNECTION_READ_TIMEOUT)
87
+ # Length of the post-error probationary period during which all requests will fail
88
+ HTTP_CONNECTION_RETRY_DELAY = 15 unless defined?(HTTP_CONNECTION_RETRY_DELAY)
89
+
90
+ #--------------------
91
+ # class methods
92
+ #--------------------
93
+ #
94
+ @@params = {}
95
+ @@params[:http_connection_retry_count] = HTTP_CONNECTION_RETRY_COUNT
96
+ @@params[:http_connection_open_timeout] = HTTP_CONNECTION_OPEN_TIMEOUT
97
+ @@params[:http_connection_read_timeout] = HTTP_CONNECTION_READ_TIMEOUT
98
+ @@params[:http_connection_retry_delay] = HTTP_CONNECTION_RETRY_DELAY
99
+
100
+ # Query the global (class-level) parameters:
101
+ #
102
+ # :user_agent => 'www.HostName.com' # String to report as HTTP User agent
103
+ # :ca_file => 'path_to_file' # Path to a CA certification file in PEM format. The file can contain several CA certificates. If this parameter isn't set, HTTPS certs won't be verified.
104
+ # :logger => Logger object # If omitted, HttpConnection logs to STDOUT
105
+ # :exception => Exception to raise # The type of exception to raise
106
+ # # if a request repeatedly fails. RuntimeError is raised if this parameter is omitted.
107
+ # :http_connection_retry_count # by default == Rightscale::HttpConnection::HTTP_CONNECTION_RETRY_COUNT
108
+ # :http_connection_open_timeout # by default == Rightscale::HttpConnection::HTTP_CONNECTION_OPEN_TIMEOUT
109
+ # :http_connection_read_timeout # by default == Rightscale::HttpConnection::HTTP_CONNECTION_READ_TIMEOUT
110
+ # :http_connection_retry_delay # by default == Rightscale::HttpConnection::HTTP_CONNECTION_RETRY_DELAY
111
+ # :raise_on_timeout # do not perform a retry if timeout is received (false by default)
112
+ def self.params
113
+ @@params
114
+ end
115
+
116
+ # Set the global (class-level) parameters
117
+ def self.params=(params)
118
+ @@params = params
119
+ end
120
+
121
+ #------------------
122
+ # instance methods
123
+ #------------------
124
+ attr_accessor :http
125
+ attr_accessor :server
126
+ attr_accessor :params # see @@params
127
+ attr_accessor :logger
128
+
129
+ # Params hash:
130
+ # :user_agent => 'www.HostName.com' # String to report as HTTP User agent
131
+ # :ca_file => 'path_to_file' # A path of a CA certification file in PEM format. The file can contain several CA certificates.
132
+ # :logger => Logger object # If omitted, HttpConnection logs to STDOUT
133
+ # :exception => Exception to raise # The type of exception to raise if a request repeatedly fails. RuntimeError is raised if this parameter is omitted.
134
+ # :http_connection_retry_count # by default == Rightscale::HttpConnection.params[:http_connection_retry_count]
135
+ # :http_connection_open_timeout # by default == Rightscale::HttpConnection.params[:http_connection_open_timeout]
136
+ # :http_connection_read_timeout # by default == Rightscale::HttpConnection.params[:http_connection_read_timeout]
137
+ # :http_connection_retry_delay # by default == Rightscale::HttpConnection.params[:http_connection_retry_delay]
138
+ # :raise_on_timeout # do not perform a retry if timeout is received (false by default)
139
+ def initialize(params={})
140
+ @params = params
141
+ @params[:http_connection_retry_count] ||= @@params[:http_connection_retry_count]
142
+ @params[:http_connection_open_timeout] ||= @@params[:http_connection_open_timeout]
143
+ @params[:http_connection_read_timeout] ||= @@params[:http_connection_read_timeout]
144
+ @params[:http_connection_retry_delay] ||= @@params[:http_connection_retry_delay]
145
+ @http = nil
146
+ @server = nil
147
+ @logger = get_param(:logger) ||
148
+ (RAILS_DEFAULT_LOGGER if defined?(RAILS_DEFAULT_LOGGER)) ||
149
+ Logger.new(STDOUT)
150
+ #--------------
151
+ # Retry state - Keep track of errors on a per-server basis
152
+ #--------------
153
+ @state = {} # retry state indexed by server: consecutive error count, error time, and error
154
+ @eof = {}
155
+ end
156
+
157
+ def get_param(name, custom_options={})
158
+ custom_options [name] || @params[name] || @@params[name]
159
+ end
160
+
161
+ # Query for the maximum size (in bytes) of a single read from the underlying
162
+ # socket. For bulk transfer, especially over fast links, this is value is
163
+ # critical to performance.
164
+ def socket_read_size?
165
+ Net::BufferedIO.socket_read_size?
166
+ end
167
+
168
+ # Set the maximum size (in bytes) of a single read from the underlying
169
+ # socket. For bulk transfer, especially over fast links, this is value is
170
+ # critical to performance.
171
+ def socket_read_size=(newsize)
172
+ Net::BufferedIO.socket_read_size=(newsize)
173
+ end
174
+
175
+ # Query for the maximum size (in bytes) of a single read from local data
176
+ # sources like files. This is important, for example, in a streaming PUT of a
177
+ # large buffer.
178
+ def local_read_size?
179
+ Net::HTTPGenericRequest.local_read_size?
180
+ end
181
+
182
+ # Set the maximum size (in bytes) of a single read from local data
183
+ # sources like files. This can be used to tune the performance of, for example, a streaming PUT of a
184
+ # large buffer.
185
+ def local_read_size=(newsize)
186
+ Net::HTTPGenericRequest.local_read_size=(newsize)
187
+ end
188
+
189
+ private
190
+
191
+ # number of consecutive errors seen for server, 0 all is ok
192
+ def error_count
193
+ @state[@server] ? @state[@server][:count] : 0
194
+ end
195
+
196
+ # time of last error for server, nil if all is ok
197
+ def error_time
198
+ @state[@server] && @state[@server][:time]
199
+ end
200
+
201
+ # message for last error for server, "" if all is ok
202
+ def error_message
203
+ @state[@server] ? @state[@server][:message] : ""
204
+ end
205
+
206
+ # add an error for a server
207
+ def error_add(message)
208
+ @state[@server] = { :count => error_count+1, :time => Time.now, :message => message }
209
+ end
210
+
211
+ # reset the error state for a server (i.e. a request succeeded)
212
+ def error_reset
213
+ @state.delete(@server)
214
+ end
215
+
216
+ # Error message stuff...
217
+ def banana_message
218
+ return "#{@server} temporarily unavailable: (#{error_message})"
219
+ end
220
+
221
+ def err_header
222
+ return "#{self.class.name} :"
223
+ end
224
+
225
+ # Adds new EOF timestamp.
226
+ # Returns the number of seconds to wait before new conection retry:
227
+ # 0.5, 1, 2, 4, 8
228
+ def add_eof
229
+ (@eof[@server] ||= []).unshift Time.now
230
+ 0.25 * 2 ** @eof[@server].size
231
+ end
232
+
233
+ # Returns first EOF timestamp or nul if have no EOFs being tracked.
234
+ def eof_time
235
+ @eof[@server] && @eof[@server].last
236
+ end
237
+
238
+ # Returns true if we are receiving EOFs during last @params[:http_connection_retry_delay] seconds
239
+ # and there were no successful response from server
240
+ def raise_on_eof_exception?
241
+ @eof[@server].nil? ? false : ( (Time.now.to_i-@params[:http_connection_retry_delay]) > @eof[@server].last.to_i )
242
+ end
243
+
244
+ # Reset a list of EOFs for this server.
245
+ # This is being called when we have got an successful response from server.
246
+ def eof_reset
247
+ @eof.delete(@server)
248
+ end
249
+
250
+ # Detects if an object is 'streamable' - can we read from it, and can we know the size?
251
+ def setup_streaming(request)
252
+ if(request.body && request.body.respond_to?(:read))
253
+ body = request.body
254
+ request.content_length = body.respond_to?(:lstat) ? body.lstat.size : body.size
255
+ request.body_stream = request.body
256
+ true
257
+ end
258
+ end
259
+
260
+ def get_fileptr_offset(request_params)
261
+ request_params[:request].body.pos
262
+ rescue Exception => e
263
+ # Probably caught this because the body doesn't support the pos() method, like if it is a socket.
264
+ # Just return 0 and get on with life.
265
+ 0
266
+ end
267
+
268
+ def reset_fileptr_offset(request, offset = 0)
269
+ if(request.body_stream && request.body_stream.respond_to?(:pos))
270
+ begin
271
+ request.body_stream.pos = offset
272
+ rescue Exception => e
273
+ @logger.warn("Failed file pointer reset; aborting HTTP retries." +
274
+ " -- #{err_header} #{e.inspect}")
275
+ raise e
276
+ end
277
+ end
278
+ end
279
+
280
+ # Start a fresh connection. The object closes any existing connection and
281
+ # opens a new one.
282
+ def start(request_params)
283
+ # close the previous if exists
284
+ finish
285
+ # create new connection
286
+ @server = request_params[:server]
287
+ @port = request_params[:port]
288
+ @protocol = request_params[:protocol]
289
+
290
+ @logger.info("Opening new #{@protocol.upcase} connection to #@server:#@port")
291
+
292
+ @http = Net::HTTP.new(@server, @port)
293
+ @http.open_timeout = get_param(:http_connection_open_timeout, request_params)
294
+ @http.read_timeout = get_param(:http_connection_read_timeout, request_params)
295
+
296
+ if @protocol == 'https'
297
+ verifyCallbackProc = Proc.new{ |ok, x509_store_ctx|
298
+ code = x509_store_ctx.error
299
+ msg = x509_store_ctx.error_string
300
+ #debugger
301
+ @logger.warn("##### #{@server} certificate verify failed: #{msg}") unless code == 0
302
+ true
303
+ }
304
+ @http.use_ssl = true
305
+ ca_file = get_param(:ca_file)
306
+ if ca_file
307
+ @http.verify_mode = OpenSSL::SSL::VERIFY_PEER
308
+ @http.verify_callback = verifyCallbackProc
309
+ @http.ca_file = ca_file
310
+ else
311
+ @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
312
+ end
313
+ end
314
+ # open connection
315
+ @http.start
316
+ end
317
+
318
+ public
319
+
320
+ =begin rdoc
321
+ Send HTTP request to server
322
+
323
+ request_params hash:
324
+ :server => 'www.HostName.com' # Hostname or IP address of HTTP server
325
+ :port => '80' # Port of HTTP server
326
+ :protocol => 'https' # http and https are supported on any port
327
+ :request => 'requeststring' # Fully-formed HTTP request to make
328
+
329
+ :raise_on_timeout # do not perform a retry if timeout is received (false by default)
330
+ :http_connection_retry_count
331
+ :http_connection_open_timeout
332
+ :http_connection_read_timeout
333
+ :http_connection_retry_delay
334
+ :user_agent
335
+ :exception
336
+
337
+ Raises RuntimeError, Interrupt, and params[:exception] (if specified in new).
338
+
339
+ =end
340
+ def request(request_params, &block)
341
+ current_params = @params.merge(request_params)
342
+ exception = get_param(:exception, current_params) || RuntimeError
343
+
344
+ # We save the offset here so that if we need to retry, we can return the file pointer to its initial position
345
+ mypos = get_fileptr_offset(current_params)
346
+ loop do
347
+ current_params[:protocol] ||= (current_params[:port] == 443 ? 'https' : 'http')
348
+ # (re)open connection to server if none exists or params has changed
349
+ same_server_as_before = @server == current_params[:server] &&
350
+ @port == current_params[:port] &&
351
+ @protocol == current_params[:protocol]
352
+
353
+ # if we are inside a delay between retries: no requests this time!
354
+ # (skip this step if the endpoint has changed)
355
+ if error_count > current_params[:http_connection_retry_count] &&
356
+ error_time + current_params[:http_connection_retry_delay] > Time.now &&
357
+ same_server_as_before
358
+
359
+ # store the message (otherwise it will be lost after error_reset and
360
+ # we will raise an exception with an empty text)
361
+ banana_message_text = banana_message
362
+ @logger.warn("#{err_header} re-raising same error: #{banana_message_text} " +
363
+ "-- error count: #{error_count}, error age: #{Time.now.to_i - error_time.to_i}")
364
+ raise exception.new(banana_message_text)
365
+ end
366
+
367
+ # try to connect server(if connection does not exist) and get response data
368
+ begin
369
+ request = current_params[:request]
370
+ request['User-Agent'] = get_param(:user_agent, current_params) || ''
371
+ unless @http &&
372
+ @http.started? &&
373
+ same_server_as_before
374
+ start(current_params)
375
+ end
376
+
377
+ # Detect if the body is a streamable object like a file or socket. If so, stream that
378
+ # bad boy.
379
+ setup_streaming(request)
380
+ # update READ_TIMEOUT value (it can be passed with request_params hash)
381
+ @http.read_timeout = get_param(:http_connection_read_timeout, current_params)
382
+ response = @http.request(request, &block)
383
+
384
+ error_reset
385
+ eof_reset
386
+ return response
387
+
388
+ # We treat EOF errors and the timeout/network errors differently. Both
389
+ # are tracked in different statistics blocks. Note below that EOF
390
+ # errors will sleep for a certain (exponentially increasing) period.
391
+ # Other errors don't sleep because there is already an inherent delay
392
+ # in them; connect and read timeouts (for example) have already
393
+ # 'slept'. It is still not clear which way we should treat errors
394
+ # like RST and resolution failures. For now, there is no additional
395
+ # delay for these errors although this may change in the future.
396
+
397
+ # EOFError means the server closed the connection on us.
398
+ rescue EOFError => e
399
+ @logger.debug("#{err_header} server #{@server} closed connection")
400
+ @http = nil
401
+
402
+ # if we have waited long enough - raise an exception...
403
+ if raise_on_eof_exception?
404
+ @logger.warn("#{err_header} raising #{exception} due to permanent EOF being received from #{@server}, error age: #{Time.now.to_i - eof_time.to_i}")
405
+ raise exception.new("Permanent EOF is being received from #{@server}.")
406
+ else
407
+ # ... else just sleep a bit before new retry
408
+ sleep(add_eof)
409
+ # We will be retrying the request, so reset the file pointer
410
+ reset_fileptr_offset(request, mypos)
411
+ end
412
+ rescue Exception => e # See comment at bottom for the list of errors seen...
413
+ @http = nil
414
+ timeout_exception = e.is_a?(Errno::ETIMEDOUT) || e.is_a?(Timeout::Error)
415
+ # Omit retries if it was explicitly requested
416
+ if current_params[:raise_on_timeout] && timeout_exception
417
+ # #6481:
418
+ # ... When creating a resource in EC2 (instance, volume, snapshot, etc) it is undetermined what happened if the call times out.
419
+ # The resource may or may not have been created in EC2. Retrying the call may cause multiple resources to be created...
420
+ raise e
421
+ end
422
+ # if ctrl+c is pressed - we have to reraise exception to terminate proggy
423
+ if e.is_a?(Interrupt) && !timeout_exception
424
+ @logger.debug( "#{err_header} request to server #{@server} interrupted by ctrl-c")
425
+ raise
426
+ elsif e.is_a?(ArgumentError) && e.message.include?('wrong number of arguments (5 for 4)')
427
+ # seems our net_fix patch was overriden...
428
+ raise exception.new('incompatible Net::HTTP monkey-patch')
429
+ end
430
+ # oops - we got a banana: log it
431
+ error_add(e.message)
432
+ @logger.warn("#{err_header} request failure count: #{error_count}, exception: #{e.inspect}")
433
+
434
+ # We will be retrying the request, so reset the file pointer
435
+ reset_fileptr_offset(request, mypos)
436
+
437
+ end
438
+ end
439
+ end
440
+
441
+ def finish(reason = '')
442
+ if @http && @http.started?
443
+ reason = ", reason: '#{reason}'" unless reason.empty?
444
+ @logger.info("Closing #{@http.use_ssl? ? 'HTTPS' : 'HTTP'} connection to #{@http.address}:#{@http.port}#{reason}")
445
+ @http.finish
446
+ end
447
+ end
448
+
449
+ # Errors received during testing:
450
+ #
451
+ # #<Timeout::Error: execution expired>
452
+ # #<Errno::ETIMEDOUT: Connection timed out - connect(2)>
453
+ # #<SocketError: getaddrinfo: Name or service not known>
454
+ # #<SocketError: getaddrinfo: Temporary failure in name resolution>
455
+ # #<EOFError: end of file reached>
456
+ # #<Errno::ECONNRESET: Connection reset by peer>
457
+ # #<OpenSSL::SSL::SSLError: SSL_write:: bad write retry>
458
+ end
459
+
460
+ end
461
+
data/lib/support.rb ADDED
@@ -0,0 +1,109 @@
1
+ # These are ActiveSupport-;like extensions to do a few handy things in the gems
2
+ # Derived from ActiveSupport, so the AS copyright notice applies:
3
+ #
4
+ #
5
+ #
6
+ # Copyright (c) 2005 David Heinemeier Hansson
7
+ #
8
+ # Permission is hereby granted, free of charge, to any person obtaining
9
+ # a copy of this software and associated documentation files (the
10
+ # "Software"), to deal in the Software without restriction, including
11
+ # without limitation the rights to use, copy, modify, merge, publish,
12
+ # distribute, sublicense, and/or sell copies of the Software, and to
13
+ # permit persons to whom the Software is furnished to do so, subject to
14
+ # the following conditions:
15
+ #
16
+ # The above copyright notice and this permission notice shall be
17
+ # included in all copies or substantial portions of the Software.
18
+ #
19
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26
+ #++
27
+ #
28
+ #
29
+ class String #:nodoc:
30
+
31
+ # Constantize tries to find a declared constant with the name specified
32
+ # in the string. It raises a NameError when the name is not in CamelCase
33
+ # or is not initialized.
34
+ #
35
+ # Examples
36
+ # "Module".constantize #=> Module
37
+ # "Class".constantize #=> Class
38
+ def right_constantize()
39
+ unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ self
40
+ raise NameError, "#{self.inspect} is not a valid constant name!"
41
+ end
42
+ Object.module_eval("::#{$1}", __FILE__, __LINE__)
43
+ end
44
+
45
+ def right_camelize()
46
+ self.dup.split(/_/).map{ |word| word.capitalize }.join('')
47
+ end
48
+
49
+ end
50
+
51
+
52
+ class Object #:nodoc:
53
+ # "", " ", nil, [], and {} are blank
54
+ def right_blank?
55
+ if respond_to?(:empty?) && respond_to?(:strip)
56
+ empty? or strip.empty?
57
+ elsif respond_to?(:empty?)
58
+ empty?
59
+ else
60
+ !self
61
+ end
62
+ end
63
+ end
64
+
65
+ class NilClass #:nodoc:
66
+ def right_blank?
67
+ true
68
+ end
69
+ end
70
+
71
+ class FalseClass #:nodoc:
72
+ def right_blank?
73
+ true
74
+ end
75
+ end
76
+
77
+ class TrueClass #:nodoc:
78
+ def right_blank?
79
+ false
80
+ end
81
+ end
82
+
83
+ class Array #:nodoc:
84
+ alias_method :right_blank?, :empty?
85
+ end
86
+
87
+ class Hash #:nodoc:
88
+ alias_method :right_blank?, :empty?
89
+
90
+ # Return a new hash with all keys converted to symbols.
91
+ def right_symbolize_keys
92
+ inject({}) do |options, (key, value)|
93
+ options[key.to_sym] = value
94
+ options
95
+ end
96
+ end
97
+ end
98
+
99
+ class String #:nodoc:
100
+ def right_blank?
101
+ empty? || strip.empty?
102
+ end
103
+ end
104
+
105
+ class Numeric #:nodoc:
106
+ def right_blank?
107
+ false
108
+ end
109
+ end
metadata CHANGED
@@ -6,7 +6,8 @@ version: !ruby/object:Gem::Version
6
6
  - 1
7
7
  - 2
8
8
  - 5
9
- version: 1.2.5
9
+ - 1
10
+ version: 1.2.5.1
10
11
  platform: ruby
11
12
  authors:
12
13
  - RightScale
@@ -26,8 +27,14 @@ extensions: []
26
27
 
27
28
  extra_rdoc_files: []
28
29
 
29
- files: []
30
-
30
+ files:
31
+ - lib/net_fix.rb
32
+ - lib/right_http_connection.rb
33
+ - lib/support.rb
34
+ - History.txt
35
+ - Manifest.txt
36
+ - Rakefile
37
+ - README.txt
31
38
  has_rdoc: true
32
39
  homepage: http://github.com/rightscale/right_http_connection
33
40
  licenses: []