-
Notifications
You must be signed in to change notification settings - Fork 15
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
BCDA-3710 - Resolve issues with stale db connections #600
Changes from 13 commits
129ef05
ac84100
25d5984
6396f6e
60f4f15
635b243
e503d69
11ee787
cf4dc65
d7ac092
5d1334b
b244e85
ce28188
3f1c431
53c0e9c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/url" | ||
"regexp" | ||
|
@@ -45,6 +46,11 @@ func NewHandler(resources []string, basePath string) *Handler { | |
|
||
h := &Handler{} | ||
|
||
db := database.GetGORMDbConnection() | ||
db.DB().SetMaxOpenConns(utils.GetEnvInt("BCDA_DB_MAX_OPEN_CONNS", 25)) | ||
db.DB().SetMaxIdleConns(utils.GetEnvInt("BCDA_DB_MAX_IDLE_CONNS", 25)) | ||
db.DB().SetConnMaxLifetime(time.Duration(utils.GetEnvInt("BCDA_DB_CONN_MAX_LIFETIME_MIN", 5)) * time.Minute) | ||
|
||
queueDatabaseURL := os.Getenv("QUEUE_DATABASE_URL") | ||
pgxcfg, err := pgx.ParseURI(queueDatabaseURL) | ||
if err != nil { | ||
|
@@ -59,6 +65,49 @@ func NewHandler(resources []string, basePath string) *Handler { | |
log.Fatal(err) | ||
} | ||
|
||
// With que-go locked to pgx v3, we need a mechanism that will allow us to | ||
// discard bad connections in the pgxpool (see: https://github.com/jackc/pgx/issues/494) | ||
// This implementation is based off of the "fix" that is present in v4 | ||
// (see: https://github.com/jackc/pgx/blob/v4.10.0/pgxpool/pool.go#L333) | ||
// Use the same approach to validate the connection associated with the gorm client | ||
Comment on lines
+72
to
+76
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea to link to resources explaining the implementation here |
||
|
||
// If we implement a cleanup function on the handler, | ||
// consider using context.WithCancel() to give us a hook to stop the goroutine | ||
ctx := context.Background() | ||
go func() { | ||
ticker := time.NewTicker(10 * time.Second) | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
ticker.Stop() | ||
log.Debug("Stopping pgxpool checker") | ||
return | ||
case <-ticker.C: | ||
log.Debug("Sending ping") | ||
c, err := pgxpool.Acquire() | ||
if err != nil { | ||
log.Warnf("Failed to acquire connection %s", err.Error()) | ||
} | ||
if err := c.Ping(ctx); err != nil { | ||
log.Warnf("Failed to ping %s", err.Error()) | ||
} | ||
pgxpool.Release(c) | ||
|
||
c1, err := db.DB().Conn(ctx) | ||
if err != nil { | ||
log.Warnf("Failed to acquire connection %s", err.Error()) | ||
continue | ||
} | ||
if err := c1.PingContext(ctx); err != nil { | ||
log.Warnf("Failed to ping %s", err.Error()) | ||
} | ||
if err := c1.Close(); err != nil { | ||
log.Warnf("Failed to close conection %s", err.Error()) | ||
} | ||
} | ||
} | ||
}() | ||
|
||
h.qc = que.NewClient(pgxpool) | ||
|
||
cutoffDuration := time.Duration(utils.GetEnvInt("CCLF_CUTOFF_DATE_DAYS", 45)*24) * time.Hour | ||
|
@@ -71,10 +120,6 @@ func NewHandler(resources []string, basePath string) *Handler { | |
log.Fatalf("Failed to parse RUNOUT_CLAIM_THRU_DATE '%s'. Err: %s", runoutClaimThru, err.Error()) | ||
} | ||
|
||
db := database.GetGORMDbConnection() | ||
db.DB().SetMaxOpenConns(utils.GetEnvInt("BCDA_DB_MAX_OPEN_CONNS", 25)) | ||
db.DB().SetMaxIdleConns(utils.GetEnvInt("BCDA_DB_MAX_IDLE_CONNS", 25)) | ||
db.DB().SetConnMaxLifetime(time.Duration(utils.GetEnvInt("BCDA_DB_CONN_MAX_LIFETIME_MIN", 5)) * time.Minute) | ||
repository := postgres.NewRepository(db) | ||
h.svc = models.NewService(repository, cutoffDuration, utils.GetEnvInt("BCDA_SUPPRESSION_LOOKBACK_DAYS", 60), | ||
runoutCutoffDuration, runoutClaimThruDate, | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still needed to keep this restart here because of issues uncovered in our CI process. It's caused by the database being dropped while the app is initializing.
Removing the TODO since the restart is not related to the restarting of the db instance.