Skip to content

Commit

Permalink
chore: add class State and use state instead of singleton (#998)
Browse files Browse the repository at this point in the history
  • Loading branch information
grieve54706 authored Dec 20, 2024
1 parent 2a99990 commit 4c5844a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 17 deletions.
13 changes: 10 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,23 @@
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()


# Define the state of the application
# Use state to store the singleton instance
class State(TypedDict):
java_engine_connector: JavaEngineConnector


@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()

0 comments on commit 4c5844a

Please sign in to comment.