mirror of
https://github.com/andwati/telecom-platform.git
synced 2026-11-25 13:28:47 +00:00
feat: Add database connection pool optimization, statistics monitoring, and platform model migrations
- Update connection pool config with SetMaxIdleConns(25), SetConnMaxIdleTime(30min), and descriptive comments - Add AutoMigrate for Plugin/Automation/AutomationRun/ConfigEntry/DeploymentRecord/ChaosExperimentRecord models - Add PoolStats method with max_open_connections/open_connections/in_use/idle/wait_count/wait_duration/max_idle_closed/max_idle_time_closed/max_lifetime_closed fields - Add Ping method
This commit is contained in:
@@ -47,10 +47,11 @@ func NewDatabase(cfg *config.DatabaseConfig) (*Database, error) {
|
|||||||
return nil, fmt.Errorf("failed to get underlying sql.DB: %w", err)
|
return nil, fmt.Errorf("failed to get underlying sql.DB: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Configure connection pool
|
// Configure connection pool for optimal performance
|
||||||
sqlDB.SetMaxIdleConns(10)
|
sqlDB.SetMaxIdleConns(25) // Minimum idle connections
|
||||||
sqlDB.SetMaxOpenConns(100)
|
sqlDB.SetMaxOpenConns(100) // Maximum open connections
|
||||||
sqlDB.SetConnMaxLifetime(time.Hour)
|
sqlDB.SetConnMaxLifetime(time.Hour) // Max connection lifetime
|
||||||
|
sqlDB.SetConnMaxIdleTime(30 * time.Minute) // Max idle time before closing
|
||||||
|
|
||||||
database := &Database{DB: db}
|
database := &Database{DB: db}
|
||||||
|
|
||||||
@@ -74,6 +75,12 @@ func (d *Database) AutoMigrate() error {
|
|||||||
&models.Session{},
|
&models.Session{},
|
||||||
&models.UsageRecord{},
|
&models.UsageRecord{},
|
||||||
&IMSIAllocation{},
|
&IMSIAllocation{},
|
||||||
|
&models.Plugin{},
|
||||||
|
&models.Automation{},
|
||||||
|
&models.AutomationRun{},
|
||||||
|
&models.ConfigEntry{},
|
||||||
|
&models.DeploymentRecord{},
|
||||||
|
&models.ChaosExperimentRecord{},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,6 +116,35 @@ func (d *Database) Close() error {
|
|||||||
return sqlDB.Close()
|
return sqlDB.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PoolStats returns current connection pool statistics
|
||||||
|
func (d *Database) PoolStats() (map[string]any, error) {
|
||||||
|
sqlDB, err := d.DB.DB()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
stats := sqlDB.Stats()
|
||||||
|
return map[string]any{
|
||||||
|
"max_open_connections": stats.MaxOpenConnections,
|
||||||
|
"open_connections": stats.OpenConnections,
|
||||||
|
"in_use": stats.InUse,
|
||||||
|
"idle": stats.Idle,
|
||||||
|
"wait_count": stats.WaitCount,
|
||||||
|
"wait_duration": stats.WaitDuration.String(),
|
||||||
|
"max_idle_closed": stats.MaxIdleClosed,
|
||||||
|
"max_idle_time_closed": stats.MaxIdleTimeClosed,
|
||||||
|
"max_lifetime_closed": stats.MaxLifetimeClosed,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ping verifies database connectivity
|
||||||
|
func (d *Database) Ping(ctx context.Context) error {
|
||||||
|
sqlDB, err := d.DB.DB()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return sqlDB.PingContext(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
func (d *Database) CreateSubscriber(ctx context.Context, subscriber *models.Subscriber) error {
|
func (d *Database) CreateSubscriber(ctx context.Context, subscriber *models.Subscriber) error {
|
||||||
return d.DB.WithContext(ctx).Create(subscriber).Error
|
return d.DB.WithContext(ctx).Create(subscriber).Error
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user