Skip to content
This repository was archived by the owner on Jul 17, 2023. It is now read-only.

Commit cf2ac24

Browse files
fix random requests failing.?
bluemap-server.py production ready???
1 parent 5924e58 commit cf2ac24

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

server.py

+5-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
"""
55

66
import gzip
7-
from contextvars import ContextVar
87
from io import BytesIO
98
from typing import TypedDict
109

@@ -58,19 +57,16 @@ class Config(TypedDict):
5857
# https://stackoverflow.com/a/70390426/4784039
5958
is_numeric = lambda x: x.replace('.', '', 1).replace('-', '', 1).isdigit()
6059

61-
_base_model_session_ctx = ContextVar("session")
60+
_sessionmaker = sessionmaker(bind, AsyncSession, expire_on_commit=False)
6261

6362
@app.middleware("request")
6463
async def inject_session(request):
65-
request.ctx.session = sessionmaker(bind, AsyncSession, expire_on_commit=False)()
66-
request.ctx.session_ctx_token = _base_model_session_ctx.set(request.ctx.session)
67-
64+
request.ctx.session = _sessionmaker()
6865

6966
@app.middleware("response")
7067
async def close_session(request, response):
71-
if hasattr(request.ctx, "session_ctx_token"):
72-
_base_model_session_ctx.reset(request.ctx.session_ctx_token)
73-
await request.ctx.session.close()
68+
if session := getattr(request.ctx, "session"):
69+
await session.close()
7470

7571
@app.get("/maps/<world:str>/.rstate")
7672
async def get_render_state(request: sanic.Request, world: str):
@@ -199,4 +195,4 @@ async def get_tile(request: sanic.Request, world: str, lod: int, params: str):
199195
if threads <= 0:
200196
app.run(host=serverbind, port=serverport, fast=True)
201197
else:
202-
app.run(host=serverbind, port=serverport, workers=threads)
198+
app.run(host=serverbind, port=serverport, workers=threads)

0 commit comments

Comments
 (0)