Skip to content

Commit

Permalink
defaulting to non-persistent connections..
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
igrigorik committed Nov 27, 2010
1 parent 07ff796 commit 1ca5b60
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 18 deletions.
18 changes: 10 additions & 8 deletions lib/em-http/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,12 @@ def send_request_header

# Set content-type header if missing and body is a Ruby hash
if not head['content-type'] and options[:body].is_a? Hash
head['content-type'] = "application/x-www-form-urlencoded"
head['content-type'] = 'application/x-www-form-urlencoded'
end

# Set connection close unless keepalive
unless options[:keepalive]
head['connection'] = 'close'
end
end

Expand Down Expand Up @@ -852,13 +857,10 @@ def process_body
on_request_complete

else
if @data.empty?
@state = :finished
on_request_complete
else
@state = :invalid
on_error "garbage at end of body"
end

@data.clear
@state = :finished
on_request_complete
end

false
Expand Down
5 changes: 3 additions & 2 deletions lib/em-http/http_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ def initialize(method, uri, options)
@port = uri.port
end

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

# Make sure the ports are set as Addressable::URI doesn't
# set the port if it isn't there
Expand Down
31 changes: 23 additions & 8 deletions spec/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -361,17 +361,32 @@ def failed(http=nil)
}
end

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

http = EventMachine::HttpRequest.new('http://mexicodiario.com/touch.public.json.php').get
http.errback { fail }
start = Time.now.to_i
http.callback {
(start - Time.now.to_i).should be_close(0,1)
EventMachine.stop
}
}
end

http.errback { failed(http) }
http.callback {
http.response_header.status.should == 200
EventMachine.stop
it "should work with keep-alive servers" do
EventMachine.run {
http = EventMachine::HttpRequest.new('http://mexicodiario.com/touch.public.json.php').get :keepalive => true

http.errback { failed(http) }
http.callback {
http.response_header.status.should == 200
EventMachine.stop
}
}
}
end
end

it "should return ETag and Last-Modified headers" do
Expand Down

0 comments on commit 1ca5b60

Please sign in to comment.