From 667b30e5e8cf01628ce0d8242eaa5864f7c987ea Mon Sep 17 00:00:00 2001 From: vrde Date: Fri, 15 Apr 2016 16:08:08 +0200 Subject: [PATCH] Add connection pool to web views --- bigchaindb/web/server.py | 11 ++++++----- bigchaindb/web/views.py | 29 +++++++++++++++-------------- tests/web/conftest.py | 2 +- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/bigchaindb/web/server.py b/bigchaindb/web/server.py index 021ceab4..3001cb21 100644 --- a/bigchaindb/web/server.py +++ b/bigchaindb/web/server.py @@ -8,6 +8,7 @@ import multiprocessing from flask import Flask +from bigchaindb import util from bigchaindb import Bigchain from bigchaindb.web import views import gunicorn.app.base @@ -45,7 +46,7 @@ class StandaloneApplication(gunicorn.app.base.BaseApplication): return self.application -def create_app(debug=False): +def create_app(settings): """Return an instance of the Flask application. Args: @@ -54,8 +55,8 @@ def create_app(debug=False): """ app = Flask(__name__) - app.debug = debug - app.config['bigchain'] = Bigchain() + app.debug = settings.get('debug', False) + app.config['bigchain_pool'] = util.pool(Bigchain, size=settings.get('threads', 4)) app.register_blueprint(views.basic_views, url_prefix='/api/v1') return app @@ -79,8 +80,8 @@ def create_server(settings): if not settings.get('threads'): settings['threads'] = (multiprocessing.cpu_count() * 2) + 1 - debug = settings.pop('debug', False) - app = create_app(debug) + app = create_app(settings) + settings.pop('debug', False) standalone = StandaloneApplication(app, settings) return standalone diff --git a/bigchaindb/web/views.py b/bigchaindb/web/views.py index 53db6f66..cf618fff 100644 --- a/bigchaindb/web/views.py +++ b/bigchaindb/web/views.py @@ -15,12 +15,11 @@ basic_views = Blueprint('basic_views', __name__) @basic_views.record def get_bigchain(state): - bigchain = state.app.config.get('bigchain') + bigchain_pool = state.app.config.get('bigchain_pool') - if bigchain is None: + if bigchain_pool is None: raise Exception('This blueprint expects you to provide ' - 'database access through `bigchain`') - + 'a pool of Bigchain instances called `bigchain_pool`') @basic_views.route('/transactions/') @@ -34,9 +33,11 @@ def get_transaction(tx_id): A JSON string containing the data about the transaction. """ - bigchain = current_app.config['bigchain'] + pool = current_app.config['bigchain_pool'] + + with pool() as bigchain: + tx = bigchain.get_transaction(tx_id) - tx = bigchain.get_transaction(tx_id) return flask.jsonify(**tx) @@ -47,7 +48,7 @@ def create_transaction(): Return: A JSON string containing the data about the transaction. """ - bigchain = current_app.config['bigchain'] + pool = current_app.config['bigchain_pool'] val = {} @@ -55,15 +56,15 @@ def create_transaction(): # set to `application/json` tx = request.get_json(force=True) - if tx['transaction']['operation'] == 'CREATE': - tx = util.transform_create(tx) - tx = bigchain.consensus.sign_transaction( - tx, private_key=bigchain.me_private) + with pool() as bigchain: + if tx['transaction']['operation'] == 'CREATE': + tx = util.transform_create(tx) + tx = bigchain.consensus.sign_transaction(tx, private_key=bigchain.me_private) - if not bigchain.consensus.verify_signature(tx): - val['error'] = 'Invalid transaction signature' + if not bigchain.consensus.verify_signature(tx): + val['error'] = 'Invalid transaction signature' - val = bigchain.write_transaction(tx) + val = bigchain.write_transaction(tx) return flask.jsonify(**tx) diff --git a/tests/web/conftest.py b/tests/web/conftest.py index 099f2fd3..bf1dd697 100644 --- a/tests/web/conftest.py +++ b/tests/web/conftest.py @@ -25,7 +25,7 @@ def app(request, node_config): restore_config(request, node_config) from bigchaindb.web import server - app = server.create_app(debug=True) + app = server.create_app({'debug': True}) return app