From 47858d88ca4ca10ac363c71550b1bafe50f8f4ce Mon Sep 17 00:00:00 2001 From: drebs Date: Thu, 26 Jan 2017 19:18:15 -0200 Subject: [refactor] allow passing threadpool pool for server sync resource Conflicts: server/src/leap/soledad/server/_resource.py testing/tests/server/test__resource.py --- server/pkg/soledad-server | 2 +- server/src/leap/soledad/server/_resource.py | 5 +++-- server/src/leap/soledad/server/_wsgi.py | 13 +++++++------ server/src/leap/soledad/server/auth.py | 13 +++++++++++-- server/src/leap/soledad/server/entrypoint.py | 11 +++++++---- 5 files changed, 29 insertions(+), 15 deletions(-) (limited to 'server') diff --git a/server/pkg/soledad-server b/server/pkg/soledad-server index 92560fa8..450f2277 100644 --- a/server/pkg/soledad-server +++ b/server/pkg/soledad-server @@ -11,7 +11,7 @@ PATH=/sbin:/bin:/usr/sbin:/usr/bin PIDFILE=/var/run/soledad.pid -RESOURCE_CLASS=leap.soledad.server.session.SoledadSession +RESOURCE_CLASS=leap.soledad.server.entrypoint.SoledadEntrypoint HTTPS_PORT=2424 CONFDIR=/etc/soledad CERT_PATH="${CONFDIR}/soledad-server.pem" diff --git a/server/src/leap/soledad/server/_resource.py b/server/src/leap/soledad/server/_resource.py index 89e252d6..4070d786 100644 --- a/server/src/leap/soledad/server/_resource.py +++ b/server/src/leap/soledad/server/_resource.py @@ -19,7 +19,7 @@ A twisted resource that serves the Soledad Server. """ from twisted.web.resource import Resource -from ._wsgi import sync_resource +from ._wsgi import get_sync_resource __all__ = ['SoledadResource'] @@ -31,7 +31,8 @@ class SoledadResource(Resource): for the Soledad Server. """ - def __init__(self): + def __init__(self, sync_pool=None): + sync_resource = get_sync_resource(sync_pool) self.children = {'': sync_resource} def getChild(self, path, request): diff --git a/server/src/leap/soledad/server/_wsgi.py b/server/src/leap/soledad/server/_wsgi.py index 13c8d13b..3e30d560 100644 --- a/server/src/leap/soledad/server/_wsgi.py +++ b/server/src/leap/soledad/server/_wsgi.py @@ -33,7 +33,7 @@ from leap.soledad.common.couch.state import CouchServerState from leap.soledad.common.log import getLogger -__all__ = ['init_couch_state', 'sync_resource'] +__all__ = ['init_couch_state', 'get_sync_resource'] _config = None @@ -76,8 +76,9 @@ def init_couch_state(_app): reactor.stop() -# setup a wsgi resource with its own threadpool -pool = threadpool.ThreadPool() -reactor.callWhenRunning(pool.start) -reactor.addSystemEventTrigger('after', 'shutdown', pool.stop) -sync_resource = WSGIResource(reactor, pool, wsgi_application) +def get_sync_resource(pool=None): + if not pool: + pool = threadpool.ThreadPool() + reactor.callWhenRunning(pool.start) + reactor.addSystemEventTrigger('after', 'shutdown', pool.stop) + return WSGIResource(reactor, pool, wsgi_application) diff --git a/server/src/leap/soledad/server/auth.py b/server/src/leap/soledad/server/auth.py index e616a94f..a5d90c46 100644 --- a/server/src/leap/soledad/server/auth.py +++ b/server/src/leap/soledad/server/auth.py @@ -42,9 +42,13 @@ from ._wsgi import get_config @implementer(IRealm) class SoledadRealm(object): + def __init__(self, sync_pool=None): + self._sync_pool = sync_pool + def requestAvatar(self, avatarId, mind, *interfaces): if IResource in interfaces: - return (IResource, SoledadResource(), lambda: None) + resource = SoledadResource(sync_pool=self._sync_pool) + return (IResource, resource, lambda: None) raise NotImplementedError() @@ -127,5 +131,10 @@ class TokenCredentialFactory(object): raise error.LoginFailed('Invalid credentials') -get_portal = lambda: Portal(SoledadRealm(), [TokenChecker()]) +def get_portal(sync_pool=None): + realm = SoledadRealm(sync_pool=sync_pool) + checker = TokenChecker() + return Portal(realm, [checker]) + + credentialFactory = TokenCredentialFactory() diff --git a/server/src/leap/soledad/server/entrypoint.py b/server/src/leap/soledad/server/entrypoint.py index df2b8934..7501a447 100644 --- a/server/src/leap/soledad/server/entrypoint.py +++ b/server/src/leap/soledad/server/entrypoint.py @@ -20,7 +20,7 @@ The entrypoint for Soledad server. from twisted.internet import reactor from .config import load_configuration -from ._resource import SoledadResource +from ._session import SoledadSession from ._wsgi import init_couch_state @@ -28,10 +28,13 @@ from ._wsgi import init_couch_state conf = load_configuration('/etc/soledad/soledad-server.conf') -class SoledadEntrypoint(SoledadResource): +class SoledadEntrypoint(SoledadSession): - def __init__(self): - SoledadResource.__init__(self, conf) + # the purpose of the entrypoint is to avoid trying to load the + # configuration file during tests. This class will be more useful when we + # add the blobs feature toggle. For now, the whole entrypoint + + pass # see the comments in application.py recarding why couch state has to be -- cgit v1.2.3