Skip to content

Commit

Permalink
feat: make poolclass configurable
Browse files Browse the repository at this point in the history
Default class is QueuePool with default engine options.

If a custom poolclass is set the default engine options are empty and must be set manually.
  • Loading branch information
thinkh committed Aug 23, 2023
1 parent 92fd31c commit c87eb3b
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions tdp_core/dbview.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,15 +623,27 @@ def dump(self, name):
return OrderedDict(name=name, description=self.description)

def create_engine(self, config) -> Engine:
engine_options = {
# Increase the pool size to 30 to avoid "too many clients" errors
"pool_size": 30,
"pool_pre_ping": True,
}
import importlib

poolclass_name = config.get("poolclass", "QueuePool") # set to SQLAlchemy default poolclass = QueuePool
try:
poolclass = getattr(importlib.import_module("sqlalchemy.pool"), poolclass_name)
except AttributeError:
_log.warning("db connector: poolclass %s not found, using default QueuePool", poolclass_name)
poolclass = sqlalchemy.pool.QueuePool

_log.info("db connector: using poolclass %s", poolclass)

# Set some default engine options for QueuePool to be backwards compatible with previous tdp_core code
engine_options = (
{"pool_size": 30, "pool_pre_ping": True} if poolclass_name == "QueuePool" or poolclass == sqlalchemy.pool.QueuePool else {}
)

engine_options.update(config.get("engine", {}))
_log.debug("db connector: create engine with options %s", engine_options)

with tracer.start_as_current_span("DBConnector.create_engine"):
return sqlalchemy.create_engine(self.dburl, **engine_options)
return sqlalchemy.create_engine(self.dburl, poolclass=poolclass, **engine_options)

def create_sessionmaker(self, engine) -> sessionmaker:
with tracer.start_as_current_span("DBConnector.create_sessionmaker"):
Expand Down

0 comments on commit c87eb3b

Please sign in to comment.