Skip to content

Commit

Permalink
Merge pull request #7957 from chrisburr/auth-manager-perf
Browse files Browse the repository at this point in the history
[v8r0] Add caches to AuthManager.getUsername
  • Loading branch information
fstagni authored Dec 19, 2024
2 parents 8ba45d2 + dc04c00 commit 2c277f1
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/DIRAC/Core/DISET/AuthManager.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
""" Module that holds DISET Authorization class for services
"""
from cachetools import TTLCache

from DIRAC.ConfigurationSystem.Client.Config import gConfig
from DIRAC.ConfigurationSystem.Client.Helpers import Registry
from DIRAC.Core.Security import Properties
Expand All @@ -26,6 +28,8 @@ def __init__(self, authSection):
:param authSection: Section containing the authorization rules
"""
self.authSection = authSection
self._cache_getUsersInGroup = TTLCache(maxsize=1000, ttl=60)
self._cache_getUsernameForDN = TTLCache(maxsize=1000, ttl=60)

def authQuery(self, methodQuery, credDict, defaultProperties=False):
"""
Expand Down Expand Up @@ -257,10 +261,18 @@ def getUsername(self, credDict):
return False
credDict[self.KW_GROUP] = result["Value"]
credDict[self.KW_PROPERTIES] = Registry.getPropertiesForGroup(credDict[self.KW_GROUP], [])
usersInGroup = Registry.getUsersInGroup(credDict[self.KW_GROUP], [])

usersInGroup = self._cache_getUsersInGroup.get(credDict[self.KW_GROUP])
if usersInGroup is None:
usersInGroup = Registry.getUsersInGroup(credDict[self.KW_GROUP], [])
self._cache_getUsersInGroup[credDict[self.KW_GROUP]] = usersInGroup
if not usersInGroup:
return False
retVal = Registry.getUsernameForDN(credDict[self.KW_DN], usersInGroup)

retVal = self._cache_getUsernameForDN.get(credDict[self.KW_DN])
if retVal is None:
retVal = Registry.getUsernameForDN(credDict[self.KW_DN], usersInGroup)
self._cache_getUsernameForDN[credDict[self.KW_DN]] = retVal
if retVal["OK"]:
credDict[self.KW_USERNAME] = retVal["Value"]
return True
Expand Down

0 comments on commit 2c277f1

Please sign in to comment.