-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinterceptor.py
109 lines (92 loc) · 3.86 KB
/
interceptor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# !/usr/local/python/bin/python
# -*- coding: utf-8 -*-
# (C) Wu Dong, 2021
# All rights reserved
# @Author: 'Wu Dong <[email protected]>'
# @Time: '5/26/21 2:07 PM'
# sys
import functools
import logging
import traceback
import typing as t
# 3p
import grpc
from grpc import ServerInterceptor
from grpc.experimental import wrap_server_method_handler
log = logging.getLogger(__name__)
class TracebackInterceptor(ServerInterceptor):
def __init__( # pylint: disable=super-init-not-called
self,
app,
exc_handler_funcs: t.Dict[t.Type[Exception], t.Callable]
) -> None:
""" Initialize traceback handler interceptor
"""
self.app = app
self.exc_handler_funcs = exc_handler_funcs
def _handler_exception(self, e, context):
""" Default exception Handler
"""
error_context = None
if isinstance(e, NotImplementedError):
error_context = (grpc.StatusCode.UNIMPLEMENTED, "Not implemented")
# 如果底层系统已经设置了异常,则直接返回即可
if context._state.code != grpc.StatusCode.OK and context._state.details:
context.abort(context._state.code, context._state.details)
if error_context is not None:
context.abort(error_context[0], error_context[1])
log.error(traceback.format_exc())
context.abort(grpc.StatusCode.INTERNAL, traceback.format_exc() if self.app.debug else "Internal Server Error")
def _wrapper(self, behavior):
@functools.wraps(behavior)
def wrapper(request, context):
ctx = self.app.request_context(request, context)
try:
ctx.push()
return behavior(request, context)
except Exception as e:
# 通过 '__mro__' 函数依次找到异常的继承关系
for cls in type(e).__mro__:
handler = self.exc_handler_funcs.get(cls)
if handler and callable(handler):
return handler(e, context)
# 默认的系统处理方式
return self._handler_exception(e, context)
finally:
ctx.pop()
return wrapper
def intercept_service(self, continuation, handler_call_details):
return wrap_server_method_handler(self._wrapper, continuation(handler_call_details))
class MiddlewareInterceptor(ServerInterceptor):
def __init__( # pylint: disable=super-init-not-called
self,
before_request_chains: t.List[t.Callable],
after_request_chains: t.List[t.Callable]
) -> None:
""" 中间件拦截器
"""
self.before_request_chains = before_request_chains
self.after_request_chains = after_request_chains
def _wrapper(self, behavior):
@functools.wraps(behavior)
def wrapper(request, context):
# Ignore method request for reflection
method = context._rpc_event.call_details.method
if method.decode() == "/grpc.reflection.v1alpha.ServerReflection/ServerReflectionInfo":
return behavior(request, context)
# 依次处理预请求
for chain in self.before_request_chains:
resp = chain(request, context)
if resp:
return resp
# 实际函数处理
response = behavior(request, context)
# 依次处理响应
for chain in self.after_request_chains:
response = chain(response)
if not response:
raise ValueError("Miss response from after response middleware: %s" % chain.__name__)
return response
return wrapper
def intercept_service(self, continuation, handler_call_details):
return wrap_server_method_handler(self._wrapper, continuation(handler_call_details))