Skip to content

Commit 1ca5b60

Browse files
committed
defaulting to non-persistent connections..
WARNING: this will break :timeout => 0 stream cases, switch/add :keepalive => true instead :timeout will now serve as intended: you can have a keepalive connection which you can close after N inactivity seconds. setting :timeout to 0 and :keepalive to true will match the previous :timeout => 0 configuration. i.e: keep connection open and don't close it no matter how stale it gets.
1 parent 07ff796 commit 1ca5b60

File tree

3 files changed

+36
-18
lines changed

3 files changed

+36
-18
lines changed

lib/em-http/client.rb

+10-8
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,12 @@ def send_request_header
425425

426426
# Set content-type header if missing and body is a Ruby hash
427427
if not head['content-type'] and options[:body].is_a? Hash
428-
head['content-type'] = "application/x-www-form-urlencoded"
428+
head['content-type'] = 'application/x-www-form-urlencoded'
429+
end
430+
431+
# Set connection close unless keepalive
432+
unless options[:keepalive]
433+
head['connection'] = 'close'
429434
end
430435
end
431436

@@ -852,13 +857,10 @@ def process_body
852857
on_request_complete
853858

854859
else
855-
if @data.empty?
856-
@state = :finished
857-
on_request_complete
858-
else
859-
@state = :invalid
860-
on_error "garbage at end of body"
861-
end
860+
861+
@data.clear
862+
@state = :finished
863+
on_request_complete
862864
end
863865

864866
false

lib/em-http/http_options.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ def initialize(method, uri, options)
1818
@port = uri.port
1919
end
2020

21-
@options[:timeout] ||= 10 # default connect & inactivity timeouts
22-
@options[:redirects] ||= 0 # default number of redirects to follow
21+
@options[:timeout] ||= 10 # default connect & inactivity timeouts
22+
@options[:redirects] ||= 0 # default number of redirects to follow
23+
@options[:keepalive] ||= false # default to single request per connection
2324

2425
# Make sure the ports are set as Addressable::URI doesn't
2526
# set the port if it isn't there

spec/request_spec.rb

+23-8
Original file line numberDiff line numberDiff line change
@@ -361,17 +361,32 @@ def failed(http=nil)
361361
}
362362
end
363363

364-
it "should work with keep-alive servers" do
365-
EventMachine.run {
364+
context "keepalive" do
365+
it "should default to non-keepalive" do
366+
EventMachine.run {
367+
headers = {'If-Modified-Since' => 'Mon, 15 Nov 2010 03:48:30 GMT'}
368+
http = EventMachine::HttpRequest.new('http://www.google.com/images/logos/ps_logo2.png').get :head => headers
366369

367-
http = EventMachine::HttpRequest.new('http://mexicodiario.com/touch.public.json.php').get
370+
http.errback { fail }
371+
start = Time.now.to_i
372+
http.callback {
373+
(start - Time.now.to_i).should be_close(0,1)
374+
EventMachine.stop
375+
}
376+
}
377+
end
368378

369-
http.errback { failed(http) }
370-
http.callback {
371-
http.response_header.status.should == 200
372-
EventMachine.stop
379+
it "should work with keep-alive servers" do
380+
EventMachine.run {
381+
http = EventMachine::HttpRequest.new('http://mexicodiario.com/touch.public.json.php').get :keepalive => true
382+
383+
http.errback { failed(http) }
384+
http.callback {
385+
http.response_header.status.should == 200
386+
EventMachine.stop
387+
}
373388
}
374-
}
389+
end
375390
end
376391

377392
it "should return ETag and Last-Modified headers" do

0 commit comments

Comments
 (0)