Skip to content

Commit 97b5112

Browse files
committed
Update index.ts
1 parent 460c543 commit 97b5112

File tree

1 file changed

+17
-28
lines changed

1 file changed

+17
-28
lines changed

server/index.ts

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,34 @@ import './models/yo';
1111

1212
dotenv.config();
1313

14+
// Global variable to track DB connection status
15+
let isConnected = false;
16+
1417
mongoose.Promise = global.Promise;
1518

1619
// Connect to MongoDB
1720
async function connectToDB() {
21+
if (isConnected) {
22+
console.log('=> Using existing database connection');
23+
return;
24+
}
25+
1826
try {
19-
const connection = await mongoose.connect(process.env.MONGO_URI || 'mongodb://localhost/yo');
27+
const connection = await mongoose.connect(process.env.MONGO_URI || 'mongodb://localhost/yo', {
28+
minPoolSize: 5,
29+
maxPoolSize: 10,
30+
});
31+
isConnected = mongoose.connection.readyState === 1; // Set flag to true if connected
2032
console.log(`Connected to database: ${connection.connection.name}`);
2133
console.log(`Host: ${connection.connection.host}`);
2234
console.log(`Port: ${connection.connection.port}`);
2335
console.log(`Connected at: ${new Date().toLocaleString()}`);
2436
} catch (err) {
2537
console.error('MongoDB connection error:', err);
26-
setTimeout(connectToDB, 3000); // Retry connection
38+
setTimeout(connectToDB, 3000); // Retry connection after 3 seconds
2739
}
2840
}
2941

30-
// Ensure the MongoDB connection events are only registered once
3142
if (mongoose.connection.readyState === 0) {
3243
mongoose.connection.on('disconnected', (err) => {
3344
console.warn(`MongoDB disconnected: ${err}`);
@@ -50,33 +61,9 @@ var limiter = RateLimit({
5061
const app = express();
5162
app.use(express.json());
5263
app.set('trust proxy', 1);
53-
app.use(limiter); // apply rate limiter to all requests
64+
app.use(limiter);
5465
app.use(yoRoutes);
5566

56-
interface ServerToClientEvents {
57-
noArg: () => void;
58-
basicEmit: (a: number, b: string, c: Buffer) => void;
59-
withAck: (d: string, callback: (e: number) => void) => void;
60-
61-
// Custom events for your application
62-
allYos: (data: any[]) => void;
63-
liveYos: (data: any[]) => void;
64-
popYos: (data: any[]) => void;
65-
}
66-
67-
interface ClientToServerEvents {
68-
hello: () => void;
69-
}
70-
71-
interface InterServerEvents {
72-
ping: () => void;
73-
}
74-
75-
interface SocketData {
76-
name: string;
77-
age: number;
78-
}
79-
8067
// Wrap the Express app with serverless-http for AWS Lambda
8168
const serverlessApp = serverless(app);
8269

@@ -85,6 +72,8 @@ export const handler = async (
8572
event: APIGatewayProxyEventV2,
8673
context: Context
8774
): Promise<APIGatewayProxyResultV2<string>> => {
75+
context.callbackWaitsForEmptyEventLoop = false; // Allows Lambda to reuse the database connection
76+
8877
try {
8978
// Connect to DB if not already connected
9079
if (mongoose.connection.readyState === 0) {

0 commit comments

Comments
 (0)