kopia lustrzana https://github.com/bugout-dev/moonstream
Added statement timeout to Postgres connection configuration
rodzic
686bfef49b
commit
8957329df5
|
@ -10,6 +10,7 @@ from sqlalchemy.orm import sessionmaker, Session
|
|||
MOONSTREAM_DB_URI = os.environ.get("MOONSTREAM_DB_URI")
|
||||
if MOONSTREAM_DB_URI is None:
|
||||
raise ValueError("MOONSTREAM_DB_URI environment variable must be set")
|
||||
|
||||
MOONSTREAM_POOL_SIZE_RAW = os.environ.get("MOONSTREAM_POOL_SIZE", 0)
|
||||
try:
|
||||
if MOONSTREAM_POOL_SIZE_RAW is not None:
|
||||
|
@ -19,8 +20,29 @@ except:
|
|||
f"Could not parse MOONSTREAM_POOL_SIZE as int: {MOONSTREAM_POOL_SIZE_RAW}"
|
||||
)
|
||||
|
||||
# https://docs.sqlalchemy.org/en/14/core/pooling.html#sqlalchemy.pool.QueuePool
|
||||
engine = create_engine(MOONSTREAM_DB_URI, pool_size=MOONSTREAM_POOL_SIZE)
|
||||
MOONSTREAM_DB_STATEMENT_TIMEOUT_MILLIS_RAW = os.environ.get(
|
||||
"MOONSTREAM_DB_STATEMENT_TIMEOUT_MILLIS"
|
||||
)
|
||||
MOONSTREAM_DB_STATEMENT_TIMEOUT_MILLIS = 30000
|
||||
try:
|
||||
if MOONSTREAM_DB_STATEMENT_TIMEOUT_MILLIS_RAW is not None:
|
||||
MOONSTREAM_DB_STATEMENT_TIMEOUT_MILLIS = int(
|
||||
MOONSTREAM_DB_STATEMENT_TIMEOUT_MILLIS_RAW
|
||||
)
|
||||
except:
|
||||
raise ValueError(
|
||||
f"MOONSTREAM_DB_STATEMENT_TIMEOUT_MILLIOS must be an integer: {MOONSTREAM_DB_STATEMENT_TIMEOUT_MILLIS_RAW}"
|
||||
)
|
||||
|
||||
# Pooling: https://docs.sqlalchemy.org/en/14/core/pooling.html#sqlalchemy.pool.QueuePool
|
||||
# Statement timeout: https://stackoverflow.com/a/44936982
|
||||
engine = create_engine(
|
||||
MOONSTREAM_DB_URI,
|
||||
pool_size=MOONSTREAM_POOL_SIZE,
|
||||
connect_args={
|
||||
"options": f"-c statement_timeout={MOONSTREAM_DB_STATEMENT_TIMEOUT_MILLIS}"
|
||||
},
|
||||
)
|
||||
SessionLocal = sessionmaker(bind=engine)
|
||||
|
||||
|
||||
|
|
Ładowanie…
Reference in New Issue