Don't hold on to the API request dbapi

... And only execute the loader once.

We now use a cached dbapi instance, and then set the object
on the request to None, disconnecting the request from the
request from the database, which doesn't need to be held
open any longer than absolutely necessary.

Interestingly... locally, my total unit test time dropped with this
change from an aggregate around 200 seconds to 130-140 seconds. It
seems there was quite a bit of overhead in that call to begin with.

Change-Id: Ibffbc0b14d3b908f772b2cac8c6fedae432b267a
This commit is contained in:
Julia Kreger 2023-08-17 10:13:43 -07:00
parent 08ce71d0f8
commit 9578eb5f79

View File

@ -34,6 +34,10 @@ GLOBAL_REQ_ID = 'openstack.global_request_id'
ID_FORMAT = (r'^req-[a-f0-9]{8}-[a-f0-9]{4}-'
r'[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$')
# Call once, don't call on each request because it is
# a ton of extra overhead.
DBAPI = dbapi.get_instance()
def policy_deprecation_check():
global CHECKED_DEPRECATED_POLICY_ARGS
@ -77,7 +81,12 @@ class DBHook(hooks.PecanHook):
"""Attach the dbapi object to the request so controllers can get to it."""
def before(self, state):
state.request.dbapi = dbapi.get_instance()
state.request.dbapi = DBAPI
def after(self, state):
# Explicitly set to None since we don't need the DB connection
# after we're done processing the request.
state.request.dbapi = None
class ContextHook(hooks.PecanHook):