<!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 "logged in" 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(
+ "Created {pool} LDAP connection pool with connectionMax={max}",
+ pool=self.poolName, max=self.connectionMax
+ )
</ins><span class="cx">
</span><span class="cx"> def getConnection(self):
</span><span class="cx"> """
</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["connection-blocked"] += 1
</del><ins>+ self.ds.poolStats["connection-{}-blocked".format(self.poolName)] += 1
</ins><span class="cx"> connection = self.connectionQueue.get()
</span><span class="cx">
</span><span class="cx">
</span><del>- connectionID = "connection-{}".format(
- self.connections.index(connection)
</del><ins>+ connectionID = "connection-{}-{}".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["connection-active"] = self.activeCount
- self.ds.poolStats["connection-max"] = max(
- self.ds.poolStats["connection-max"], self.activeCount
</del><ins>+ self.ds.poolStats["connection-{}-active".format(self.poolName)] = self.activeCount
+ self.ds.poolStats["connection-{}-max".format(self.poolName)] = max(
+ self.ds.poolStats["connection-{}-max".format(self.poolName)], self.activeCount
</ins><span class="cx"> )
</span><span class="cx">
</span><span class="cx"> if self.activeCount > self.connectionMax:
</span><span class="cx"> self.log.error(
</span><del>- "Active LDAP connections ({active}) exceeds maximum ({max})",
- active=self.activeCount, max=self.connectionMax
</del><ins>+ "[Pool: {pool}] Active LDAP connections ({active}) exceeds maximum ({max})",
+ 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"> """
</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["connection-{}-active".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"> """
</span><del>- self.ds.poolStats["connection-errors"] += 1
</del><ins>+ self.ds.poolStats["connection-{}-errors".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["connection-{}-active".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"> """
</span><span class="cx">
</span><del>- self.log.debug("Connecting to LDAP at {log_source.url}")
</del><ins>+ self.log.debug(
+ "[Pool: {pool}] Connecting to LDAP at {url}",
+ 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>- "Bound to LDAP as {credentials.username}",
- credentials=self.credentials
</del><ins>+ "[Pool: {pool}] Bound to LDAP as {credentials.username}",
+ 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>- "Unable to bind to LDAP as {credentials.username}",
- credentials=self.credentials
</del><ins>+ "[Pool: {pool}] Unable to bind to LDAP as {credentials.username}",
+ 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("Starting TLS for {log_source.url}")
</del><ins>+ self.log.debug(
+ "[Pool: {pool}] Starting TLS for {url}",
+ 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>- "query": ConnectionPool("query", self, credentials, connectionMax / 2),
- "auth": ConnectionPool("auth", self, None, connectionMax / 2),
</del><ins>+ "query": ConnectionPool("query", self, credentials, queryConnectionMax),
+ "auth": ConnectionPool("auth", 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("Unexpected error {error} trying to authenticate {dn}", 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("", "")
</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"> """
</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["auth"]
</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, "auth"):
</span><span class="cx"> self.assertEquals(1, len(pool.connections))
</span><ins>+ self.assertEquals(1, service.poolStats["connection-auth-active"])
</ins><span class="cx">
</span><ins>+ # When the above context exits, the connection is returned
+ self.assertEquals(0, service.poolStats["connection-auth-active"])
+
</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, "auth"):
</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["connection-auth-active"])
+ self.assertEquals(1, service.poolStats["connection-auth-active"])
+ self.assertEquals(0, service.poolStats["connection-auth-active"])
</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, "auth"):
</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, "auth") 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"uid=wsanchez,cn=user,{0}".format(self.baseDN),
- u"zehcnasw"
- )
</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["connection-blocked"])
</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"uid=wsanchez,cn=user,{0}".format(self.baseDN),
+ u"zehcnasw"
+ )
</ins><span class="cx">
</span><ins>+ # Proof we bumped up against connectionMax:
+ self.assertEquals(1, service.poolStats["connection-auth-blocked"])
+
</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["connection-max"])
</del><ins>+ self.assertEquals(2, service.poolStats["connection-auth-max"])
+ self.assertEquals(0, service.poolStats["connection-auth-active"])
</ins><span class="cx">
</span><span class="cx">
</span><span class="cx">
</span></span></pre>
</div>
</div>
</body>
</html>