fix: stabilize competition results flow

This commit is contained in:
dahyeon committed 2026-07-13 13:55:08 +00:00
1 parent 3bd3120eb1
commit a88562b208
20 files changed
+411 -65

No files matched your search

@@ -39,6 +39,7 @@ export function resolveMongoConnectionOptions(
export class ConnectionService implements OnModuleInit {
private readonly logger = new Logger(ConnectionService.name);
private connection: mongoose.Connection | null = null;
private connectionPromise: Promise<mongoose.Connection> | null = null;
constructor(private readonly secretService: SecretService) {}
private tournamentModels: Map<string, mongoose.Model<any>> | null = null;
@@ -51,35 +52,49 @@ export class ConnectionService implements OnModuleInit {
return this.connection;
}
const pendingConnection = this.connectionPromise ?? this.openConnection();
this.connectionPromise = pendingConnection;
try {
return await pendingConnection;
} finally {
if (this.connectionPromise === pendingConnection) {
this.connectionPromise = null;
}
}
}
private async openConnection(): Promise<mongoose.Connection> {
const connectionString = await this.secretService.get(
'MONGODB_CONNECTION_STRING',
);
const dbName = resolveMongoDbName();
const appName = resolveMongoAppName();
const connectionOptions = resolveMongoConnectionOptions(dbName, appName);
const defaultConnection = await mongoose
const connection = await mongoose
.createConnection(connectionString, connectionOptions)
.asPromise();
this.connection = defaultConnection;
this.connection = connection;
this.tournamentModels = null;
this.logger.log(
`MongoDB connection created. dbName=${dbName} appName=${appName} autoIndex=${
connectionOptions.autoIndex === true
}`,
);
return this.connection;
return connection;
}
public async getModel<K extends keyof TournamentModelTypes>(
modelKey: K,
): Promise<mongoose.Model<TournamentModelTypes[K]>> {
const connection = await this.getConnection();
if (this.tournamentModels)
return this.tournamentModels.get(modelKey) as mongoose.Model<
TournamentModelTypes[K]
>;
const models = new Map<K, mongoose.Model<TournamentModelTypes[K]>>();
const connection = await this.getConnection();
tournamentModelDefinitions.forEach((modelDefinition) => {
applyMongoosePerformancePlugin(modelDefinition.schema);