Skip to content
This repository has been archived by the owner on Jul 12, 2023. It is now read-only.

Commit

Permalink
Keepalive (#393)
Browse files Browse the repository at this point in the history
* add connection keepalive

* add period DB connection ping calls

* change log messages

* vet

* start w/ idle connection check
  • Loading branch information
mikehelmick authored Aug 27, 2020
1 parent 37e2f1f commit 3edafb7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
22 changes: 12 additions & 10 deletions pkg/database/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package database
import (
"fmt"
"strings"
"time"

"github.com/google/exposure-notifications-server/pkg/keys"
"github.com/google/exposure-notifications-server/pkg/secrets"
Expand All @@ -25,16 +26,17 @@ import (

// Config represents the env var based configuration for database connections.
type Config struct {
Name string `env:"DB_NAME" json:",omitempty"`
User string `env:"DB_USER" json:",omitempty"`
Host string `env:"DB_HOST, default=localhost" json:",omitempty"`
Port string `env:"DB_PORT, default=5432" json:",omitempty"`
SSLMode string `env:"DB_SSLMODE, default=require" json:",omitempty"`
ConnectionTimeout uint `env:"DB_CONNECT_TIMEOUT" json:",omitempty"`
Password string `env:"DB_PASSWORD" json:"-"` // ignored by zap's JSON formatter
SSLCertPath string `env:"DB_SSLCERT" json:",omitempty"`
SSLKeyPath string `env:"DB_SSLKEY" json:",omitempty"`
SSLRootCertPath string `env:"DB_SSLROOTCERT" json:",omitempty"`
Name string `env:"DB_NAME" json:",omitempty"`
User string `env:"DB_USER" json:",omitempty"`
Host string `env:"DB_HOST, default=localhost" json:",omitempty"`
Port string `env:"DB_PORT, default=5432" json:",omitempty"`
SSLMode string `env:"DB_SSLMODE, default=require" json:",omitempty"`
ConnectionTimeout uint `env:"DB_CONNECT_TIMEOUT" json:",omitempty"`
Password string `env:"DB_PASSWORD" json:"-"` // ignored by zap's JSON formatter
SSLCertPath string `env:"DB_SSLCERT" json:",omitempty"`
SSLKeyPath string `env:"DB_SSLKEY" json:",omitempty"`
SSLRootCertPath string `env:"DB_SSLROOTCERT" json:",omitempty"`
MaxConnectionIdleTime time.Duration `env:"DB_MAX_CONN_IDLE_TIME, default=1m" json:",omitempty"`

// Debug is a boolean that indicates whether the database should log SQL
// commands.
Expand Down
4 changes: 4 additions & 0 deletions pkg/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ func (db *Database) OpenWithCacher(ctx context.Context, cacher cache.Cacher) err
return fmt.Errorf("database gorm.Open: %w", err)
}

if c.MaxConnectionIdleTime > 0 {
rawDB.DB().SetConnMaxIdleTime(c.MaxConnectionIdleTime)
}

// Log SQL statements in debug mode.
if c.Debug {
rawDB = rawDB.LogMode(true)
Expand Down
1 change: 1 addition & 0 deletions terraform/services.tf
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ locals {
DB_SSLROOTCERT = "secret://${google_secret_manager_secret_version.db-secret-version["sslrootcert"].id}?target=file"
DB_USER = google_sql_user.user.name
DB_VERIFICATION_CODE_DATABASE_KEY = "secret://${google_secret_manager_secret_version.db-verification-code-hmac.id}"
DB_CONNECTION_KEEPALIVE_TIME = "1m"
}

firebase_config = {
Expand Down

0 comments on commit 3edafb7

Please sign in to comment.