Skip to content

Commit

Permalink
Take throttled code from WMCore, see dmwm/WMCore#9158 (#5880)
Browse files Browse the repository at this point in the history
  • Loading branch information
vkuznet authored and belforte committed May 21, 2019
1 parent 1d90e7f commit 877dc88
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 65 deletions.
5 changes: 4 additions & 1 deletion src/python/CRABInterface/HTCondorDataWorkflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
from WMCore.Services.pycurl_manager import ResponseHeader
from WMCore.REST.Error import ExecutionError, InvalidParameter

from CRABInterface.Utils import conn_handler, global_user_throttle
# WMCore Utils module
from Utils.Throttled import global_user_throttle

from CRABInterface.Utils import conn_handler
from ServerUtilities import FEEDBACKMAIL, PUBLICATIONDB_STATES, isCouchDBURL, getEpochFromDBTime
from Databases.FileMetaDataDB.Oracle.FileMetaData.FileMetaData import GetFromTaskAndType

Expand Down
64 changes: 0 additions & 64 deletions src/python/CRABInterface/Utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,70 +145,6 @@ def wrapped_func(*args, **kwargs):
return wrapped_func
return wrap

class _ThrottleCounter(object):

def __init__(self, throttle, user):
self.throttle = throttle
self.user = user

def __enter__(self):
ctr = self.throttle._incUser(self.user)
#self.throttle.logger.debug("Entering throttled function with counter %d for user %s" % (ctr, self.user))
if ctr >= self.throttle.getLimit():
self.throttle._decUser(self.user)
raise ExecutionError("The current number of active operations for this resource exceeds the limit of %d for user %s" % (self.throttle.getLimit(), self.user))

def __exit__(self, type, value, traceback):
ctr = self.throttle._decUser(self.user)
#self.throttle.logger.debug("Exiting throttled function with counter %d for user %s" % (ctr, self.user))

class UserThrottle(object):

def __init__(self, limit=3):
self.lock = threading.Lock()
self.tls = threading.local()
self.users = {}
self.limit = limit
self.logger = logging.getLogger("CRABLogger.Utils.UserThrottle")

def getLimit(self):
return self.limit

def throttleContext(self, user):
self.users.setdefault(user, 0)
return _ThrottleCounter(self, user)

def make_throttled(self):
def throttled_decorator(fn):
def throttled_wrapped_function(*args, **kw):
username = cherrypy.request.user['login']
with self.throttleContext(username):
return fn(*args, **kw)
return throttled_wrapped_function
return throttled_decorator

def _incUser(self, user):
retval = 0
with self.lock:
retval = self.users[user]
if getattr(self.tls, 'count', None) == None:
self.tls.count = 0
self.tls.count += 1
if self.tls.count == 1:
self.users[user] = retval + 1
return retval

def _decUser(self, user):
retval = 0
with self.lock:
retval = self.users[user]
self.tls.count -= 1
if self.tls.count == 0:
self.users[user] = retval - 1
return retval

global_user_throttle = UserThrottle()

def retrieveUserCert(func):
def wrapped_func(*args, **kwargs):
logger = logging.getLogger("CRABLogger.Utils")
Expand Down

0 comments on commit 877dc88

Please sign in to comment.