Echo Server does not work #1011
-
Hello, I would like to build an EchoServer with Concurrent::Async. For this I have the following code: require 'concurrent-ruby'
require 'socket'
class EchoServer
include Concurrent::Async
def initialize(port)
@server = TCPServer.new('localhost', port)
@server.do_not_reverse_lookup = true
end
def run
puts "Accepting connections."
loop do
puts "Ready for new connection."
async.handle_connection @server.accept
puts "Connection delegated."
end
end
def handle_connection(socket)
puts "Handle new connection."
_, port, host = socket.peeraddr
puts "Incoming connection from #{host} port #{port}."
while ! socket.closed?
while ! socket.eof?
IO::copy_stream(socket, socket)
end
socket.flush
end
end
end
server = EchoServer.new 6666
server.async.run
sleep 10000 However, the server does not work as desired. It accepts incoming connections. However, it never seems to reach the handle_connection function. Is this a bug? Or do I have a thinking error somewhere here? Does anyone have any ideas about this? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hi! Maybe I can help. concurrent-ruby/lib/concurrent-ruby/concurrent/async.rb Lines 101 to 103 in 1982b92 You should not have |
Beta Was this translation helpful? Give feedback.
If you need more than 1 thread, I don't think
Concurrent::Async
is the right tool, because it will only ever allocate 1 task thread for your object.I think what you want is to instantiate a thread pool (or use the global io pool, though it is unbounded) and then create Concurrent::Future / Promises that use that pool.
Something that looks like this (sorry, this is off the top of my head, so it might have some typos):