Пример #1
0
Файл: h2.py Проект: Krukov/levin
 async def middleware(self, request: Request, handler,
                      call_next) -> Response:
     request.set("add_push", self._create_add_push, lazy=True)
     response: Response = await call_next(request, handler)
     if request.get(self._scope_value):
         response.pushes = request.get(self._scope_value)
     if request.get("push"):
         response.pushes.append(
             _Push(path=request.get("push").format(
                 **request._scope).encode(), ))
     return response
Пример #2
0
 def middleware(request: Request, handler, call_next) -> Response:
     params = inspect.signature(handler).parameters
     kwargs = {}
     for param in params.values():
         if hasattr(param.annotation, "_value_name"):
             kwargs[param.name] = request.get(param.annotation._value_name)
     if kwargs:
         handler = partial(handler, **kwargs)
     return call_next(request, handler)
Пример #3
0
    async def _run_with_profile(self, request: Request, handler, call_next):
        async with self._lock:
            self._targets.remove(_request_hash(request))

            profile = SimpleProfile(depth=request.get("depth", self.depth), memory=self.with_memory)
            profile.add_target(handler)
            handler = profile.trace(handler)
            try:
                return await call_next(request, handler)
            finally:
                self.callback(profile)
Пример #4
0
 async def middleware(self, request: Request, handler, call_next):
     if _request_hash(request) in self._targets and not self._lock.locked():
         return await self._run_with_profile(request, handler, call_next)
     start = self.get_time()
     try:
         return await call_next(request, handler)
     finally:
         if request.get("profile_condition", self.profile_condition)(
             request, self.get_time() - start, self.threshold
         ):
             request.logger.info("add to profile")
             self._targets.append(_request_hash(request))