Improve session management in rest logic.
This commit is contained in:
parent
1dd241d2c9
commit
1c241d268f
20
api/rest.py
20
api/rest.py
|
@ -11,8 +11,9 @@ import logging
|
||||||
import os
|
import os
|
||||||
import pkg_resources
|
import pkg_resources
|
||||||
import typing as T
|
import typing as T
|
||||||
|
import weakref
|
||||||
|
|
||||||
from aiohttp_session import get_session, Session
|
from aiohttp_session import new_session, get_session, Session
|
||||||
from passlib.hash import bcrypt
|
from passlib.hash import bcrypt
|
||||||
|
|
||||||
from bot_z.async_operator import AsyncOperator, push_to_loop
|
from bot_z.async_operator import AsyncOperator, push_to_loop
|
||||||
|
@ -21,7 +22,9 @@ from api import BASE_URI, DEBUG
|
||||||
|
|
||||||
alog = logging.getLogger("api")
|
alog = logging.getLogger("api")
|
||||||
routes = web.RouteTableDef()
|
routes = web.RouteTableDef()
|
||||||
OPERATORS = {} # type: T.Dict[T.Text, AsyncOperator]
|
OPERATORS = weakref.WeakKeyDictionary(
|
||||||
|
{}
|
||||||
|
) # type: weakref.WeakKeyDictionary[T.Text, AsyncOperator]
|
||||||
BASE_PATH = pkg_resources.resource_filename(__name__, "assets")
|
BASE_PATH = pkg_resources.resource_filename(__name__, "assets")
|
||||||
EXECUTOR = ProcessPoolExecutor()
|
EXECUTOR = ProcessPoolExecutor()
|
||||||
# WARN: the default il 12 rounds; both the server and the client shall compute
|
# WARN: the default il 12 rounds; both the server and the client shall compute
|
||||||
|
@ -54,13 +57,17 @@ async def get_set_operator(
|
||||||
op = None
|
op = None
|
||||||
if "async_operator" in session:
|
if "async_operator" in session:
|
||||||
op = OPERATORS.get(session["async_operator"])
|
op = OPERATORS.get(session["async_operator"])
|
||||||
base_uri = request.app["base_uri"]
|
else:
|
||||||
debug = request.app["debug"]
|
session = await new_session(request)
|
||||||
headless = request.app["headless"]
|
|
||||||
if op is None:
|
if op is None or session.new:
|
||||||
|
base_uri = request.app["base_uri"]
|
||||||
|
debug = request.app["debug"]
|
||||||
|
headless = request.app["headless"]
|
||||||
op = AsyncOperator(base_uri, name=user, headless=headless, debug=debug)
|
op = AsyncOperator(base_uri, name=user, headless=headless, debug=debug)
|
||||||
session["async_operator"] = user
|
session["async_operator"] = user
|
||||||
OPERATORS[user] = op
|
OPERATORS[user] = op
|
||||||
|
|
||||||
return op, session
|
return op, session
|
||||||
|
|
||||||
|
|
||||||
|
@ -135,6 +142,7 @@ async def logout_handler(request: web.Request) -> web.Response:
|
||||||
{"error": "No session", "logged_in": False}, status=401
|
{"error": "No session", "logged_in": False}, status=401
|
||||||
)
|
)
|
||||||
res = await logout(op)
|
res = await logout(op)
|
||||||
|
session.invalidate()
|
||||||
alog.debug("logout result: %s", res)
|
alog.debug("logout result: %s", res)
|
||||||
# FIX: assess if better to invalidate session and dump the browser instance.
|
# FIX: assess if better to invalidate session and dump the browser instance.
|
||||||
del OPERATORS[op_key]
|
del OPERATORS[op_key]
|
||||||
|
|
Loading…
Reference in New Issue
Block a user