Optimize healthcheck handler to avoid unnecessary ping in prod + add prod-only config for connection reuse

This commit is contained in:
David Dworken 2022-11-26 18:33:54 -08:00
parent a98bff0db8
commit 865ce06b95
No known key found for this signature in database

View File

@ -390,14 +390,6 @@ func addDeletionRequestHandler(ctx context.Context, w http.ResponseWriter, r *ht
} }
func healthCheckHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) { func healthCheckHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
db, err := GLOBAL_DB.DB()
if err != nil {
panic(fmt.Sprintf("failed to get DB: %v", err))
}
err = db.Ping()
if err != nil {
panic(fmt.Sprintf("failed to ping DB: %v", err))
}
if isProductionEnvironment() { if isProductionEnvironment() {
// Check that we have a reasonable looking set of devices/entries in the DB // Check that we have a reasonable looking set of devices/entries in the DB
rows, err := GLOBAL_DB.Raw("SELECT true FROM enc_history_entries LIMIT 1 OFFSET 1000").Rows() rows, err := GLOBAL_DB.Raw("SELECT true FROM enc_history_entries LIMIT 1 OFFSET 1000").Rows()
@ -422,6 +414,15 @@ func healthCheckHandler(ctx context.Context, w http.ResponseWriter, r *http.Requ
EncryptedId: "healthcheck_enc_id", EncryptedId: "healthcheck_enc_id",
ReadCount: 10000, ReadCount: 10000,
})) }))
} else {
db, err := GLOBAL_DB.DB()
if err != nil {
panic(fmt.Sprintf("failed to get DB: %v", err))
}
err = db.Ping()
if err != nil {
panic(fmt.Sprintf("failed to ping DB: %v", err))
}
} }
ok := "OK" ok := "OK"
w.Write([]byte(ok)) w.Write([]byte(ok))
@ -629,6 +630,11 @@ func InitDB() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
if isProductionEnvironment() {
tx.SetMaxIdleConns(10)
tx.SetMaxOpenConns(100)
tx.SetConnMaxLifetime(time.Minute * 30)
}
} }
func decrementVersion(version string) (string, error) { func decrementVersion(version string) (string, error) {