|
| 1 | +import re |
| 2 | +from flask import g |
| 3 | +from nameko.standalone.rpc import ClusterRpcProxy |
| 4 | +from .connection_pool import ConnectionPool |
| 5 | +from .errors import ( |
| 6 | + BadConfigurationError |
| 7 | +) |
| 8 | + |
| 9 | +class PooledClusterRpcProxy(object): |
| 10 | + |
| 11 | + _pool = None |
| 12 | + _config = None |
| 13 | + |
| 14 | + def __init__(self, config=None): |
| 15 | + if config: |
| 16 | + self.configure(config) |
| 17 | + |
| 18 | + def configure(self, config): |
| 19 | + if not config.get('AMQP_URI'): |
| 20 | + raise BadConfigurationError("Please provide a valid configuration.") |
| 21 | + |
| 22 | + self._config = config |
| 23 | + self._pool = ConnectionPool( |
| 24 | + self._get_nameko_connection, |
| 25 | + initial_connections=config.get('INITIAL_CONNECTIONS', 2), |
| 26 | + max_connections=config.get('MAX_CONNETIONS', 8) |
| 27 | + ) |
| 28 | + |
| 29 | + def _get_nameko_connection(self): |
| 30 | + proxy = ClusterRpcProxy(self._config) |
| 31 | + return proxy.start() |
| 32 | + |
| 33 | + def get_connection(self): |
| 34 | + if not self._pool: |
| 35 | + raise ClusterNotConfiguredError("Please configure your cluster beore requesting a connection.") |
| 36 | + return self._pool.get_connection() |
| 37 | + |
| 38 | + def release_connection(self, connection): |
| 39 | + return self._pool.release_connection(connection) |
| 40 | + |
| 41 | + |
| 42 | +class FlaskPooledClusterRpcProxy(PooledClusterRpcProxy): |
| 43 | + def __init__(self, app=None): |
| 44 | + if app: |
| 45 | + self.init_app(app) |
| 46 | + |
| 47 | + def init_app(self, app): |
| 48 | + config = dict() |
| 49 | + for key, val in app.config.iteritems(): |
| 50 | + match = re.match(r"NAMEKO\_(?P<name>.*)", key) |
| 51 | + if match: |
| 52 | + config[match.group('name')] = val |
| 53 | + self.configure(config) |
| 54 | + app.teardown_appcontext(self._teardown_nameko_connection) |
| 55 | + |
| 56 | + def get_connection(self): |
| 57 | + connection = getattr(g, '_nameko_connection', None) |
| 58 | + if not connection: |
| 59 | + connection = super(FlaskPooledClusterRpcProxy, self).get_connection() |
| 60 | + g._nameko_connection = connection |
| 61 | + return connection |
| 62 | + |
| 63 | + def _teardown_nameko_connection(self, exception): |
| 64 | + connection = getattr(g, '_nameko_connection', None) |
| 65 | + if connection is not None: |
| 66 | + self.release_connection(connection) |
| 67 | + |
| 68 | + def __getattr__(self, name): |
| 69 | + return getattr(self.get_connection(), name) |
| 70 | + |
| 71 | + def __getitem__(self, name): |
| 72 | + return getattr(self.get_connection(), name) |
| 73 | + |
0 commit comments