From 7c5d55e0b9e571266676032667264cbdb09532da Mon Sep 17 00:00:00 2001 From: Azareal Date: Mon, 14 Jun 2021 13:32:07 +1000 Subject: [PATCH] Add Dev.HourDBTimeout unlisted config setting. Shorter duration during conn drop period. Improve TickWatch.DumpElapsed formatting. --- common/site.go | 2 ++ common/tickloop.go | 32 +++++++++++++++++++++++++++----- mssql.go | 4 +++- mysql.go | 1 + pgsql.go | 3 ++- 5 files changed, 35 insertions(+), 7 deletions(-) diff --git a/common/site.go b/common/site.go index 5febbe6a..ddbfc92d 100644 --- a/common/site.go +++ b/common/site.go @@ -161,6 +161,8 @@ type devConfig struct { LogLongTick bool // unlisted setting LogNewLongRoute bool // unlisted setting Log4thLongRoute bool // unlisted setting + + HourDBTimeout bool // unlisted setting } // configHolder is purely for having a big struct to unmarshal data into diff --git a/common/tickloop.go b/common/tickloop.go index 0b1665c0..30391e5c 100644 --- a/common/tickloop.go +++ b/common/tickloop.go @@ -67,13 +67,21 @@ func (l *TickLoop) Loop() { var ErrDBDown = errors.New("The database is down") +func DBTimeout() time.Duration { + if Dev.HourDBTimeout { + return time.Hour + } + //db.SetConnMaxLifetime(time.Second * 60 * 5) // Make this infinite as the temporary lifetime change will purge the stale connections? + return -1 +} + func StartTick() (abort bool) { db := qgen.Builder.GetConn() isDBDown := atomic.LoadInt32(&IsDBDown) if e := db.Ping(); e != nil { // TODO: There's a bit of a race here, but it doesn't matter if this error appears multiple times in the logs as it's capped at three times, we just want to cut it down 99% of the time if isDBDown == 0 { - db.SetConnMaxLifetime(time.Second / 2) // Drop all the connections and start over + db.SetConnMaxLifetime(time.Second / 4) // Drop all the connections and start over LogWarning(e, ErrDBDown.Error()) } atomic.StoreInt32(&IsDBDown, 1) @@ -82,8 +90,7 @@ func StartTick() (abort bool) { if isDBDown == 1 { log.Print("The database is back") } - //db.SetConnMaxLifetime(time.Second * 60 * 5) // Make this infinite as the temporary lifetime change will purge the stale connections? - db.SetConnMaxLifetime(-1) + db.SetConnMaxLifetime(DBTimeout()) atomic.StoreInt32(&IsDBDown, 0) return false } @@ -250,6 +257,7 @@ func (w *TickWatch) DumpElapsed() { elapse := func(name string, bef, v int64) { if bef == 0 || v == 0 { ff("%s: %d\n", v) + return } dur := time.Duration(v - bef) milli := dur.Milliseconds() @@ -267,8 +275,20 @@ func (w *TickWatch) DumpElapsed() { f("Name: " + w.Name + "\n") ff("Start: %d\n", w.Start) elapse("DBCheck", w.Start, w.DBCheck) + if w.DBCheck == 0 { + Log(sb.String()) + return + } elapse("StartHook", w.DBCheck, w.StartHook) + if w.StartHook == 0 { + Log(sb.String()) + return + } elapse("Tasks", w.StartHook, w.Tasks) + if w.Tasks == 0 { + Log(sb.String()) + return + } elapse("EndHook", w.Tasks, w.EndHook) Log(sb.String()) @@ -287,11 +307,13 @@ func (w *TickWatch) Run() { select { case <-w.Ticker.C: // Less noisy logs - if n > 2 && n%2 != 0 { + if n > 20 && n%5 == 0 { + Logf("%d seconds elapsed since tick %s started", 5*n, w.Name) + } else if n > 2 && n%2 != 0 { Logf("%d seconds elapsed since tick %s started", 5*n, w.Name) } if !downOnce && w.DBCheck == 0 { - qgen.Builder.GetConn().SetConnMaxLifetime(time.Second / 2) // Drop all the connections and start over + qgen.Builder.GetConn().SetConnMaxLifetime(time.Second / 4) // Drop all the connections and start over LogWarning(ErrDBDown) atomic.StoreInt32(&IsDBDown, 1) downOnce = true diff --git a/mssql.go b/mssql.go index 6ba315f0..0525d8cd 100644 --- a/mssql.go +++ b/mssql.go @@ -13,7 +13,8 @@ import ( "net/url" "github.com/Azareal/Gosora/common" - "github.com/Azareal/Gosora/query_gen" + c "github.com/Azareal/Gosora/common" + qgen "github.com/Azareal/Gosora/query_gen" _ "github.com/denisenkom/go-mssqldb" ) @@ -52,6 +53,7 @@ func initMSSQL() (err error) { // Only hold connections open for five seconds to avoid accumulating a large number of stale connections //db.SetConnMaxLifetime(5 * time.Second) + db.SetConnMaxLifetime(c.DBTimeout()) // Build the generated prepared statements, we are going to slowly move the queries over to the query generator rather than writing them all by hand, this'll make it easier for us to implement database adapters for other databases like PostgreSQL, MSSQL, SQlite, etc. err = _gen_mssql() diff --git a/mysql.go b/mysql.go index 78df0095..8d9b3d1e 100644 --- a/mysql.go +++ b/mysql.go @@ -45,6 +45,7 @@ func initMySQL() (err error) { // Only hold connections open for five seconds to avoid accumulating a large number of stale connections //db.SetConnMaxLifetime(5 * time.Second) + db.SetConnMaxLifetime(c.DBTimeout()) // Build the generated prepared statements, we are going to slowly move the queries over to the query generator rather than writing them all by hand, this'll make it easier for us to implement database adapters for other databases like PostgreSQL, MSSQL, SQlite, etc. err = _gen_mysql() diff --git a/pgsql.go b/pgsql.go index f365a772..05851237 100644 --- a/pgsql.go +++ b/pgsql.go @@ -9,7 +9,7 @@ import ( "strings" c "github.com/Azareal/Gosora/common" - "github.com/Azareal/Gosora/query_gen" + qgen "github.com/Azareal/Gosora/query_gen" _ "github.com/lib/pq" ) @@ -45,6 +45,7 @@ func initPgsql() (err error) { // Only hold connections open for five seconds to avoid accumulating a large number of stale connections //db.SetConnMaxLifetime(5 * time.Second) + db.SetConnMaxLifetime(c.DBTimeout()) err = _gen_pgsql() if err != nil {