Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/HH-230357' into EXP-98821
Browse files Browse the repository at this point in the history
  • Loading branch information
HH ReleaseBot committed Sep 16, 2024
2 parents 9487dbb + 4ed7709 commit df89a3f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
18 changes: 17 additions & 1 deletion frontik/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import os
import time
from ctypes import c_bool, c_int
from typing import Optional, Union
from typing import Any, Optional, Union

from aiokafka import AIOKafkaProducer
from fastapi import FastAPI, HTTPException
Expand Down Expand Up @@ -52,6 +52,10 @@ def __init__(self) -> None:
self.include_router(_router)


def anyio_noop(*_args: Any, **_kwargs: Any) -> None:
raise RuntimeError(f'trying to use non async {_args[0]}')


class FrontikApplication:
request_id = ''

Expand All @@ -60,6 +64,7 @@ class DefaultConfig:

def __init__(self, app_module_name: Optional[str] = None) -> None:
self.start_time = time.time()
self.patch_anyio()

self.app_module_name: str = app_module_name or self.__class__.__module__
app_module = importlib.import_module(self.app_module_name)
Expand Down Expand Up @@ -93,6 +98,17 @@ def __init__(self, app_module_name: Optional[str] = None) -> None:
self.asgi_app = FrontikAsgiApp()
self.service_discovery: ServiceDiscovery

def patch_anyio(self) -> None:
"""
We have problems with anyio running sync dependencies in threadpool, so sync deps are prohibited
"""
try:
import anyio

anyio.to_thread.run_sync = anyio_noop # type: ignore
except ImportError:
pass

def __call__(self, tornado_request: httputil.HTTPServerRequest) -> None:
# for make it more asgi, reimplement tornado.http1connection._server_request_loop and ._read_message
task = asyncio.create_task(serve_tornado_request(self, self.asgi_app, tornado_request))
Expand Down
8 changes: 0 additions & 8 deletions frontik/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from threading import Lock
from typing import Callable, Optional, Union

import anyio
import tornado.autoreload
from http_client.balancing import Upstream
from tornado.httpserver import HTTPServer
Expand Down Expand Up @@ -178,10 +177,3 @@ async def _deinit_app(app: FrontikApplication) -> None:
await asyncio.sleep(options.stop_timeout)
if app.http_client_factory is not None:
await app.http_client_factory.http_client.client_session.close()


def anyio_noop(*_args, **_kwargs):
raise RuntimeError(f'trying to use non async {_args[0]}')


anyio.to_thread.run_sync = anyio_noop

0 comments on commit df89a3f

Please sign in to comment.