Skip to content

Commit a62374c

Browse files
committed
perf: optimize database connection pool configuration and job scheduling interval
- adjust database connection pool parameters: reduce the maximum number of connections and increase connection lifecycle - add configuration for maximum connection idle time - optimize SQLite connection parameters, add busy_timeout and synchronous mode - add null checks for the update logic of file scanning jobs
1 parent cbe6209 commit a62374c

File tree

4 files changed

+12
-6
lines changed

4 files changed

+12
-6
lines changed

cmd/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ func main() {
168168

169169
// Initialize job layer
170170
fileScanJob := job.NewFileScanJob(fileScanService, storageManager, syncRepo, appLogger, 5*time.Minute)
171-
eventProcessorJob := job.NewEventProcessorJob(appLogger, syncRepo, embeddingProcessService, codegraphProcessor, 15*time.Second, storageManager)
172-
statusCheckerJob := job.NewStatusCheckerJob(embeddingStatusService, storageManager, syncRepo, appLogger, 10*time.Second)
171+
eventProcessorJob := job.NewEventProcessorJob(appLogger, syncRepo, embeddingProcessService, codegraphProcessor, 30*time.Second, storageManager)
172+
statusCheckerJob := job.NewStatusCheckerJob(embeddingStatusService, storageManager, syncRepo, appLogger, 15*time.Second)
173173
eventCleanerJob := job.NewEventCleanerJob(eventRepo, appLogger)
174174
indexCleanJob := job.NewIndexCleanJob(appLogger, indexer, workspaceRepo, storageManager, codebaseEmbeddingRepo, syncRepo, eventRepo)
175175
// Initialize handler layer

internal/config/database.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type DatabaseConfig struct {
1212
MaxOpenConns int `json:"maxOpenConns"` // 最大打开连接数
1313
MaxIdleConns int `json:"maxIdleConns"` // 最大空闲连接数
1414
ConnMaxLifetime time.Duration `json:"connMaxLifetime"` // 连接最大生命周期
15+
ConnMaxIdleTime time.Duration `json:"connMaxIdleTime"` // 连接最大空闲时间
1516
EnableWAL bool `json:"enableWAL"` // 启用WAL模式
1617
EnableForeignKeys bool `json:"enableForeignKeys"` // 启用外键约束
1718
// 分批删除配置
@@ -24,9 +25,10 @@ func DefaultDatabaseConfig() *DatabaseConfig {
2425
return &DatabaseConfig{
2526
DataDir: utils.DbDir,
2627
DatabaseName: "codebase_indexer.db",
27-
MaxOpenConns: 25,
28-
MaxIdleConns: 10,
29-
ConnMaxLifetime: 5 * time.Minute,
28+
MaxOpenConns: 5,
29+
MaxIdleConns: 3,
30+
ConnMaxLifetime: 15 * time.Minute,
31+
ConnMaxIdleTime: 3 * time.Minute, // 连接空闲超过3分钟则关闭
3032
EnableWAL: true,
3133
EnableForeignKeys: true,
3234
BatchDeleteSize: 1000, // 默认每批删除1000条记录

internal/database/manager.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (m *SQLiteManager) Initialize() error {
5959

6060
// 打开数据库连接
6161
// db, err := sql.Open("sqlite3", dbPath+"?_foreign_keys=on&_journal_mode=WAL")
62-
db, err := sql.Open("sqlite", dbPath+"?_foreign_keys=on&_journal_mode=WAL")
62+
db, err := sql.Open("sqlite", dbPath+"?_foreign_keys=on&_journal_mode=WAL&_busy_timeout=5000&_synchronous=NORMAL")
6363
if err != nil {
6464
return err
6565
}
@@ -68,6 +68,7 @@ func (m *SQLiteManager) Initialize() error {
6868
db.SetMaxOpenConns(m.config.MaxOpenConns)
6969
db.SetMaxIdleConns(m.config.MaxIdleConns)
7070
db.SetConnMaxLifetime(m.config.ConnMaxLifetime)
71+
db.SetConnMaxIdleTime(m.config.ConnMaxIdleTime)
7172

7273
// 测试连接
7374
if err := db.Ping(); err != nil {

internal/service/file_scanner.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,9 @@ func (ws *fileScanService) UpdateWorkspaceStats(workspace *model.Workspace) erro
221221
return fmt.Errorf("failed to get codebase config: %w", err)
222222
}
223223
fileNum := len(codebaseConfig.HashTree)
224+
if fileNum == workspace.FileNum {
225+
return nil
226+
}
224227

225228
// 更新工作区文件数量
226229
updateWorkspace := model.Workspace{

0 commit comments

Comments
 (0)