Skip to content

Instantly share code, notes, and snippets.

@wycats
Created March 8, 2011 17:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wycats/860571 to your computer and use it in GitHub Desktop.
Save wycats/860571 to your computer and use it in GitHub Desktop.
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