Skip to content

Commit b300880

Browse files
authored
fix: limit parallel connections to database to 1 (#172)
1 parent 0e8f00e commit b300880

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

internal/models/database.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,6 @@ func ConnectDatabase() error {
3030
Logger: gorm_zerolog.New(),
3131
}
3232

33-
// logger.New(
34-
// golog.New(os.Stdout, "\r\n", golog.LstdFlags),
35-
// logger.Config{
36-
// SlowThreshold: time.Millisecond,
37-
// LogLevel: logger.Error,
38-
// IgnoreRecordNotFoundError: true,
39-
// Colorful: true,
40-
// },
41-
// ),
42-
4333
// Check with database driver to use. If DB_HOST is set, assume postgresql
4434
_, ok := os.LookupEnv("DB_HOST")
4535
if ok {
@@ -66,6 +56,20 @@ func ConnectDatabase() error {
6656
return err
6757
}
6858

59+
sqlDB, err := db.DB()
60+
if err != nil {
61+
return err
62+
}
63+
64+
// Get new connections after one hour
65+
sqlDB.SetConnMaxLifetime(time.Hour)
66+
67+
// This is done to prevent SQLITE_BUSY errors.
68+
// If you have ideas how to improve this, you are very welcome to open an issue or a PR. Thank you!
69+
sqlDB.SetMaxIdleConns(1)
70+
sqlDB.SetMaxOpenConns(1)
71+
6972
DB = db
73+
7074
return nil
7175
}

0 commit comments

Comments
 (0)