Add connection pool to web views

This commit is contained in:
vrde 2016-04-15 16:08:08 +02:00
parent 92ee52bb64
commit 667b30e5e8
No known key found for this signature in database
GPG Key ID: 6581C7C39B3D397D
3 changed files with 22 additions and 20 deletions

View File

@ -8,6 +8,7 @@ import multiprocessing
from flask import Flask from flask import Flask
from bigchaindb import util
from bigchaindb import Bigchain from bigchaindb import Bigchain
from bigchaindb.web import views from bigchaindb.web import views
import gunicorn.app.base import gunicorn.app.base
@ -45,7 +46,7 @@ class StandaloneApplication(gunicorn.app.base.BaseApplication):
return self.application return self.application
def create_app(debug=False): def create_app(settings):
"""Return an instance of the Flask application. """Return an instance of the Flask application.
Args: Args:
@ -54,8 +55,8 @@ def create_app(debug=False):
""" """
app = Flask(__name__) app = Flask(__name__)
app.debug = debug app.debug = settings.get('debug', False)
app.config['bigchain'] = Bigchain() app.config['bigchain_pool'] = util.pool(Bigchain, size=settings.get('threads', 4))
app.register_blueprint(views.basic_views, url_prefix='/api/v1') app.register_blueprint(views.basic_views, url_prefix='/api/v1')
return app return app
@ -79,8 +80,8 @@ def create_server(settings):
if not settings.get('threads'): if not settings.get('threads'):
settings['threads'] = (multiprocessing.cpu_count() * 2) + 1 settings['threads'] = (multiprocessing.cpu_count() * 2) + 1
debug = settings.pop('debug', False) app = create_app(settings)
app = create_app(debug) settings.pop('debug', False)
standalone = StandaloneApplication(app, settings) standalone = StandaloneApplication(app, settings)
return standalone return standalone

View File

@ -15,12 +15,11 @@ basic_views = Blueprint('basic_views', __name__)
@basic_views.record @basic_views.record
def get_bigchain(state): 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 ' raise Exception('This blueprint expects you to provide '
'database access through `bigchain`') 'a pool of Bigchain instances called `bigchain_pool`')
@basic_views.route('/transactions/<tx_id>') @basic_views.route('/transactions/<tx_id>')
@ -34,9 +33,11 @@ def get_transaction(tx_id):
A JSON string containing the data about the transaction. 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) return flask.jsonify(**tx)
@ -47,7 +48,7 @@ def create_transaction():
Return: Return:
A JSON string containing the data about the transaction. A JSON string containing the data about the transaction.
""" """
bigchain = current_app.config['bigchain'] pool = current_app.config['bigchain_pool']
val = {} val = {}
@ -55,15 +56,15 @@ def create_transaction():
# set to `application/json` # set to `application/json`
tx = request.get_json(force=True) tx = request.get_json(force=True)
if tx['transaction']['operation'] == 'CREATE': with pool() as bigchain:
tx = util.transform_create(tx) if tx['transaction']['operation'] == 'CREATE':
tx = bigchain.consensus.sign_transaction( tx = util.transform_create(tx)
tx, private_key=bigchain.me_private) tx = bigchain.consensus.sign_transaction(tx, private_key=bigchain.me_private)
if not bigchain.consensus.verify_signature(tx): if not bigchain.consensus.verify_signature(tx):
val['error'] = 'Invalid transaction signature' val['error'] = 'Invalid transaction signature'
val = bigchain.write_transaction(tx) val = bigchain.write_transaction(tx)
return flask.jsonify(**tx) return flask.jsonify(**tx)

View File

@ -25,7 +25,7 @@ def app(request, node_config):
restore_config(request, node_config) restore_config(request, node_config)
from bigchaindb.web import server from bigchaindb.web import server
app = server.create_app(debug=True) app = server.create_app({'debug': True})
return app return app