Skip to content

Instantly share code, notes, and snippets.

@prosm
Created May 3, 2011 12:34
Show Gist options
  • Save prosm/953250 to your computer and use it in GitHub Desktop.
Save prosm/953250 to your computer and use it in GitHub Desktop.
Proof of concept showing memory leak in em-websocket
require 'rubygems'
require 'eventmachine'
require 'open-uri'
require 'em-http-request'
def connect_to_ws
http = EventMachine::HttpRequest.new("ws://127.0.0.1:8081/websocket").get :timeout => 0
http.errback { |msg|
puts msg
}
http.callback {
puts "connected"
}
http.stream {
puts "received reply from server"
}
end
EM.epoll = true
EM.set_descriptor_table_size(10000)
EM.run do
10_000.times do |i|
puts i
connect_to_ws
end
end
require 'rubygems'
require 'eventmachine'
require 'em-websocket'
require 'faker'
class ServerHandler
def initialize
@clients = {}
end
def send_message_to_users(message)
@clients.each do |id, client|
client.send(message) #leaking memory
end
end
def add_client(ws)
@clients[rand(10000000)] = ws
end
def remove_client(id)
@clients.delete id
end
def run
puts "starting server"
EventMachine.epoll = true
EventMachine.set_descriptor_table_size(10000)
EventMachine.run {
EventMachine::WebSocket.start(:host => "127.0.0.1", :port => 8081) do |ws|
client_id = 0
ws.onopen do
puts "WebSocket connection open"
client_id = add_client(ws)
end
ws.onclose do
remove_client(client_id)
end
end
EventMachine.add_periodic_timer(3) {
text = Faker::Lorem.paragraphs(200).join
send_message_to_users(text)
}
}
end
end
ServerHandler.new.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment