Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add class State and use state instead of singleton #998

Merged
merged 3 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions ibis-server/app/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from collections.abc import AsyncIterator
from contextlib import asynccontextmanager
from typing import TypedDict
from uuid import uuid4

from asgi_correlation_id import CorrelationIdMiddleware
Expand All @@ -9,17 +10,21 @@
from starlette.responses import PlainTextResponse

from app.config import get_config
from app.mdl.java_engine import JavaEngineConnector, get_java_engine_connector
from app.mdl.java_engine import JavaEngineConnector
from app.middleware import ProcessTimeMiddleware, RequestLogMiddleware
from app.model import ConfigModel, CustomHttpError
from app.routers import v2, v3

get_config().init_logger()


class State(TypedDict):
java_engine_connector: JavaEngineConnector
Comment on lines +23 to +24
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to add some comment or explanation for this class.



@asynccontextmanager
async def lifespan(app: FastAPI) -> AsyncIterator[dict[str, JavaEngineConnector]]:
async with get_java_engine_connector() as java_engine_connector:
async def lifespan(app: FastAPI) -> AsyncIterator[State]:
async with JavaEngineConnector() as java_engine_connector:
yield {"java_engine_connector": java_engine_connector}


Expand Down
15 changes: 1 addition & 14 deletions ibis-server/app/mdl/java_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,7 @@
wren_engine_endpoint = get_config().wren_engine_endpoint


class Singleton(type):
_instances = {}

def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super().__call__(*args, **kwargs)
return cls._instances[cls]


class JavaEngineConnector(metaclass=Singleton):
class JavaEngineConnector:
def __init__(self):
self.client = httpx.AsyncClient(
base_url=wren_engine_endpoint,
Expand Down Expand Up @@ -58,7 +49,3 @@ async def __aenter__(self):

async def __aexit__(self, exc_type, exc_val, exc_tb):
await self.close()


def get_java_engine_connector() -> JavaEngineConnector:
return JavaEngineConnector()
Loading