-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
refactor(database): Refactor database connection management to avoid sqlite database is locked #1303
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
PaienNate
wants to merge
5
commits into
OpenListTeam:main
Choose a base branch
from
PaienNate:refactor/database
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
refactor(database): Refactor database connection management to avoid sqlite database is locked #1303
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
20020e9
refactor(database): Refactor database connection management to suppor…
6124862
fix(database): fix copilot notice bug
01d9748
Merge branch 'OpenListTeam:main' into refactor/database
PaienNate a27421e
Merge branch 'main' into refactor/database
PaienNate 0d6622a
Merge branch 'main' into refactor/database
PaienNate File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package dbengine | ||
|
||
import ( | ||
"fmt" | ||
"github.com/OpenListTeam/OpenList/v4/internal/model" | ||
"gorm.io/driver/mysql" | ||
"gorm.io/gorm" | ||
) | ||
|
||
// CreateMysqlCon creates MySQL database connections | ||
func CreateMysqlCon(dsn string, gormConfig *gorm.Config) (con model.Connection, err error) { | ||
var ( | ||
db *gorm.DB | ||
) | ||
|
||
// Create MySQL database connection | ||
db, err = gorm.Open(mysql.Open(dsn), gormConfig) | ||
if err != nil { | ||
return model.Connection{}, fmt.Errorf("cannot create MySQL database connection: %w", err) | ||
} | ||
|
||
// Get underlying database connection for configuration | ||
sqlDB, err := db.DB() | ||
if err != nil { | ||
return model.Connection{}, fmt.Errorf("cannot access underlying MySQL database connection: %w", err) | ||
} | ||
|
||
// Set connection pool parameters | ||
sqlDB.SetMaxOpenConns(100) | ||
sqlDB.SetMaxIdleConns(10) | ||
sqlDB.SetConnMaxLifetime(0) | ||
|
||
// For MySQL, both read and write connections point to the same database instance | ||
return model.Connection{ | ||
Read: db, // Read connection | ||
Write: db, // Write connection | ||
}, nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package dbengine | ||
|
||
import ( | ||
"fmt" | ||
"github.com/OpenListTeam/OpenList/v4/internal/model" | ||
"gorm.io/driver/postgres" | ||
"gorm.io/gorm" | ||
) | ||
|
||
// CreatePostgresCon creates PostgreSQL database connections | ||
func CreatePostgresCon(dsn string, gormConfig *gorm.Config) (con model.Connection, err error) { | ||
var ( | ||
db *gorm.DB | ||
) | ||
|
||
// Create PostgreSQL database connection | ||
db, err = gorm.Open(postgres.Open(dsn), gormConfig) | ||
if err != nil { | ||
return model.Connection{}, fmt.Errorf("cannot create PostgreSQL database connection: %w", err) | ||
} | ||
|
||
// Get underlying database connection for configuration | ||
sqlDB, err := db.DB() | ||
if err != nil { | ||
return model.Connection{}, fmt.Errorf("cannot access underlying PostgreSQL database connection: %w", err) | ||
} | ||
|
||
// Set connection pool parameters | ||
sqlDB.SetMaxOpenConns(100) | ||
sqlDB.SetMaxIdleConns(10) | ||
sqlDB.SetConnMaxLifetime(0) | ||
|
||
// For PostgreSQL, both read and write connections point to the same database instance | ||
return model.Connection{ | ||
Read: db, // Read connection | ||
Write: db, // Write connection | ||
}, nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package dbengine | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
"github.com/OpenListTeam/OpenList/v4/internal/model" | ||
"gorm.io/driver/sqlite" | ||
"gorm.io/gorm" | ||
"runtime" | ||
"strings" | ||
) | ||
|
||
// CreateSqliteCon uses best practices for sqlite and creates two connections | ||
// One optimized for reading and the other optimized for writing | ||
// found the information here: https://kerkour.com/sqlite-for-servers | ||
// copied from https://github.com/bihe/monorepo/blob/477e534bd4c0814cdca73fea774b518148cebd3f/pkg/persistence/sqlite.go#L59 | ||
// with little edit. | ||
// it should solve "database is locked error", and make better performance. | ||
func CreateSqliteCon(dsn string, gormConfig *gorm.Config) (con model.Connection, err error) { | ||
var ( | ||
read *gorm.DB | ||
write *gorm.DB | ||
) | ||
|
||
// Read DB | ||
read, err = gorm.Open(sqlite.Open(dsn), gormConfig) | ||
if err != nil { | ||
return model.Connection{}, fmt.Errorf("cannot create read database connection: %w", err) | ||
} | ||
readDB, err := read.DB() | ||
if err != nil { | ||
return model.Connection{}, fmt.Errorf("cannot access underlying read database connection: %w", err) | ||
} | ||
if !strings.Contains(dsn, ":memory:") && !strings.Contains(dsn, "mode=memory") { | ||
err = setDefaultPragmas(readDB) | ||
} | ||
if err != nil { | ||
return model.Connection{}, err | ||
} | ||
readDB.SetMaxOpenConns(max(4, runtime.NumCPU())) // read in parallel with open connection per core | ||
|
||
// WriteDB | ||
write, err = gorm.Open(sqlite.Open(dsn), gormConfig) | ||
if err != nil { | ||
return model.Connection{}, fmt.Errorf("cannot create write database connection: %w", err) | ||
} | ||
writeDB, err := write.DB() | ||
if err != nil { | ||
return model.Connection{}, fmt.Errorf("cannot access underlying write database connection: %w", err) | ||
} | ||
if !strings.Contains(dsn, ":memory:") && !strings.Contains(dsn, "mode=memory") { | ||
err = setDefaultPragmas(readDB) | ||
} | ||
if err != nil { | ||
return model.Connection{}, err | ||
} | ||
writeDB.SetMaxOpenConns(1) // only use one active connection for writing | ||
|
||
return model.Connection{ | ||
Read: read, | ||
Write: write, | ||
}, nil | ||
} | ||
|
||
// SetDefaultPragmas defines some sqlite pragmas for good performance and litestream compatibility | ||
// https://highperformancesqlite.com/articles/sqlite-recommended-pragmas | ||
// https://litestream.io/tips/ | ||
func setDefaultPragmas(db *sql.DB) error { | ||
var ( | ||
stmt string | ||
val string | ||
) | ||
defaultPragmas := map[string]string{ | ||
"journal_mode": "wal", // https://www.sqlite.org/pragma.html#pragma_journal_mode | ||
"busy_timeout": "5000", // https://www.sqlite.org/pragma.html#pragma_busy_timeout | ||
"synchronous": "1", // NORMAL --> https://www.sqlite.org/pragma.html#pragma_synchronous | ||
"cache_size": "10000", // 10000 pages = 40MB --> https://www.sqlite.org/pragma.html#pragma_cache_size | ||
"foreign_keys": "1", // 1(bool) --> https://www.sqlite.org/pragma.html#pragma_foreign_keys | ||
} | ||
|
||
// set the pragmas | ||
for k := range defaultPragmas { | ||
stmt = fmt.Sprintf("pragma %s = %s", k, defaultPragmas[k]) | ||
if _, err := db.Exec(stmt); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
// validate the pragmas | ||
for k := range defaultPragmas { | ||
row := db.QueryRow(fmt.Sprintf("pragma %s", k)) | ||
err := row.Scan(&val) | ||
if err != nil { | ||
return err | ||
} | ||
if val != defaultPragmas[k] { | ||
return fmt.Errorf("could not set pragma %s to %s", k, defaultPragmas[k]) | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.