@@ -11,23 +11,34 @@ import './models/yo';
11
11
12
12
dotenv . config ( ) ;
13
13
14
+ // Global variable to track DB connection status
15
+ let isConnected = false ;
16
+
14
17
mongoose . Promise = global . Promise ;
15
18
16
19
// Connect to MongoDB
17
20
async function connectToDB ( ) {
21
+ if ( isConnected ) {
22
+ console . log ( '=> Using existing database connection' ) ;
23
+ return ;
24
+ }
25
+
18
26
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
20
32
console . log ( `Connected to database: ${ connection . connection . name } ` ) ;
21
33
console . log ( `Host: ${ connection . connection . host } ` ) ;
22
34
console . log ( `Port: ${ connection . connection . port } ` ) ;
23
35
console . log ( `Connected at: ${ new Date ( ) . toLocaleString ( ) } ` ) ;
24
36
} catch ( err ) {
25
37
console . error ( 'MongoDB connection error:' , err ) ;
26
- setTimeout ( connectToDB , 3000 ) ; // Retry connection
38
+ setTimeout ( connectToDB , 3000 ) ; // Retry connection after 3 seconds
27
39
}
28
40
}
29
41
30
- // Ensure the MongoDB connection events are only registered once
31
42
if ( mongoose . connection . readyState === 0 ) {
32
43
mongoose . connection . on ( 'disconnected' , ( err ) => {
33
44
console . warn ( `MongoDB disconnected: ${ err } ` ) ;
@@ -50,33 +61,9 @@ var limiter = RateLimit({
50
61
const app = express ( ) ;
51
62
app . use ( express . json ( ) ) ;
52
63
app . set ( 'trust proxy' , 1 ) ;
53
- app . use ( limiter ) ; // apply rate limiter to all requests
64
+ app . use ( limiter ) ;
54
65
app . use ( yoRoutes ) ;
55
66
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
-
80
67
// Wrap the Express app with serverless-http for AWS Lambda
81
68
const serverlessApp = serverless ( app ) ;
82
69
@@ -85,6 +72,8 @@ export const handler = async (
85
72
event : APIGatewayProxyEventV2 ,
86
73
context : Context
87
74
) : Promise < APIGatewayProxyResultV2 < string > > => {
75
+ context . callbackWaitsForEmptyEventLoop = false ; // Allows Lambda to reuse the database connection
76
+
88
77
try {
89
78
// Connect to DB if not already connected
90
79
if ( mongoose . connection . readyState === 0 ) {
0 commit comments