Skip to content

Commit

Permalink
Add pooling for db connections
Browse files Browse the repository at this point in the history
  • Loading branch information
Pansysk75 committed Dec 31, 2023
1 parent 57365b4 commit 583209c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 24 deletions.
12 changes: 6 additions & 6 deletions api-server/app/Build/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,36 @@
# Get specific build
@build_bp.route('/build/<int:build_id>')
def get_build(build_id):
db_connection = database.ensure_app_connection(current_app)
db_connection = database.get_connection(current_app)
build = Build.get(db_connection, build_id)
return build if build else (jsonify({"error": "Build not found"}), 404)

# Post new build
@build_bp.route('/build', methods=['POST'])
def post_build():
db_connection = database.ensure_app_connection(current_app)
db_connection = database.get_connection(current_app)
new_build = request.get_json()
build_id = Build.post(db_connection, new_build)
return jsonify(build_id)

# Get all builds
@build_bp.route('/builds')
def get_builds():
db_connection = database.ensure_app_connection(current_app)
db_connection = database.get_connection(current_app)
builds = Builds.get(db_connection)
return builds # at worst this will be an empty list

# Get build ratings
@build_bp.route('/build/<int:build_id>/ratings')
def get_build_ratings(build_id):
db_connection = database.ensure_app_connection(current_app)
db_connection = database.get_connection(current_app)
ratings = BuildRating.get(db_connection, build_id)
return jsonify(ratings)

# Post new build rating
@build_bp.route('/build/<int:build_id>/ratings', methods=['POST'])
def post_build_rating(build_id):
db_connection = database.ensure_app_connection(current_app)
db_connection = database.get_connection(current_app)
new_rating = request.get_json()
new_rating["Build_id"] = build_id
result = BuildRating.post(db_connection, new_rating)
Expand All @@ -45,7 +45,7 @@ def post_build_rating(build_id):
# Delete build rating
@build_bp.route('/build/<int:build_id>/ratings', methods=['DELETE'])
def delete_build_rating(build_id):
db_connection = database.ensure_app_connection(current_app)
db_connection = database.get_connection(current_app)
Username_and_Build_id = request.get_json()
Username_and_Build_id["Build_id"] = build_id
result = BuildRating.delete(db_connection, Username_and_Build_id)
Expand Down
14 changes: 7 additions & 7 deletions api-server/app/Components/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,49 @@
# Get all gpus
@components_bp.route('/components/gpus', methods=['GET'])
def get_gpus():
db_connection = database.ensure_app_connection(current_app)
db_connection = database.get_connection(current_app)
gpus = Components.get_gpus(db_connection)
return jsonify(gpus)

# Get all cpus
@components_bp.route('/components/cpus', methods=['GET'])
def get_cpus():
db_connection = database.ensure_app_connection(current_app)
db_connection = database.get_connection(current_app)
cpus = Components.get_cpus(db_connection)
return jsonify(cpus)

# Get all mobos
@components_bp.route('/components/mobos', methods=['GET'])
def get_mobos():
db_connection = database.ensure_app_connection(current_app)
db_connection = database.get_connection(current_app)
mobos = Components.get_mobos(db_connection)
return jsonify(mobos)

# Get all rams
@components_bp.route('/components/rams', methods=['GET'])
def get_rams():
db_connection = database.ensure_app_connection(current_app)
db_connection = database.get_connection(current_app)
rams = Components.get_rams(db_connection)
return jsonify(rams)

# Get all psus
@components_bp.route('/components/psus', methods=['GET'])
def get_psus():
db_connection = database.ensure_app_connection(current_app)
db_connection = database.get_connection(current_app)
psus = Components.get_psus(db_connection)
return jsonify(psus)

# Get all cases
@components_bp.route('/components/cases', methods=['GET'])
def get_cases():
db_connection = database.ensure_app_connection(current_app)
db_connection = database.get_connection(current_app)
cases = Components.get_cases(db_connection)
return jsonify(cases)

# Get all storages
@components_bp.route('/components/storages', methods=['GET'])
def get_storages():
db_connection = database.ensure_app_connection(current_app)
db_connection = database.get_connection(current_app)
storages = Components.get_storages(db_connection)
return jsonify(storages)

12 changes: 6 additions & 6 deletions api-server/app/User/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
# Get specific user from username, and all build_ids associated with that user
@user_bp.route('/user/<string:username>')
def get_user(username):
db_connection = database.ensure_app_connection(current_app)
db_connection = database.get_connection(current_app)
user = User.get(db_connection, username)
return user if user else (jsonify({"error": "User not found"}), 404)

# Create new user
@user_bp.route('/user', methods=['POST'])
def post_user():
db_connection = database.ensure_app_connection(current_app)
db_connection = database.get_connection(current_app)
new_user = User.post(db_connection, request.json)
return new_user if new_user else (jsonify({"error": "Username already taken"}), 409)

Expand All @@ -25,27 +25,27 @@ def post_user():
# # Delete user
# @user_bp.route('/user/<string:username>', methods=['DELETE'])
# def delete_user(username):
# db_connection = database.ensure_app_connection(current_app)
# db_connection = database.get_connection(current_app)
# deleted_user = User.delete(db_connection, username)
# return deleted_user if deleted_user else (jsonify({"error": "User not found"}), 404)

# Get all favorites of a user
@user_bp.route('/user/<string:username>/favorites')
def get_user_favorites(username):
db_connection = database.ensure_app_connection(current_app)
db_connection = database.get_connection(current_app)
favorites = UserFavorite.get(db_connection, username)
return jsonify(favorites)

# Add build to user favorites
@user_bp.route('/user/<string:username>/favorites', methods=['POST'])
def post_user_favorite(username):
db_connection = database.ensure_app_connection(current_app)
db_connection = database.get_connection(current_app)
result = UserFavorite.post(db_connection, username, request.json["Build_id"])
return jsonify(result) if result else (jsonify({"error": "Favorite already exists"}), 409)

# Delete build from user favorites
@user_bp.route('/user/<string:username>/favorites', methods=['DELETE'])
def delete_user_favorite(username):
db_connection = database.ensure_app_connection(current_app)
db_connection = database.get_connection(current_app)
result = UserFavorite.delete(db_connection, username, request.json["Build_id"])
return jsonify(result) if result else (jsonify({"error": "Favorite not found"}), 404)
15 changes: 10 additions & 5 deletions api-server/app/database.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
from dbutils.pooled_db import PooledDB
import pymysql

def connect_app(app, config):
print(f"Connecting to database {config['database']} on {config['host']}:{config['port']} as {config['username']}")
app.config['db_connection'] = pymysql.connect(

app.config['db_pool'] = PooledDB(
creator=pymysql, # the module to use for connecting to the database
host=config['host'],
port=config['port'],
user=config['username'],
password=config['password'],
db=config['database'],
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor
cursorclass=pymysql.cursors.DictCursor,
blocking=True, # block and wait for a connection
maxconnections=5 # max number of connections in the pool
)

# Sometimes db disconnects, this function will reconnect if necessary
def ensure_app_connection(app):
def get_connection(app):
try:
app.config['db_connection'].ping(reconnect=True)
return app.config['db_connection']
connection = app.config['db_pool'].connection()
return connection
except AttributeError:
raise Exception("Trying to reconnect to database, but no connection exists")
1 change: 1 addition & 0 deletions api-server/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
DBUtils==3.0.3
Flask==3.0.0
Flask_Cors==4.0.0
PyMySQL==1.1.0

0 comments on commit 583209c

Please sign in to comment.