remove logging to increase speed, add more caching to policy and rewriter
This commit is contained in:
@ -1,7 +1,5 @@
|
||||
"""passbook core policy engine"""
|
||||
import cProfile
|
||||
from logging import getLogger
|
||||
|
||||
# from logging import getLogger
|
||||
from amqp.exceptions import UnexpectedFrame
|
||||
from celery import group
|
||||
from celery.exceptions import TimeoutError as CeleryTimeoutError
|
||||
@ -11,19 +9,7 @@ from ipware import get_client_ip
|
||||
from passbook.core.celery import CELERY_APP
|
||||
from passbook.core.models import Policy, User
|
||||
|
||||
|
||||
def profileit(func):
|
||||
def wrapper(*args, **kwargs):
|
||||
datafn = func.__name__ + ".profile" # Name the data file sensibly
|
||||
prof = cProfile.Profile()
|
||||
retval = prof.runcall(func, *args, **kwargs)
|
||||
prof.dump_stats(datafn)
|
||||
return retval
|
||||
|
||||
return wrapper
|
||||
|
||||
|
||||
LOGGER = getLogger(__name__)
|
||||
# LOGGER = getLogger(__name__)
|
||||
|
||||
def _cache_key(policy, user):
|
||||
return "%s#%s" % (policy.uuid, user.pk)
|
||||
@ -37,8 +23,8 @@ def _policy_engine_task(user_pk, policy_pk, **kwargs):
|
||||
user_obj = User.objects.get(pk=user_pk)
|
||||
for key, value in kwargs.items():
|
||||
setattr(user_obj, key, value)
|
||||
LOGGER.debug("Running policy `%s`#%s for user %s...", policy_obj.name,
|
||||
policy_obj.pk.hex, user_obj)
|
||||
# LOGGER.debug("Running policy `%s`#%s for user %s...", policy_obj.name,
|
||||
# policy_obj.pk.hex, user_obj)
|
||||
policy_result = policy_obj.passes(user_obj)
|
||||
# Handle policy result correctly if result, message or just result
|
||||
message = None
|
||||
@ -47,10 +33,10 @@ def _policy_engine_task(user_pk, policy_pk, **kwargs):
|
||||
# Invert result if policy.negate is set
|
||||
if policy_obj.negate:
|
||||
policy_result = not policy_result
|
||||
LOGGER.debug("Policy %r#%s got %s", policy_obj.name, policy_obj.pk.hex, policy_result)
|
||||
# LOGGER.debug("Policy %r#%s got %s", policy_obj.name, policy_obj.pk.hex, policy_result)
|
||||
cache_key = _cache_key(policy_obj, user_obj)
|
||||
cache.set(cache_key, (policy_obj.action, policy_result, message))
|
||||
LOGGER.debug("Cached entry as %s", cache_key)
|
||||
# LOGGER.debug("Cached entry as %s", cache_key)
|
||||
return policy_obj.action, policy_result, message
|
||||
|
||||
class PolicyEngine:
|
||||
@ -79,7 +65,6 @@ class PolicyEngine:
|
||||
self.__request = request
|
||||
return self
|
||||
|
||||
@profileit
|
||||
def build(self):
|
||||
"""Build task group"""
|
||||
if not self.__user:
|
||||
@ -96,16 +81,16 @@ class PolicyEngine:
|
||||
for policy in self.policies:
|
||||
cached_policy = cache.get(_cache_key(policy, self.__user), None)
|
||||
if cached_policy:
|
||||
LOGGER.warning("Taking result from cache for %s", policy.pk.hex)
|
||||
# LOGGER.debug("Taking result from cache for %s", policy.pk.hex)
|
||||
cached_policies.append(cached_policy)
|
||||
else:
|
||||
LOGGER.warning("Evaluating policy %s", policy.pk.hex)
|
||||
# LOGGER.debug("Evaluating policy %s", policy.pk.hex)
|
||||
signatures.append(_policy_engine_task.signature(
|
||||
args=(self.__user.pk, policy.pk.hex),
|
||||
kwargs=kwargs,
|
||||
time_limit=policy.timeout))
|
||||
self.__get_timeout += policy.timeout
|
||||
LOGGER.warning("Set total policy timeout to %r", self.__get_timeout)
|
||||
# LOGGER.debug("Set total policy timeout to %r", self.__get_timeout)
|
||||
# If all policies are cached, we have an empty list here.
|
||||
if signatures:
|
||||
self.__group = group(signatures)()
|
||||
@ -134,7 +119,7 @@ class PolicyEngine:
|
||||
for policy_action, policy_result, policy_message in result:
|
||||
passing = (policy_action == Policy.ACTION_ALLOW and policy_result) or \
|
||||
(policy_action == Policy.ACTION_DENY and not policy_result)
|
||||
LOGGER.debug('Action=%s, Result=%r => %r', policy_action, policy_result, passing)
|
||||
# LOGGER.debug('Action=%s, Result=%r => %r', policy_action, policy_result, passing)
|
||||
if policy_message:
|
||||
messages.append(policy_message)
|
||||
if not passing:
|
||||
|
||||
@ -299,7 +299,7 @@ with CONFIG.cd('log'):
|
||||
},
|
||||
'django': {
|
||||
'handlers': ['queue'],
|
||||
'level': 'DEBUG',
|
||||
'level': 'INFO',
|
||||
'propagate': True,
|
||||
},
|
||||
'tasks': {
|
||||
@ -324,7 +324,7 @@ with CONFIG.cd('log'):
|
||||
},
|
||||
'daphne': {
|
||||
'handlers': ['queue'],
|
||||
'level': 'DEBUG',
|
||||
'level': 'INFO',
|
||||
'propagate': True,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user