mirror of
https://github.com/yusufipk/OpenFrame.git
synced 2026-09-11 17:46:06 +00:00
chore(infra): gate DB pool debug logs and de-duplicate rate-limit cleanup timer
This commit is contained in:
@@ -5,6 +5,8 @@
|
|||||||
# ============================================================================
|
# ============================================================================
|
||||||
# PostgreSQL connection string
|
# PostgreSQL connection string
|
||||||
DATABASE_URL="postgresql://user:password@localhost:5432/openframe?schema=public"
|
DATABASE_URL="postgresql://user:password@localhost:5432/openframe?schema=public"
|
||||||
|
# Enable PostgreSQL pool connect/acquire debug logs (set to "true" only when debugging)
|
||||||
|
DB_POOL_DEBUG="false"
|
||||||
|
|
||||||
# ============================================================================
|
# ============================================================================
|
||||||
# AUTHENTICATION - NextAuth.js
|
# AUTHENTICATION - NextAuth.js
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ const globalForPool = globalThis as unknown as {
|
|||||||
pgPool: Pool | undefined;
|
pgPool: Pool | undefined;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const isDbPoolDebugEnabled = process.env.DB_POOL_DEBUG === 'true';
|
||||||
|
|
||||||
function createPool(connectionString: string): Pool {
|
function createPool(connectionString: string): Pool {
|
||||||
// Prevent multiple pools from being created during development (Next.js hot reload)
|
// Prevent multiple pools from being created during development (Next.js hot reload)
|
||||||
if (globalForPool.pgPool) {
|
if (globalForPool.pgPool) {
|
||||||
@@ -31,6 +33,7 @@ function createPool(connectionString: string): Pool {
|
|||||||
// Don't crash the app on unexpected pool errors
|
// Don't crash the app on unexpected pool errors
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (isDbPoolDebugEnabled) {
|
||||||
pool.on('connect', () => {
|
pool.on('connect', () => {
|
||||||
console.debug('New database connection established');
|
console.debug('New database connection established');
|
||||||
});
|
});
|
||||||
@@ -38,6 +41,7 @@ function createPool(connectionString: string): Pool {
|
|||||||
pool.on('acquire', () => {
|
pool.on('acquire', () => {
|
||||||
console.debug('Connection acquired from pool');
|
console.debug('Connection acquired from pool');
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// Store pool globally to prevent multiple instances during development
|
// Store pool globally to prevent multiple instances during development
|
||||||
if (process.env.NODE_ENV !== 'production') {
|
if (process.env.NODE_ENV !== 'production') {
|
||||||
|
|||||||
+14
-5
@@ -1,6 +1,12 @@
|
|||||||
import { db } from '@/lib/db';
|
import { db } from '@/lib/db';
|
||||||
import { NextResponse } from 'next/server';
|
import { NextResponse } from 'next/server';
|
||||||
|
|
||||||
|
const RATE_LIMIT_CLEANUP_INTERVAL_MS = 5 * 60 * 1000;
|
||||||
|
|
||||||
|
const globalForRateLimitCleanup = globalThis as unknown as {
|
||||||
|
rateLimitCleanupIntervalStarted?: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
interface RateLimitConfig {
|
interface RateLimitConfig {
|
||||||
windowMs: number; // Time window in milliseconds
|
windowMs: number; // Time window in milliseconds
|
||||||
maxRequests: number; // Max requests per window
|
maxRequests: number; // Max requests per window
|
||||||
@@ -172,12 +178,15 @@ export async function cleanupRateLimits(): Promise<void> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start cleanup interval when the module is loaded (for self-hosted servers)
|
// Start cleanup interval once per process to avoid duplicate scheduling on module reload.
|
||||||
// Cleanup runs every 5 minutes to remove expired rate limit entries
|
if (!globalForRateLimitCleanup.rateLimitCleanupIntervalStarted && typeof setInterval !== 'undefined') {
|
||||||
if (typeof setInterval !== 'undefined') {
|
const interval = setInterval(() => {
|
||||||
setInterval(() => {
|
|
||||||
cleanupRateLimits().catch(console.error);
|
cleanupRateLimits().catch(console.error);
|
||||||
}, 5 * 60 * 1000);
|
}, RATE_LIMIT_CLEANUP_INTERVAL_MS);
|
||||||
|
|
||||||
|
// Avoid keeping Node.js process alive because of housekeeping timers.
|
||||||
|
interval.unref?.();
|
||||||
|
globalForRateLimitCleanup.rateLimitCleanupIntervalStarted = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user