benoitc/socketpool — Issue #5: connection put itself back into pool
Author: cactus
Created: 2012-02-17 00:40:02+00:00
State: open

It seems to me that if a connection falls out of scope or is deleted (del) with no additional references to it, it would no longer be available in the pool (and possibly not cleanly release resources as the underlying socket may not have been closed?).

Would it make sense to pass the pool itself to the Connection as an argument upon creation, and provide the ability in a `__del__ ` inside the Connection to check `self._connected`, and put itself back into the pool if it is indeed still connected?

A call to invalidate would set `_connected` to False, so in that case when reference count hits zero, it should be cleaned up.

Something like..

~~~
class TcpConnector(Connector):
def __init__(self, host, port, pool, backend_mod):
self._s = backend_mod.Socket(socket.AF_INET, socket.SOCK_STREAM)
self._s.connect((host, port))
self.host = host
self.port = port
self.backend_mod = backend_mod
self._pool = pool
self._connected = True
self._life = time.time()

def __del__():
if self._connected == True:
self._pool.release_connection(self)

## rest of class code..
~~~

Thoughts?