il2cpp-dump-analyzer-mcp
Version:
Agentic RAG system for analyzing IL2CPP dump.cs files from Unity games
199 lines • 6.53 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SupabaseConnectionManager = void 0;
exports.createConnectionManager = createConnectionManager;
const supabase_js_1 = require("@supabase/supabase-js");
/**
* Enhanced Supabase connection manager with pooling and health monitoring
*/
class SupabaseConnectionManager {
constructor(supabaseUrl, supabaseKey, config = {}) {
this.supabaseUrl = supabaseUrl;
this.supabaseKey = supabaseKey;
this.client = null;
this.healthCheckInterval = null;
this.isHealthy = true;
this.lastHealthCheck = null;
this.config = {
maxConnections: 10,
minConnections: 2,
idleTimeoutMs: 30000,
acquireTimeoutMs: 10000,
enableHealthChecks: true,
healthCheckIntervalMs: 60000,
...config
};
this.stats = {
totalConnections: 0,
activeConnections: 0,
idleConnections: 0,
waitingRequests: 0,
totalAcquired: 0,
totalReleased: 0,
totalCreated: 0,
totalDestroyed: 0
};
this.initialize();
}
/**
* Get singleton instance of connection manager
*/
static getInstance(supabaseUrl, supabaseKey, config) {
if (!SupabaseConnectionManager.instance) {
if (!supabaseUrl || !supabaseKey) {
throw new Error('Supabase URL and key are required for first initialization');
}
SupabaseConnectionManager.instance = new SupabaseConnectionManager(supabaseUrl, supabaseKey, config);
}
return SupabaseConnectionManager.instance;
}
/**
* Initialize the connection manager
*/
async initialize() {
try {
// Create the primary client with optimized settings
this.client = (0, supabase_js_1.createClient)(this.supabaseUrl, this.supabaseKey, {
auth: {
persistSession: false, // Disable session persistence for server-side usage
autoRefreshToken: false
},
db: {
schema: 'public'
},
global: {
headers: {
'x-client-info': 'il2cpp-dump-analyzer-mcp'
}
}
});
this.stats.totalCreated++;
this.stats.totalConnections++;
// Start health checks if enabled
if (this.config.enableHealthChecks) {
this.startHealthChecks();
}
console.log('Supabase connection manager initialized successfully');
}
catch (error) {
console.error('Failed to initialize Supabase connection manager:', error);
throw error;
}
}
/**
* Get a database client
*/
getClient() {
if (!this.client) {
throw new Error('Connection manager not initialized');
}
if (!this.isHealthy) {
throw new Error('Database connection is unhealthy');
}
this.stats.totalAcquired++;
this.stats.activeConnections++;
return this.client;
}
/**
* Release a database client (for compatibility with pooling patterns)
*/
releaseClient() {
this.stats.totalReleased++;
if (this.stats.activeConnections > 0) {
this.stats.activeConnections--;
}
}
/**
* Start health check monitoring
*/
startHealthChecks() {
if (this.healthCheckInterval) {
clearInterval(this.healthCheckInterval);
}
this.healthCheckInterval = setInterval(async () => {
await this.performHealthCheck();
}, this.config.healthCheckIntervalMs);
// Perform initial health check
this.performHealthCheck();
}
/**
* Perform a health check on the database connection
*/
async performHealthCheck() {
try {
if (!this.client) {
this.isHealthy = false;
return;
}
// Simple health check query
const { error } = await this.client
.from('il2cpp_documents')
.select('id')
.limit(1);
this.isHealthy = !error;
this.lastHealthCheck = new Date();
if (error) {
console.warn('Database health check failed:', error);
}
}
catch (error) {
this.isHealthy = false;
this.lastHealthCheck = new Date();
console.warn('Database health check error:', error);
}
}
/**
* Get connection pool statistics
*/
getStats() {
return { ...this.stats };
}
/**
* Get health status
*/
getHealthStatus() {
return {
isHealthy: this.isHealthy,
lastHealthCheck: this.lastHealthCheck,
stats: this.getStats()
};
}
/**
* Cleanup and close connections
*/
async cleanup() {
if (this.healthCheckInterval) {
clearInterval(this.healthCheckInterval);
this.healthCheckInterval = null;
}
// Note: Supabase client doesn't have explicit close method
// but we can mark it as cleaned up
this.client = null;
this.stats.totalDestroyed++;
this.stats.totalConnections = 0;
this.stats.activeConnections = 0;
console.log('Supabase connection manager cleaned up');
}
/**
* Reset singleton instance (useful for testing)
*/
static reset() {
if (SupabaseConnectionManager.instance) {
SupabaseConnectionManager.instance.cleanup();
SupabaseConnectionManager.instance = null;
}
}
}
exports.SupabaseConnectionManager = SupabaseConnectionManager;
/**
* Factory function to create connection manager from environment variables
*/
function createConnectionManager(config) {
const supabaseUrl = process.env.SUPABASE_URL;
const supabaseKey = process.env.SUPABASE_KEY || process.env.SUPABASE_ANON_KEY;
if (!supabaseUrl || !supabaseKey) {
throw new Error('SUPABASE_URL and SUPABASE_KEY environment variables are required');
}
return SupabaseConnectionManager.getInstance(supabaseUrl, supabaseKey, config);
}
//# sourceMappingURL=connection-manager.js.map