1
0
mirror of https://github.com/juanfont/headscale.git synced 2024-12-20 19:09:07 +01:00

Set all anti-cgo options and add comment

This commit is contained in:
Kristoffer Dalby 2022-02-23 16:15:20 +00:00
parent 67f5c32b49
commit 686e45cf27

16
db.go
View File

@ -2,6 +2,7 @@ package headscale
import ( import (
"errors" "errors"
"time"
"github.com/glebarez/sqlite" "github.com/glebarez/sqlite"
"gorm.io/driver/postgres" "gorm.io/driver/postgres"
@ -81,13 +82,24 @@ func (h *Headscale) openDB() (*gorm.DB, error) {
switch h.dbType { switch h.dbType {
case Sqlite: case Sqlite:
db, err = gorm.Open(sqlite.Open(h.dbString), &gorm.Config{ db, err = gorm.Open(
sqlite.Open(h.dbString+"?_synchronous=1&_journal_mode=WAL"),
&gorm.Config{
DisableForeignKeyConstraintWhenMigrating: true, DisableForeignKeyConstraintWhenMigrating: true,
Logger: log, Logger: log,
}) },
)
db.Exec("PRAGMA foreign_keys=ON")
// The pure Go SQLite library does not handle locking in
// the same way as the C based one and we cant use the gorm
// connection pool as of 2022/02/23.
sqlDB, _ := db.DB() sqlDB, _ := db.DB()
sqlDB.SetMaxIdleConns(1)
sqlDB.SetMaxOpenConns(1) sqlDB.SetMaxOpenConns(1)
sqlDB.SetConnMaxIdleTime(time.Hour)
case Postgres: case Postgres:
db, err = gorm.Open(postgres.Open(h.dbString), &gorm.Config{ db, err = gorm.Open(postgres.Open(h.dbString), &gorm.Config{
DisableForeignKeyConstraintWhenMigrating: true, DisableForeignKeyConstraintWhenMigrating: true,