perf(douyu): 拆分任务槽并改造绑定扫码调度
This commit is contained in:
@@ -71,6 +71,21 @@ if not DATABASE_URL.startswith("sqlite"):
|
||||
|
||||
engine = create_engine(DATABASE_URL, **engine_options)
|
||||
|
||||
worker_engine_options = {
|
||||
"connect_args": connect_args,
|
||||
"echo": False,
|
||||
}
|
||||
if not DATABASE_URL.startswith("sqlite"):
|
||||
worker_engine_options.update(
|
||||
pool_pre_ping=True,
|
||||
pool_size=_env_int("WORKER_DB_POOL_SIZE", 12),
|
||||
max_overflow=_env_int("WORKER_DB_MAX_OVERFLOW", 4),
|
||||
pool_timeout=_env_int("WORKER_DB_POOL_TIMEOUT", 30),
|
||||
pool_recycle=_env_int("DB_POOL_RECYCLE", 1800),
|
||||
)
|
||||
|
||||
worker_engine = create_engine(DATABASE_URL, **worker_engine_options)
|
||||
|
||||
|
||||
if DATABASE_URL.startswith("sqlite"):
|
||||
|
||||
@@ -82,6 +97,14 @@ if DATABASE_URL.startswith("sqlite"):
|
||||
cursor.execute("PRAGMA busy_timeout=30000")
|
||||
cursor.execute("PRAGMA foreign_keys=ON")
|
||||
cursor.close()
|
||||
|
||||
@event.listens_for(worker_engine, "connect")
|
||||
def _set_worker_sqlite_pragmas(dbapi_connection, connection_record):
|
||||
cursor = dbapi_connection.cursor()
|
||||
cursor.execute("PRAGMA journal_mode=WAL")
|
||||
cursor.execute("PRAGMA busy_timeout=30000")
|
||||
cursor.execute("PRAGMA foreign_keys=ON")
|
||||
cursor.close()
|
||||
else:
|
||||
|
||||
@event.listens_for(engine, "connect")
|
||||
@@ -96,8 +119,24 @@ else:
|
||||
finally:
|
||||
cursor.close()
|
||||
|
||||
@event.listens_for(worker_engine, "connect")
|
||||
def _set_worker_mysql_session_guards(dbapi_connection, connection_record):
|
||||
cursor = dbapi_connection.cursor()
|
||||
try:
|
||||
cursor.execute(
|
||||
"SET SESSION innodb_lock_wait_timeout = "
|
||||
f"{_env_int('DB_LOCK_WAIT_TIMEOUT', 10)}"
|
||||
)
|
||||
finally:
|
||||
cursor.close()
|
||||
|
||||
|
||||
SessionLocal = sessionmaker(bind=engine, autocommit=False, autoflush=False)
|
||||
WorkerSessionLocal = sessionmaker(
|
||||
bind=worker_engine,
|
||||
autocommit=False,
|
||||
autoflush=False,
|
||||
)
|
||||
Base = declarative_base()
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user