Browse Source

Allow pool status to be printed as a table

This patch adds a __str__() method to the ConnectionManager class,
which allows for a nice readible table to be obtained that shows
the current state of the connection pool.  This can be very useful
for troubleshooting or monitoring issues related to connection
pooling.  The table will contain a row for each connection within
the pool, with columns showing the connection slot, connectivity
status, activity status, URI, connection lifetime, and bind DN.
The header row will also indicate the pool size and maximum
connection lifetime setting.

Note that this adds a dependency on the prettytable module.  This
new dependency seems worth it for the nice readible table format
it produces.

Change-Id: If0abfef405d05ecd499bdf6201ff465bd845957b
Nathan Kinder 5 months ago
parent
commit
acc14fca3a
4 changed files with 39 additions and 0 deletions
  1. 19
    0
      README.rst
  2. 18
    0
      ldappool/__init__.py
  3. 1
    0
      lower-constraints.txt
  4. 1
    0
      requirements.txt

+ 19
- 0
README.rst View File

@@ -32,6 +32,25 @@ context manager with the *connection* method::
32 32
 The connector returned by *connection* is a LDAPObject, that's binded to the
33 33
 server. See https://pypi.org/project/python-ldap/ for details on how to use a connector.
34 34
 
35
+It is possible to check the state of the pool by representing the pool as a string::
36
+
37
+    from ldappool import ConnectionManager
38
+
39
+    cm = ConnectionManager('ldap://localhost', size=2)
40
+
41
+    .. do something with cm ..
42
+
43
+    print(cm)
44
+
45
+This will result in output similar to this table::
46
+
47
+    +--------------+-----------+----------+------------------+--------------------+------------------------------+
48
+    | Slot (2 max) | Connected |  Active  |       URI        | Lifetime (600 max) |           Bind DN            |
49
+    +--------------+-----------+----------+------------------+--------------------+------------------------------+
50
+    |      1       | connected | inactive | ldap://localhost |  0.00496101379395  | uid=tuser,dc=example,dc=test |
51
+    |      2       | connected | inactive | ldap://localhost |  0.00532603263855  | uid=tuser,dc=example,dc=test |
52
+    +--------------+-----------+----------+------------------+--------------------+------------------------------+
53
+
35 54
 
36 55
 ConnectionManager options
37 56
 :::::::::::::::::::::::::

+ 18
- 0
ldappool/__init__.py View File

@@ -43,6 +43,7 @@ import time
43 43
 
44 44
 import ldap
45 45
 from ldap.ldapobject import ReconnectLDAPObject
46
+from prettytable import PrettyTable
46 47
 import re
47 48
 import six
48 49
 from six import PY2
@@ -423,3 +424,20 @@ class ConnectionManager(object):
423 424
                     log.debug('Failure attempting to unbind on purge; '
424 425
                               'should be harmless', exc_info=True)
425 426
                 self._pool.remove(conn)
427
+
428
+    def __str__(self):
429
+        table = PrettyTable()
430
+        table.field_names = ['Slot (%d max)' % self.size,
431
+                             'Connected', 'Active', 'URI',
432
+                             'Lifetime (%d max)' % self.max_lifetime,
433
+                             'Bind DN']
434
+
435
+        with self._pool_lock:
436
+            for slot, conn in enumerate(self._pool):
437
+                table.add_row([
438
+                    slot + 1,
439
+                    'connected' if conn.connected else 'not connected',
440
+                    'active' if conn.active else 'inactive',
441
+                    conn._uri, conn.get_lifetime(), conn.who])
442
+
443
+        return str(table)

+ 1
- 0
lower-constraints.txt View File

@@ -17,6 +17,7 @@ openstackdocstheme==1.18.1
17 17
 pbr==2.0.0
18 18
 pep257==0.7.0
19 19
 pep8==1.5.7
20
+prettytable==0.7.2
20 21
 pyflakes==0.8.1
21 22
 Pygments==2.2.0
22 23
 python-ldap==3.0.0

+ 1
- 0
requirements.txt View File

@@ -2,3 +2,4 @@
2 2
 # of appearance. Changing the order has an impact on the overall integration
3 3
 # process, which may cause wedges in the gate later.
4 4
 python-ldap>=3.0.0 # PSF
5
+PrettyTable<0.8,>=0.7.2

Loading…
Cancel
Save