Created
March 8, 2011 17:12
-
-
Save wycats/860571 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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