Skip to content

Commit

Permalink
Revert "Add request chain middleware support"
Browse files Browse the repository at this point in the history
This reverts commit 38e68d3.
  • Loading branch information
wenzong committed Jan 5, 2016
1 parent 38e68d3 commit e306628
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 55 deletions.
4 changes: 0 additions & 4 deletions zask/__init__.py
Expand Up @@ -3,7 +3,6 @@
__version__ = '1.8'

import gevent
from gevent.local import local

from zask.config import Config
from zask.utils import get_root_path
Expand Down Expand Up @@ -38,9 +37,6 @@ def logger(self):

class LocalContext(object):

def __init__(self):
self.stash = local()

def get_request_cxt(self):
return gevent.getcurrent()

Expand Down
54 changes: 3 additions & 51 deletions zask/ext/zerorpc/__init__.py
Expand Up @@ -11,7 +11,6 @@

import time
import inspect
import uuid

from logging import getLogger, StreamHandler, Formatter, DEBUG, INFO, ERROR
from logging.handlers import TimedRotatingFileHandler
Expand All @@ -23,22 +22,19 @@
from zerorpc.gevent_zmq import logger as gevent_logger
from zerorpc.core import logger as core_logger

from zask import _request_ctx
from zask.logging import debug_handler, production_handler

access_logger = getLogger(__name__)

ACCESS_LOG_FORMAT = (
'[%(asctime)s] - %(access_key)s - %(uuid)s - %(message)s'
'[%(asctime)s] - %(access_key)s - %(message)s'
)

CONFIG_ENDPOINT_MIDDLEWARE = 'file'
CONFIG_CUSTOME_HEADER_MIDDLEWARE = 'header'
ACCESS_LOG_MIDDLEWARE = 'access_log'
REQUEST_CHAIN_MIDDLEWARE = 'uuid'
DEFAULT_MIDDLEWARES = [
CONFIG_CUSTOME_HEADER_MIDDLEWARE,
REQUEST_CHAIN_MIDDLEWARE,
ACCESS_LOG_MIDDLEWARE
]

Expand Down Expand Up @@ -186,45 +182,6 @@ def load_task_context(self, event_header):
raise NoSuchAccessKeyException(event_header.get('access_key'))


class RequestChainMiddleware(object):
"""Generate UUID for requests and store in greenlet's local storage
"""
def __init__(self, app):
self.app = app

def get_uuid(self):
if not hasattr(_request_ctx.stash, 'uuid'):
setattr(_request_ctx.stash, 'uuid', str(uuid.uuid1()))
return _request_ctx.stash.uuid

def set_uuid(self, uuid):
setattr(_request_ctx.stash, 'uuid', uuid)

def clear_uuid(self):
if hasattr(_request_ctx.stash, 'uuid'):
delattr(_request_ctx.stash, 'uuid')

def server_before_exec(self, request_event):
if not request_event.header.get('uuid'):
request_event.header.update({
'uuid': self.get_uuid(),
})
else:
self.set_uuid(request_event.header.get('uuid'))

def server_after_exec(self, request_event, reply_event):
self.clear_uuid()

def server_inspect_exception(self, request_event, reply_event, task_context, exc_infos):
self.clear_uuid()

def client_before_request(self, event):
if not event.header.get('uuid'):
event.header.update({
'uuid': self.get_uuid(),
})


class AccessLogMiddleware(object):

"""This can't be used before initialize the logger.
Expand All @@ -248,17 +205,15 @@ def server_after_exec(self, request_event, reply_event):
request_event.name,
_milli_time() - start)
access_key = request_event.header.get('access_key', None)
uuid = request_event.header.get('uuid', None)
access_logger.info(log, extra={'access_key': access_key, 'uuid': uuid})
access_logger.info(log, extra={'access_key': access_key})

def server_inspect_exception(self, request_event, reply_event, task_context, exc_infos):
exc_type, exc_value, exc_traceback = exc_infos
log = '"%s" - "%s" - ERROR - %s' % (self._class_name,
request_event.name,
exc_type.__name__)
access_key = request_event.header.get('access_key', None)
uuid = request_event.header.get('uuid', None)
access_logger.info(log, extra={'access_key': access_key, 'uuid': uuid})
access_logger.info(log, extra={'access_key': access_key})


class ZeroRPC(object):
Expand Down Expand Up @@ -352,9 +307,6 @@ def _init_zerorpc_context(self):
elif CONFIG_ENDPOINT_MIDDLEWARE in self._middlewares:
context.register_middleware(ConfigEndpointMiddleware(self.app))

if REQUEST_CHAIN_MIDDLEWARE in self._middlewares:
context.register_middleware(RequestChainMiddleware(self.app))

if ACCESS_LOG_MIDDLEWARE in self._middlewares:
context.register_middleware(AccessLogMiddleware(self.app))
_Server.__context__ = _Client.__context__ = context
Expand Down

0 comments on commit e306628

Please sign in to comment.