Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add capability for conn to put itself back into the pool #9

Merged
merged 3 commits into from

2 participants

@cactus

Add capability for a connection to put itself back into the pool if:

  • explicitly told to
  • all existing references are removed/deleted and the connection is still valid (connected==True).

This is achieve by passing the current pool to the client upon
creation. The client can then optionally choose to put iself back into
the pool, if the connector class supports it.

This is likely a breaking change, due to the connector class
instantiation signature modification.

fix for issue #5

cactus added some commits
@cactus cactus Add capability for conn to put itself back into the pool
Add capability for a connection to put itself back into the pool if:

*   explicitly told to
*   all existing references are removed/deleted and the connection is
    still valid (connected==True).

This is achieve by passing the current pool to the client upon
creation. The client can then optionally choose to put iself back into
the pool, if the connector class supports it.

This is likely a breaking change, due to the connector class
instantiation signature modification.

fix for issue #5
46f0525
@cactus cactus fix NameError in threading example ef40a11
@cactus cactus fix for using the wrong pool element in the signature f81bcff
@benoitc benoitc merged commit e473a88 into benoitc:master
@benoitc
Owner

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 29, 2012
  1. @cactus

    Add capability for conn to put itself back into the pool

    cactus authored
    Add capability for a connection to put itself back into the pool if:
    
    *   explicitly told to
    *   all existing references are removed/deleted and the connection is
        still valid (connected==True).
    
    This is achieve by passing the current pool to the client upon
    creation. The client can then optionally choose to put iself back into
    the pool, if the connector class supports it.
    
    This is likely a breaking change, due to the connector class
    instantiation signature modification.
    
    fix for issue #5
  2. @cactus
  3. @cactus
This page is out of date. Refresh to see the latest.
View
13 README.rst
@@ -71,13 +71,17 @@ Example of a connector
class TcpConnector(Connector):
- def __init__(self, host, port, backend_mod):
+ def __init__(self, host, port, backend_mod, pool=None):
self._s = backend_mod.Socket(socket.AF_INET, socket.SOCK_STREAM)
self._s.connect((host, port))
self.host = host
self.port = port
self._connected = True
self._life = time.time()
+ self._pool = pool
+
+ def __del__(self):
+ self.release()
def matches(self, **match_options):
target_host = match_options.get('host')
@@ -99,6 +103,13 @@ Example of a connector
self._connected = False
self._life = -1
+ def release(self):
+ if self._pool is not None:
+ if self._connected:
+ self._pool.release_connection(self)
+ else:
+ self._pool = None
+
def send(self, data):
return self._s.send(data)
View
2  examples/test_threaded.py
@@ -70,6 +70,6 @@ def runpool():
th.daemnon = True
th.start()
- queue.join()
+ q.join()
server.shutdown()
View
13 socketpool/conn.py
@@ -26,7 +26,7 @@ def invalidate(self):
class TcpConnector(Connector):
- def __init__(self, host, port, backend_mod):
+ def __init__(self, host, port, backend_mod, pool=None):
self._s = backend_mod.Socket(socket.AF_INET, socket.SOCK_STREAM)
self._s.connect((host, port))
self.host = host
@@ -34,6 +34,10 @@ def __init__(self, host, port, backend_mod):
self.backend_mod = backend_mod
self._connected = True
self._life = time.time()
+ self._pool = pool
+
+ def __del__(self):
+ self.release()
def matches(self, **match_options):
target_host = match_options.get('host')
@@ -62,6 +66,13 @@ def invalidate(self):
self._connected = False
self._life = -1
+ def release(self):
+ if self._pool is not None:
+ if self._connected:
+ self._pool.release_connection(self)
+ else:
+ self._pool = None
+
def send(self, data):
return self._s.send(data)
View
4 socketpool/pool.py
@@ -32,10 +32,12 @@ def __init__(self, factory,
self.timeout = timeout
self.max_lifetime = max_lifetime
if options is None:
- self.options = {"backend_mod": self.backend_mod}
+ self.options = {"backend_mod": self.backend_mod,
+ "pool": self}
else:
self.options = options
self.options["backend_mod"] = self.backend_mod
+ self.options["pool"] = self
self._reaper = None
if reap_connections:
Something went wrong with that request. Please try again.