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

Keepalive #393

Merged
merged 5 commits into from
Aug 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't done as much research here, but wouldn't SetConnMaxLifetime work better (and avoid all this overhead)?

db.DB().SetConnMaxLifetime(1*time.Minute)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

made that the default - we can start there and see if it helps

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