mirror of
https://github.com/jupyterhub/jupyterhub.git
synced 2025-10-18 15:33:02 +00:00
use thread-local properties for hub, proxy
This commit is contained in:
@@ -347,7 +347,6 @@ class JupyterHub(Application):
|
|||||||
debug_db = Bool(False, config=True,
|
debug_db = Bool(False, config=True,
|
||||||
help="log all database transactions. This has A LOT of output"
|
help="log all database transactions. This has A LOT of output"
|
||||||
)
|
)
|
||||||
db = Any()
|
|
||||||
session_factory = Any()
|
session_factory = Any()
|
||||||
|
|
||||||
admin_access = Bool(False, config=True,
|
admin_access = Bool(False, config=True,
|
||||||
@@ -541,18 +540,43 @@ class JupyterHub(Application):
|
|||||||
# store the loaded trait value
|
# store the loaded trait value
|
||||||
self.cookie_secret = secret
|
self.cookie_secret = secret
|
||||||
|
|
||||||
_db_local = None
|
# thread-local storage of db objects
|
||||||
|
_local = Instance(threading.local, ())
|
||||||
@property
|
@property
|
||||||
def db(self):
|
def db(self):
|
||||||
if not hasattr(self._db_local, 'db'):
|
if not hasattr(self._local, 'db'):
|
||||||
print("Making new connection", self)
|
self._local.db = scoped_session(self.session_factory)()
|
||||||
self._db_local.db = scoped_session(self.session_factory)()
|
return self._local.db
|
||||||
return self._db_local.db
|
|
||||||
|
@property
|
||||||
|
def hub(self):
|
||||||
|
if not getattr(self._local, 'hub', None):
|
||||||
|
q = self.db.query(orm.Hub)
|
||||||
|
assert q.count() <= 1
|
||||||
|
self._local.hub = q.first()
|
||||||
|
return self._local.hub
|
||||||
|
|
||||||
|
@hub.setter
|
||||||
|
def hub(self, hub):
|
||||||
|
self._local.hub = hub
|
||||||
|
|
||||||
|
@property
|
||||||
|
def proxy(self):
|
||||||
|
if not getattr(self._local, 'proxy', None):
|
||||||
|
q = self.db.query(orm.Proxy)
|
||||||
|
assert q.count() <= 1
|
||||||
|
p = self._local.proxy = q.first()
|
||||||
|
if p:
|
||||||
|
p.auth_token = self.proxy_auth_token
|
||||||
|
return self._local.proxy
|
||||||
|
|
||||||
|
@proxy.setter
|
||||||
|
def proxy(self, proxy):
|
||||||
|
self._local.proxy = proxy
|
||||||
|
|
||||||
def init_db(self):
|
def init_db(self):
|
||||||
"""Create the database connection"""
|
"""Create the database connection"""
|
||||||
self.log.debug("Connecting to db: %s", self.db_url)
|
self.log.debug("Connecting to db: %s", self.db_url)
|
||||||
self._db_local = threading.local()
|
|
||||||
try:
|
try:
|
||||||
self.session_factory = orm.new_session_factory(
|
self.session_factory = orm.new_session_factory(
|
||||||
self.db_url,
|
self.db_url,
|
||||||
|
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
from queue import Queue
|
||||||
from subprocess import Popen
|
from subprocess import Popen
|
||||||
|
|
||||||
from .. import orm
|
from .. import orm
|
||||||
@@ -100,7 +101,15 @@ def test_external_proxy(request, io_loop):
|
|||||||
}))
|
}))
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
assert app.proxy.api_server.port == proxy_port
|
assert app.proxy.api_server.port == proxy_port
|
||||||
assert app.proxy.auth_token == new_auth_token
|
|
||||||
|
# get updated auth token from main thread
|
||||||
|
def get_app_proxy_token():
|
||||||
|
q = Queue()
|
||||||
|
app.io_loop.add_callback(lambda : q.put(app.proxy.auth_token))
|
||||||
|
return q.get(timeout=2)
|
||||||
|
|
||||||
|
assert get_app_proxy_token() == new_auth_token
|
||||||
|
app.proxy.auth_token = new_auth_token
|
||||||
|
|
||||||
# check that the routes are correct
|
# check that the routes are correct
|
||||||
routes = io_loop.run_sync(app.proxy.get_routes)
|
routes = io_loop.run_sync(app.proxy.get_routes)
|
||||||
|
Reference in New Issue
Block a user