Skip to content

Commit

Permalink
Apply settings on every new sqlite connection in the pool
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbakker committed Apr 1, 2024
1 parent 9cce31d commit 88b79ed
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 26 deletions.
5 changes: 2 additions & 3 deletions cmd/toxstatus/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,8 @@ func startRoot(cmd *cobra.Command, args []string) {
NoColor: !isatty.IsTerminal(os.Stderr.Fd()),
}))

readConn, writeConn, err := db.OpenReadWrite(ctx, rootFlags.DB, db.OpenOptions{
CacheSize: rootFlags.DBCacheSize,
})
db.RegisterPragmaHook(rootFlags.DBCacheSize)
readConn, writeConn, err := db.OpenReadWrite(ctx, rootFlags.DB, db.OpenOptions{})
if err != nil {
logErrorAndExit(logger, "Unable to open db", slog.Any("err", err))
}
Expand Down
44 changes: 23 additions & 21 deletions internal/db/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,30 @@ import (
"fmt"
"net/url"
"runtime"

"github.com/mattn/go-sqlite3"
)

type OpenOptions struct {
CacheSize int
Params map[string]string
Params map[string]string
}

func RegisterPragmaHook(cacheSize int) {
sql.Register("toxstatus_sqlite3", &sqlite3.SQLiteDriver{
ConnectHook: func(c *sqlite3.SQLiteConn) error {
fmt.Println("Executing pragmas")
pragmas := fmt.Sprintf(`
PRAGMA journal_mode = WAL;
PRAGMA busy_timeout = 5000;
PRAGMA synchronous = NORMAL;
PRAGMA cache_size = -%d;
PRAGMA foreign_keys = true;
PRAGMA temp_store = memory;
`, cacheSize)
_, err := c.Exec(pragmas, nil)
return err
},
})
}

func OpenReadWrite(ctx context.Context, dbFile string, opts OpenOptions) (rdb *sql.DB, wdb *sql.DB, err error) {
Expand All @@ -27,16 +46,7 @@ func OpenReadWrite(ctx context.Context, dbFile string, opts OpenOptions) (rdb *s
query.Set("_txlock", "immediate")
uri.RawQuery = query.Encode()

pragmas := fmt.Sprintf(`
PRAGMA journal_mode = WAL;
PRAGMA busy_timeout = 5000;
PRAGMA synchronous = NORMAL;
PRAGMA cache_size = -%d;
PRAGMA foreign_keys = true;
PRAGMA temp_store = memory;
`, opts.CacheSize)

readConn, err := sql.Open("sqlite3", uri.String())
readConn, err := sql.Open("toxstatus_sqlite3", uri.String())
if err != nil {
return nil, nil, err
}
Expand All @@ -47,11 +57,7 @@ func OpenReadWrite(ctx context.Context, dbFile string, opts OpenOptions) (rdb *s
}()
readConn.SetMaxOpenConns(max(4, runtime.NumCPU()))

if _, err = readConn.ExecContext(ctx, pragmas); err != nil {
return nil, nil, fmt.Errorf("configure db conn: %w", err)
}

writeConn, err := sql.Open("sqlite3", uri.String())
writeConn, err := sql.Open("toxstatus_sqlite3", uri.String())
if err != nil {
return nil, nil, err
}
Expand All @@ -62,10 +68,6 @@ func OpenReadWrite(ctx context.Context, dbFile string, opts OpenOptions) (rdb *s
}()
writeConn.SetMaxOpenConns(1)

if _, err = writeConn.ExecContext(ctx, pragmas); err != nil {
return nil, nil, fmt.Errorf("configure db conn: %w", err)
}

if _, err = writeConn.ExecContext(ctx, Schema); err != nil {
return nil, nil, fmt.Errorf("init db: %w", err)
}
Expand Down
7 changes: 5 additions & 2 deletions internal/repo/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ import (

var ctx = context.Background()

func init() {
db.RegisterPragmaHook(2000)
}

func initRepo(t *testing.T) (repo *NodesRepo, close func() error) {
readConn, writeConn, err := db.OpenReadWrite(ctx, ":memory:", db.OpenOptions{
CacheSize: 2000,
Params: map[string]string{"cache": "shared"},
Params: map[string]string{"cache": "shared"},
})
if err != nil {
t.Fatal(err)
Expand Down

0 comments on commit 88b79ed

Please sign in to comment.