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 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

View File

@ -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/<tx_id>')
@ -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)

View File

@ -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