Skip to content

Instantly share code, notes, and snippets.

@mloughran
Created August 15, 2012 19:33
Show Gist options
  • Save mloughran/3362910 to your computer and use it in GitHub Desktop.
Save mloughran/3362910 to your computer and use it in GitHub Desktop.
em-http-request not closing connections (v 1.0.2)

Start the sinatra app

bundle exec ruby app.rb

Use em-http-request to fetch the streaming URL exposed by sinatra - you should see that the request continues to stream to completion

bundle exec ruby requester.rb

Now uncomment conn.conn.close_connection and run again.

require 'sinatra'
get '/long_stream' do
puts "Handling request"
status 200
stream do |s|
100000.times do |i|
s << 'lorem ipsum'
end
end
end
source :rubygems
gem 'eventmachine', '1.0.0.rc.4'
gem 'em-http-request', '1.0.2'
gem 'sinatra'
gem 'thin'
GEM
remote: http://rubygems.org/
specs:
addressable (2.3.2)
cookiejar (0.3.0)
daemons (1.1.9)
em-http-request (1.0.2)
addressable (>= 2.2.3)
cookiejar
em-socksify
eventmachine (>= 1.0.0.beta.4)
http_parser.rb (>= 0.5.3)
em-socksify (0.2.1)
eventmachine (>= 1.0.0.beta.4)
eventmachine (1.0.0.rc.4)
http_parser.rb (0.5.3)
rack (1.4.1)
rack-protection (1.2.0)
rack
sinatra (1.3.2)
rack (~> 1.3, >= 1.3.6)
rack-protection (~> 1.2)
tilt (~> 1.3, >= 1.3.3)
thin (1.4.1)
daemons (>= 1.0.9)
eventmachine (>= 0.12.6)
rack (>= 1.0.0)
tilt (1.3.3)
PLATFORMS
ruby
DEPENDENCIES
em-http-request (= 1.0.2)
eventmachine (= 1.0.0.rc.4)
sinatra
thin
require 'eventmachine'
require 'em-http'
MAX_BYTES = 1000
EM.run {
conn = EM::HttpRequest.new('http://localhost:4567/long_stream')
req = conn.get
received_bytes = 0
req.stream { |chunk|
p [:chunk_received, chunk.size]
received_bytes += chunk.size
if received_bytes > MAX_BYTES
p "Attempting to close connection"
# This will close the connection if uncommented
# conn.conn.close_connection
# This calls the callback but doesn't actually close the connection
conn.close(:max_bytes_exceeded)
end
}
req.callback {
p 'callback'
}
req.errback {
p 'errback'
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment