Skip to content

Commit

Permalink
Use seperate database connection for dumping
Browse files Browse the repository at this point in the history
  • Loading branch information
jlelse committed Sep 4, 2024
1 parent e210bd7 commit 5910d0d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 25 deletions.
46 changes: 28 additions & 18 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"net/url"
"os"
"runtime"
"strings"
"sync"

Expand All @@ -18,9 +17,8 @@ import (
)

type database struct {
a *goBlog
writeDb *sql.DB
readDb *sql.DB
a *goBlog
writeDb, readDb, dumpDb *sql.DB
// Other things
pc singleflightx.Group[string, []byte] // persistant cache
pcm sync.RWMutex // post creation
Expand All @@ -36,7 +34,8 @@ func (a *goBlog) initDatabase(logging bool) error {
a.info("Initialize database")
}
// Setup db
db, err := a.openDatabase(a.cfg.Db.File, logging)
dumpEnabled := a.cfg.Db.DumpFile != ""
db, err := a.openDatabase(a.cfg.Db.File, logging, dumpEnabled)
if err != nil {
return err
}
Expand All @@ -50,7 +49,7 @@ func (a *goBlog) initDatabase(logging bool) error {
}
})

if a.cfg.Db.DumpFile != "" {
if dumpEnabled {
a.hourlyHooks = append(a.hourlyHooks, func() {
db.dump(a.cfg.Db.DumpFile)
})
Expand All @@ -63,7 +62,7 @@ func (a *goBlog) initDatabase(logging bool) error {
return nil
}

func (a *goBlog) openDatabase(file string, logging bool) (*database, error) {
func (a *goBlog) openDatabase(file string, logging, dump bool) (*database, error) {
file = lo.If(strings.Contains(file, "?"), file+"&").Else(file + "?")
// Register driver
dbDriverName := "goblog_db_" + uuid.NewString()
Expand Down Expand Up @@ -101,8 +100,6 @@ func (a *goBlog) openDatabase(file string, logging bool) (*database, error) {
}
writeDb.SetMaxOpenConns(1)
writeDb.SetMaxIdleConns(1)
writeDb.SetConnMaxIdleTime(0)
writeDb.SetConnMaxLifetime(0)
if err := writeDb.Ping(); err != nil {
return nil, err
}
Expand All @@ -126,18 +123,27 @@ func (a *goBlog) openDatabase(file string, logging bool) (*database, error) {
if err != nil {
return nil, err
}
readDb.SetMaxOpenConns(max(4, runtime.NumCPU()))
readDb.SetMaxIdleConns(max(4, runtime.NumCPU()))
readDb.SetConnMaxIdleTime(0)
readDb.SetConnMaxLifetime(0)
if err := readDb.Ping(); err != nil {
return nil, err
}
// Dump db
var dumpDb *sql.DB
if dump {
dumpDb, err = sql.Open("sqlite3", file+readParams.Encode())
if err != nil {
return nil, err
}
dumpDb.SetMaxIdleConns(0)
if err := dumpDb.Ping(); err != nil {
return nil, err
}
}
// Create custom database struct
return &database{
a: a,
writeDb: writeDb,
readDb: readDb,
dumpDb: dumpDb,
debug: a.cfg.Db != nil && a.cfg.Db.Debug,
}, nil
}
Expand Down Expand Up @@ -210,18 +216,22 @@ func (db *database) dump(file string) {
return
}
defer f.Close()
if err = sqlite3dump.DumpDB(db.readDb, f, sqlite3dump.WithTransaction(false)); err != nil {
if err = sqlite3dump.DumpDB(db.dumpDb, f, sqlite3dump.WithTransaction(false)); err != nil {
db.a.error("Error while dump db", "err", err)
}
}

func (db *database) close() error {
if db == nil || (db.writeDb == nil && db.readDb == nil) {
if db == nil {
return nil
}
writeErr := db.writeDb.Close()
readErr := db.readDb.Close()
return errors.Join(writeErr, readErr)
var errs []error
for _, db := range []*sql.DB{db.writeDb, db.readDb, db.dumpDb} {
if db != nil {
errs = append(errs, db.Close())
}
}
return errors.Join(errs...)
}

func (db *database) rebuildFTSIndex() {
Expand Down
2 changes: 1 addition & 1 deletion database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func Test_database(t *testing.T) {
cfg: &config{},
}

db, err := app.openDatabase("file::memory:?cache=shared", false)
db, err := app.openDatabase("file::memory:?cache=shared", false, false)
if err != nil {
t.Fatalf("Error: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ require (
github.com/emersion/go-sasl v0.0.0-20231106173351-e73c9f7bad43
github.com/emersion/go-smtp v0.21.3
github.com/go-ap/activitypub v0.0.0-20240408091739-ba76b44c2594
github.com/go-ap/client v0.0.0-20240826175954-dd523526a531
github.com/go-ap/client v0.0.0-20240903140120-3e6ae9fe585e
github.com/go-ap/jsonld v0.0.0-20221030091449-f2a191312c73
github.com/go-chi/chi/v5 v5.1.0
github.com/go-fed/httpsig v1.1.0
Expand Down Expand Up @@ -70,7 +70,7 @@ require (
)

require (
git.sr.ht/~mariusor/cache v0.0.0-20240826175759-e4d5dbc2aa25 // indirect
git.sr.ht/~mariusor/cache v0.0.0-20240903182617-9cd70c7b90f5 // indirect
git.sr.ht/~mariusor/go-xsd-duration v0.0.0-20220703122237-02e73435a078 // indirect
git.sr.ht/~mariusor/lw v0.0.0-20240323171419-d538df4af052 // indirect
github.com/andybalholm/cascadia v1.3.2 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ git.jlel.se/jlelse/goldmark-mark v0.0.0-20210522162520-9788c89266a4 h1:p3c/vCY6M
git.jlel.se/jlelse/goldmark-mark v0.0.0-20210522162520-9788c89266a4/go.mod h1:ZFhxwbX+afhgbzh5rpkSJUp6vIduNPtIGDrsWpIcHTE=
git.jlel.se/jlelse/template-strings v0.0.0-20220211095702-c012e3b5045b h1:zrGLEeWzv7bzGRUKsS42akQpszXwEU+8nXV2Z2iDSJM=
git.jlel.se/jlelse/template-strings v0.0.0-20220211095702-c012e3b5045b/go.mod h1:UNLE8cup2GTHbsE89xezRwq3GhKspPI9NyckPbgJEmw=
git.sr.ht/~mariusor/cache v0.0.0-20240826175759-e4d5dbc2aa25 h1:zQWITMIN/2gIzKvHb4ZI8kXOpes+ZdPrJNW0Qr1E6Vw=
git.sr.ht/~mariusor/cache v0.0.0-20240826175759-e4d5dbc2aa25/go.mod h1:Yv1mSKccec0/7Jn75Zx03n7nt2SGccoX9E9jLOA62Hw=
git.sr.ht/~mariusor/cache v0.0.0-20240903182617-9cd70c7b90f5 h1:Ne+MnYUgyjrhTqugAoeImYbH6ic+BphskVLTJnKYaaY=
git.sr.ht/~mariusor/cache v0.0.0-20240903182617-9cd70c7b90f5/go.mod h1:Yv1mSKccec0/7Jn75Zx03n7nt2SGccoX9E9jLOA62Hw=
git.sr.ht/~mariusor/go-xsd-duration v0.0.0-20220703122237-02e73435a078 h1:cliQ4HHsCo6xi2oWZYKWW4bly/Ory9FuTpFPRxj/mAg=
git.sr.ht/~mariusor/go-xsd-duration v0.0.0-20220703122237-02e73435a078/go.mod h1:g/V2Hjas6Z1UHUp4yIx6bATpNzJ7DYtD0FG3+xARWxs=
git.sr.ht/~mariusor/lw v0.0.0-20240323171419-d538df4af052 h1:MKtevO63p4vyFsjjJq6ejop64JQr6OxkpkYK6XmdQZE=
Expand Down Expand Up @@ -67,8 +67,8 @@ github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nos
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/go-ap/activitypub v0.0.0-20240408091739-ba76b44c2594 h1:er3GvGCm7bJwHostjZlsRy7uiUuCquUVF9Fe0TrwiPI=
github.com/go-ap/activitypub v0.0.0-20240408091739-ba76b44c2594/go.mod h1:yRUfFCoZY6C1CWalauqEQ5xYgSckzEBEO/2MBC6BOME=
github.com/go-ap/client v0.0.0-20240826175954-dd523526a531 h1:c37pX4+87Ifp/pmYhkONdN1IXaxw9iIoDgWj8tiwn0w=
github.com/go-ap/client v0.0.0-20240826175954-dd523526a531/go.mod h1:2l/4y8SyLCtdKBcYk/UF2B2PYsEA17qh2f6/S0iOvaU=
github.com/go-ap/client v0.0.0-20240903140120-3e6ae9fe585e h1:mdeTwfLw49/WdYq3kIVlwJd4goaes8tS35tROTHTwhA=
github.com/go-ap/client v0.0.0-20240903140120-3e6ae9fe585e/go.mod h1:y3vCIKDbdc5TkPBYyauaz2p3ur/4k/hrZbx6JRRi7Yo=
github.com/go-ap/errors v0.0.0-20240304112515-6077fa9c17b0 h1:H9MGShwybHLSln6K8RxHPMHiLcD86Lru+5TVW2TcXHY=
github.com/go-ap/errors v0.0.0-20240304112515-6077fa9c17b0/go.mod h1:5x8a6P/dhmMGFxWLcyYlyOuJ2lRNaHGhRv+yu8BaTSI=
github.com/go-ap/jsonld v0.0.0-20221030091449-f2a191312c73 h1:GMKIYXyXPGIp+hYiWOhfqK4A023HdgisDT4YGgf99mw=
Expand Down

0 comments on commit 5910d0d

Please sign in to comment.