<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[15784] twext/trunk/twext/who/ldap</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.calendarserver.org//changeset/15784">15784</a></dd>
<dt>Author</dt> <dd>sagen@apple.com</dd>
<dt>Date</dt> <dd>2016-08-03 16:20:52 -0700 (Wed, 03 Aug 2016)</dd>
</dl>

<h3>Log Message</h3>
<pre>LDAP connection pool sizes now indepently configurable; pool name is included in log messages and stats keys; an anonymous bind is performed after the credentials-verification bind in case servers limit the number of times a given user can be &quot;logged in&quot; at the same time.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#twexttrunktwextwholdap_servicepy">twext/trunk/twext/who/ldap/_service.py</a></li>
<li><a href="#twexttrunktwextwholdaptesttest_servicepy">twext/trunk/twext/who/ldap/test/test_service.py</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="twexttrunktwextwholdap_servicepy"></a>
<div class="modfile"><h4>Modified: twext/trunk/twext/who/ldap/_service.py (15783 => 15784)</h4>
<pre class="diff"><span>
<span class="info">--- twext/trunk/twext/who/ldap/_service.py        2016-08-03 18:01:01 UTC (rev 15783)
+++ twext/trunk/twext/who/ldap/_service.py        2016-08-03 23:20:52 UTC (rev 15784)
</span><span class="lines">@@ -217,6 +217,10 @@
</span><span class="cx">         self.activeCount = 0
</span><span class="cx">         self.connectionsCreated = 0
</span><span class="cx">         self.connectionMax = connectionMax
</span><ins>+        self.log.debug(
+            &quot;Created {pool} LDAP connection pool with connectionMax={max}&quot;,
+            pool=self.poolName, max=self.connectionMax
+        )
</ins><span class="cx"> 
</span><span class="cx">     def getConnection(self):
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="lines">@@ -243,25 +247,25 @@
</span><span class="cx">                 self.connectionCreateLock.release()
</span><span class="cx">             else:
</span><span class="cx">                 self.connectionCreateLock.release()
</span><del>-                self.ds.poolStats[&quot;connection-blocked&quot;] += 1
</del><ins>+                self.ds.poolStats[&quot;connection-{}-blocked&quot;.format(self.poolName)] += 1
</ins><span class="cx">                 connection = self.connectionQueue.get()
</span><span class="cx"> 
</span><span class="cx"> 
</span><del>-        connectionID = &quot;connection-{}&quot;.format(
-            self.connections.index(connection)
</del><ins>+        connectionID = &quot;connection-{}-{}&quot;.format(
+            self.poolName, self.connections.index(connection)
</ins><span class="cx">         )
</span><span class="cx"> 
</span><span class="cx">         self.ds.poolStats[connectionID] += 1
</span><span class="cx">         self.activeCount = len(self.connections) - self.connectionQueue.qsize()
</span><del>-        self.ds.poolStats[&quot;connection-active&quot;] = self.activeCount
-        self.ds.poolStats[&quot;connection-max&quot;] = max(
-            self.ds.poolStats[&quot;connection-max&quot;], self.activeCount
</del><ins>+        self.ds.poolStats[&quot;connection-{}-active&quot;.format(self.poolName)] = self.activeCount
+        self.ds.poolStats[&quot;connection-{}-max&quot;.format(self.poolName)] = max(
+            self.ds.poolStats[&quot;connection-{}-max&quot;.format(self.poolName)], self.activeCount
</ins><span class="cx">         )
</span><span class="cx"> 
</span><span class="cx">         if self.activeCount &gt; self.connectionMax:
</span><span class="cx">             self.log.error(
</span><del>-                &quot;Active LDAP connections ({active}) exceeds maximum ({max})&quot;,
-                active=self.activeCount, max=self.connectionMax
</del><ins>+                &quot;[Pool: {pool}] Active LDAP connections ({active}) exceeds maximum ({max})&quot;,
+                pool=self.poolName, active=self.activeCount, max=self.connectionMax
</ins><span class="cx">             )
</span><span class="cx">         return connection
</span><span class="cx"> 
</span><span class="lines">@@ -272,6 +276,7 @@
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         self.connectionQueue.put(connection)
</span><span class="cx">         self.activeCount = len(self.connections) - self.connectionQueue.qsize()
</span><ins>+        self.ds.poolStats[&quot;connection-{}-active&quot;.format(self.poolName)] = self.activeCount
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     def failedConnection(self, connection):
</span><span class="lines">@@ -279,9 +284,10 @@
</span><span class="cx">         A connection has failed; remove it from the list of active connections.
</span><span class="cx">         A new one will be created if needed.
</span><span class="cx">         &quot;&quot;&quot;
</span><del>-        self.ds.poolStats[&quot;connection-errors&quot;] += 1
</del><ins>+        self.ds.poolStats[&quot;connection-{}-errors&quot;.format(self.poolName)] += 1
</ins><span class="cx">         self.connections.remove(connection)
</span><span class="cx">         self.activeCount = len(self.connections) - self.connectionQueue.qsize()
</span><ins>+        self.ds.poolStats[&quot;connection-{}-active&quot;.format(self.poolName)] = self.activeCount
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     def _connect(self):
</span><span class="lines">@@ -295,7 +301,10 @@
</span><span class="cx">         @raises: L{LDAPConnectionError} if unable to connect.
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx"> 
</span><del>-        self.log.debug(&quot;Connecting to LDAP at {log_source.url}&quot;)
</del><ins>+        self.log.debug(
+            &quot;[Pool: {pool}] Connecting to LDAP at {url}&quot;,
+            pool=self.poolName, url=self.ds.url
+        )
</ins><span class="cx">         connection = self._newConnection()
</span><span class="cx"> 
</span><span class="cx">         if self.credentials is not None:
</span><span class="lines">@@ -306,15 +315,15 @@
</span><span class="cx">                         self.credentials.password,
</span><span class="cx">                     )
</span><span class="cx">                     self.log.debug(
</span><del>-                        &quot;Bound to LDAP as {credentials.username}&quot;,
-                        credentials=self.credentials
</del><ins>+                        &quot;[Pool: {pool}] Bound to LDAP as {credentials.username}&quot;,
+                        pool=self.poolName, credentials=self.credentials
</ins><span class="cx">                     )
</span><span class="cx">                 except (
</span><span class="cx">                     ldap.INVALID_CREDENTIALS, ldap.INVALID_DN_SYNTAX
</span><span class="cx">                 ) as e:
</span><span class="cx">                     self.log.error(
</span><del>-                        &quot;Unable to bind to LDAP as {credentials.username}&quot;,
-                        credentials=self.credentials
</del><ins>+                        &quot;[Pool: {pool}] Unable to bind to LDAP as {credentials.username}&quot;,
+                        pool=self.poolName, credentials=self.credentials
</ins><span class="cx">                     )
</span><span class="cx">                     raise LDAPBindAuthError(
</span><span class="cx">                         self.credentials.username, e
</span><span class="lines">@@ -354,7 +363,10 @@
</span><span class="cx">                 connection.set_option(option, value)
</span><span class="cx"> 
</span><span class="cx">         if self.ds._useTLS:
</span><del>-            self.log.debug(&quot;Starting TLS for {log_source.url}&quot;)
</del><ins>+            self.log.debug(
+                &quot;[Pool: {pool}] Starting TLS for {url}&quot;,
+                pool=self.poolName, url=self.ds.url
+            )
</ins><span class="cx">             connection.start_tls_s()
</span><span class="cx"> 
</span><span class="cx">         self.connectionsCreated += 1
</span><span class="lines">@@ -394,7 +406,8 @@
</span><span class="cx">         extraFilters=None,
</span><span class="cx">         ownThreadpool=True,
</span><span class="cx">         threadPoolMax=10,
</span><del>-        connectionMax=10,
</del><ins>+        authConnectionMax=5,
+        queryConnectionMax=5,
</ins><span class="cx">         tries=3,
</span><span class="cx">         warningThresholdSeconds=5,
</span><span class="cx">         _debug=False,
</span><span class="lines">@@ -509,11 +522,10 @@
</span><span class="cx">                 max(threadPoolMax, self.threadpool.max)
</span><span class="cx">             )
</span><span class="cx"> 
</span><del>-        # Separate pools for LDAP queries and LDAP binds.  Note, they each get
-        # half of connectionMax.
</del><ins>+        # Separate pools for LDAP queries and LDAP binds.
</ins><span class="cx">         self.connectionPools = {
</span><del>-            &quot;query&quot;: ConnectionPool(&quot;query&quot;, self, credentials, connectionMax / 2),
-            &quot;auth&quot;: ConnectionPool(&quot;auth&quot;, self, None, connectionMax / 2),
</del><ins>+            &quot;query&quot;: ConnectionPool(&quot;query&quot;, self, credentials, queryConnectionMax),
+            &quot;auth&quot;: ConnectionPool(&quot;auth&quot;, self, None, authConnectionMax),
</ins><span class="cx">         }
</span><span class="cx">         self.poolStats = collections.defaultdict(int)
</span><span class="cx"> 
</span><span class="lines">@@ -625,6 +637,10 @@
</span><span class="cx">             except Exception as e:
</span><span class="cx">                 self.log.error(&quot;Unexpected error {error} trying to authenticate {dn}&quot;, error=str(e), dn=dn)
</span><span class="cx">                 return False
</span><ins>+            finally:
+                # Do an unauthenticated bind on this connection at the end in
+                # case the server limits the number of concurrent auths by a given user.
+                connection.simple_bind_s(&quot;&quot;, &quot;&quot;)
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     def _recordsFromQueryString(
</span></span></pre></div>
<a id="twexttrunktwextwholdaptesttest_servicepy"></a>
<div class="modfile"><h4>Modified: twext/trunk/twext/who/ldap/test/test_service.py (15783 => 15784)</h4>
<pre class="diff"><span>
<span class="info">--- twext/trunk/twext/who/ldap/test/test_service.py        2016-08-03 18:01:01 UTC (rev 15783)
+++ twext/trunk/twext/who/ldap/test/test_service.py        2016-08-03 23:20:52 UTC (rev 15784)
</span><span class="lines">@@ -499,7 +499,7 @@
</span><span class="cx">         actually created is what we expect.
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx"> 
</span><del>-        service = self.service(connectionMax=4)
</del><ins>+        service = self.service(authConnectionMax=2)
</ins><span class="cx">         pool = service.connectionPools[&quot;auth&quot;]
</span><span class="cx"> 
</span><span class="cx">         self.assertEquals(0, len(pool.connections))
</span><span class="lines">@@ -508,7 +508,11 @@
</span><span class="cx">         # Ask for a connection and check the counts
</span><span class="cx">         with TestService.Connection(service, &quot;auth&quot;):
</span><span class="cx">             self.assertEquals(1, len(pool.connections))
</span><ins>+            self.assertEquals(1, service.poolStats[&quot;connection-auth-active&quot;])
</ins><span class="cx"> 
</span><ins>+        # When the above context exits, the connection is returned
+        self.assertEquals(0, service.poolStats[&quot;connection-auth-active&quot;])
+
</ins><span class="cx">         self.assertEquals(1, len(pool.connections))
</span><span class="cx">         self.assertEquals(1, pool.connectionsCreated)
</span><span class="cx"> 
</span><span class="lines">@@ -519,33 +523,40 @@
</span><span class="cx">             with TestService.Connection(service, &quot;auth&quot;):
</span><span class="cx">                 self.assertEquals(2, len(pool.connections))
</span><span class="cx">                 self.assertEquals(2, pool.connectionsCreated)
</span><ins>+                self.assertEquals(2, service.poolStats[&quot;connection-auth-active&quot;])
+            self.assertEquals(1, service.poolStats[&quot;connection-auth-active&quot;])
+        self.assertEquals(0, service.poolStats[&quot;connection-auth-active&quot;])
</ins><span class="cx"> 
</span><del>-        # Ask for three connections (one more than connectionMax/2) and
</del><ins>+        # Ask for three connections (one more than connectionMax) and
</ins><span class="cx">         # the third will actually block until the returnConnection( ) call
</span><span class="cx">         with TestService.Connection(service, &quot;auth&quot;):
</span><span class="cx">             self.assertEquals(2, len(pool.connections))
</span><span class="cx">             self.assertEquals(2, pool.connectionsCreated)
</span><del>-            with TestService.Connection(service, &quot;auth&quot;) as connection:
-                self.assertEquals(2, len(pool.connections))
-                self.assertEquals(2, pool.connectionsCreated)
</del><span class="cx"> 
</span><del>-                # schedule a connection to be returned in 1 second
-                from twisted.internet import reactor
-                reactor.callLater(1, pool.returnConnection, connection)
</del><ins>+            # For the 2nd connection, don't use the context manager so we can
+            # control when returnConnection happens
+            connection = pool.getConnection()
+            self.assertEquals(2, len(pool.connections))
+            self.assertEquals(2, pool.connectionsCreated)
</ins><span class="cx"> 
</span><del>-                # For the third connection, I'm using this method so it gets
-                # requested in a thread, otherwise we'd hang.
-                yield service._authenticateUsernamePassword(
-                    u&quot;uid=wsanchez,cn=user,{0}&quot;.format(self.baseDN),
-                    u&quot;zehcnasw&quot;
-                )
</del><ins>+            # schedule connection 2 to be returned in 1 second
+            from twisted.internet import reactor
+            reactor.callLater(1, pool.returnConnection, connection)
</ins><span class="cx"> 
</span><del>-        # Proof we bumped up against connection-max:
-        self.assertEquals(1, service.poolStats[&quot;connection-blocked&quot;])
</del><ins>+            # For the third connection, I'm using this method so it gets
+            # requested in a thread, otherwise we'd hang.
+            yield service._authenticateUsernamePassword(
+                u&quot;uid=wsanchez,cn=user,{0}&quot;.format(self.baseDN),
+                u&quot;zehcnasw&quot;
+            )
</ins><span class="cx"> 
</span><ins>+        # Proof we bumped up against connectionMax:
+        self.assertEquals(1, service.poolStats[&quot;connection-auth-blocked&quot;])
+
</ins><span class="cx">         self.assertEquals(2, len(pool.connections))
</span><span class="cx">         self.assertEquals(2, pool.connectionsCreated)
</span><del>-        self.assertEquals(2, service.poolStats[&quot;connection-max&quot;])
</del><ins>+        self.assertEquals(2, service.poolStats[&quot;connection-auth-max&quot;])
+        self.assertEquals(0, service.poolStats[&quot;connection-auth-active&quot;])
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>