refactoring: full refactoring the database structure from 1 file to 2 file db struct to avoid conflict 2 writters and sqllite busy, improve tests
Some checks failed
build / build (push) Has been cancelled

This commit is contained in:
d3m0k1d
2026-01-22 20:29:19 +03:00
parent 9a7e5a4796
commit 5f607d0be0
13 changed files with 989 additions and 364 deletions

View File

@@ -4,7 +4,7 @@ import (
"time"
)
func Write(db *DB, resultCh <-chan *LogEntry) {
func Write(db *Request_Writer, resultCh <-chan *LogEntry) {
db.logger.Info("Starting log writer")
const batchSize = 100
const flushInterval = 1 * time.Second
@@ -28,17 +28,15 @@ func Write(db *DB, resultCh <-chan *LogEntry) {
"INSERT INTO requests (service, ip, path, method, status, created_at) VALUES (?, ?, ?, ?, ?, ?)",
)
if err != nil {
err := tx.Rollback()
if err != nil {
db.logger.Error("Failed to rollback transaction", "error", err)
}
db.logger.Error("Failed to prepare statement", "error", err)
if rollbackErr := tx.Rollback(); rollbackErr != nil {
db.logger.Error("Failed to rollback transaction", "error", rollbackErr)
}
return
}
defer func() {
err := stmt.Close()
if err != nil {
db.logger.Error("Failed to close statement", "error", err)
if closeErr := stmt.Close(); closeErr != nil {
db.logger.Error("Failed to close statement", "error", closeErr)
}
}()
@@ -58,10 +56,10 @@ func Write(db *DB, resultCh <-chan *LogEntry) {
if err := tx.Commit(); err != nil {
db.logger.Error("Failed to commit transaction", "error", err)
} else {
db.logger.Debug("Flushed batch", "count", len(batch))
return
}
db.logger.Debug("Flushed batch", "count", len(batch))
batch = batch[:0]
}