Skip to content

Commit

Permalink
fix: fix the error of database is locked when use sqlite (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
Li4n0 authored Sep 13, 2022
1 parent 3fa2cae commit d29b767
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 2 deletions.
9 changes: 7 additions & 2 deletions internal/database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,37 @@ package database
import (
"errors"
"strings"
"sync"

"gorm.io/gorm"
)

var (
DB *gorm.DB
Driver DriverType
Locker sync.Mutex
)

type DriverType = string

const Sqlite = "sqlite"
const Mysql = "mysql"
const Postgres = "postgres"
const UnknowDatabase = "unknown database"
const UnknownDatabase = "unknown database"

func InitDB(dsn string) (err error) {
dbName, dbDsn := dbType(dsn)

switch dbName {
case Sqlite:
DB, err = NewSqlite3(dbDsn)
Driver = Sqlite
case Mysql:
DB, err = NewMysql(dbDsn)
Driver = Mysql
case Postgres:
DB, err = NewPostgres(dbDsn)
Driver = Postgres
default:
err = errors.New("unsupported database")
}
Expand All @@ -45,5 +50,5 @@ func dbType(dsn string) (DriverType, string) {
} else if strings.Contains(dsn, "@tcp") { //兼容上个版本的写法
return Mysql, dsn
}
return UnknowDatabase, dsn
return UnknownDatabase, dsn
}
7 changes: 7 additions & 0 deletions pkg/dns/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ func newRecord(rule *Rule, flag, domain, remoteIp, ipArea string) (r *Record, er
Domain: domain,
Rule: *rule,
}

// sqlite db-level lock to prevent too much write operation lead to error of `database is locked` #54
if database.Driver == database.Sqlite {
database.Locker.Lock()
defer database.Locker.Unlock()
}

return r, database.DB.Create(r).Error
}

Expand Down
7 changes: 7 additions & 0 deletions pkg/ftp/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ func NewRecord(rule *Rule, flag, user, password, method, path, ip, area string,
File: file,
Rule: *rule,
}

// sqlite db-level lock to prevent too much write operation lead to error of `database is locked` #54
if database.Driver == database.Sqlite {
database.Locker.Lock()
defer database.Locker.Unlock()
}

return r, database.DB.Create(r).Error
}

Expand Down
7 changes: 7 additions & 0 deletions pkg/ldap/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ func NewRecord(rule *Rule, flag, path, ip, area string) (r *Record, err error) {
Path: path,
Rule: *rule,
}

// sqlite db-level lock to prevent too much write operation lead to error of `database is locked` #54
if database.Driver == database.Sqlite {
database.Locker.Lock()
defer database.Locker.Unlock()
}

return r, database.DB.Create(r).Error
}

Expand Down
7 changes: 7 additions & 0 deletions pkg/mysql/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ func newRecord(rule *Rule, flag, username, schema, clientName, clientOS, remoteI
Files: files,
Rule: *rule,
}

// sqlite db-level lock to prevent too much write operation lead to error of `database is locked` #54
if database.Driver == database.Sqlite {
database.Locker.Lock()
defer database.Locker.Unlock()
}

return r, database.DB.Create(r).Error
}

Expand Down
7 changes: 7 additions & 0 deletions pkg/rhttp/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ func NewRecord(rule *Rule, flag, method, url, ip, area, raw string) (r *Record,
RawRequest: raw,
Rule: *rule,
}

// sqlite db-level lock to prevent too much write operation lead to error of `database is locked` #54
if database.Driver == database.Sqlite {
database.Locker.Lock()
defer database.Locker.Unlock()
}

return r, database.DB.Create(r).Error
}

Expand Down
7 changes: 7 additions & 0 deletions pkg/rmi/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ func NewRecord(rule *Rule, flag, path, ip, area string) (r *Record, err error) {
Path: path,
Rule: *rule,
}

// sqlite db-level lock to prevent too much write operation lead to error of `database is locked` #54
if database.Driver == database.Sqlite {
database.Locker.Lock()
defer database.Locker.Unlock()
}

return r, database.DB.Create(r).Error
}

Expand Down

0 comments on commit d29b767

Please sign in to comment.