Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class SocketPool
def initialize
@sockets = Hash.new { |h,k| h[k] = [] }
@mutex = Mutex.new
end
def checkout(host, port)
@mutex.synchronize do
sockets = @sockets[host][port]
if sockets.empty?
TCPSocket.new(host, port)
else
sockets.pop
end
end
end
def checkin(socket)
@mutex.synchronize do
@sockets[socket.host][socket.port] << socket
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.