Fix SQLite concurrency issues
A quick test with ApacheBench revealed that SQLite really can't handle multiple concurrent requests with the default settings, due to a locked database. This fixes that by following the suggestions here: https://github.com/mattn/go-sqlite3#faq Testing with ab -n 100 -c 5 http://localhost:8080/blog/post shows that this fixes the issue. But we could improve performance by reducing writes, like what's mentioned in T545. Part of T529
This commit is contained in:
parent
61de04338e
commit
f04469beee
5
app.go
5
app.go
|
@ -442,8 +442,10 @@ func connectToDatabase(app *app) {
|
||||||
var err error
|
var err error
|
||||||
if app.cfg.Database.Type == "mysql" {
|
if app.cfg.Database.Type == "mysql" {
|
||||||
db, err = sql.Open(app.cfg.Database.Type, fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=true&loc=%s", app.cfg.Database.User, app.cfg.Database.Password, app.cfg.Database.Host, app.cfg.Database.Port, app.cfg.Database.Database, url.QueryEscape(time.Local.String())))
|
db, err = sql.Open(app.cfg.Database.Type, fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=true&loc=%s", app.cfg.Database.User, app.cfg.Database.Password, app.cfg.Database.Host, app.cfg.Database.Port, app.cfg.Database.Database, url.QueryEscape(time.Local.String())))
|
||||||
|
db.SetMaxOpenConns(50)
|
||||||
} else if app.cfg.Database.Type == "sqlite3" {
|
} else if app.cfg.Database.Type == "sqlite3" {
|
||||||
db, err = sql.Open("sqlite3", "./writefreely.db?parseTime=true")
|
db, err = sql.Open("sqlite3", "./writefreely.db?parseTime=true&cached=shared")
|
||||||
|
db.SetMaxOpenConns(1)
|
||||||
} else {
|
} else {
|
||||||
log.Error("Invalid database type '%s'. Only 'mysql' and 'sqlite3' are supported right now.", app.cfg.Database.Type)
|
log.Error("Invalid database type '%s'. Only 'mysql' and 'sqlite3' are supported right now.", app.cfg.Database.Type)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
@ -453,7 +455,6 @@ func connectToDatabase(app *app) {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
app.db = &datastore{db, app.cfg.Database.Type}
|
app.db = &datastore{db, app.cfg.Database.Type}
|
||||||
app.db.SetMaxOpenConns(50)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func shutdown(app *app) {
|
func shutdown(app *app) {
|
||||||
|
|
Loading…
Reference in New Issue