Skip to content

Commit

Permalink
Merge pull request #110 from aws-containers/catalog-db-timeout
Browse files Browse the repository at this point in the history
feat: Added parameter to control catalog database connect timeout
  • Loading branch information
niallthomson authored Jan 10, 2023
2 parents f11a565 + a68397d commit 3452515
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/catalog/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ The following environment variables are available for configuring the service:
| DB_USER | The username for authenticating to the database | `catalog_user` |
| DB_PASSWORD | The password for authenticating to the database | |
| DB_MIGRATE | Whether to run the database migration function on startup | `true` |
| DB_CONNECT_TIMEOUT | Database connection timeout in seconds | `5` |

## Running

Expand Down
15 changes: 8 additions & 7 deletions src/catalog/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ type AppConfiguration struct {

// DatabaseConfiguration exported
type DatabaseConfiguration struct {
Type string `env:"DB_TYPE,default=mysql"`
Endpoint string `env:"DB_ENDPOINT,default=catalog-db:3306"`
ReadEndpoint string `env:"DB_READ_ENDPOINT"`
Name string `env:"DB_NAME,default=sampledb"`
User string `env:"DB_USER,default=catalog_user"`
Password string `env:"DB_PASSWORD"`
Migrate bool `env:"DB_MIGRATE,default=true"`
Type string `env:"DB_TYPE,default=mysql"`
Endpoint string `env:"DB_ENDPOINT,default=catalog-db:3306"`
ReadEndpoint string `env:"DB_READ_ENDPOINT"`
Name string `env:"DB_NAME,default=sampledb"`
User string `env:"DB_USER,default=catalog_user"`
Password string `env:"DB_PASSWORD"`
Migrate bool `env:"DB_MIGRATE,default=true"`
ConnectTimeout int `env:"DB_CONNECT_TIMEOUT,default=5"`
}
20 changes: 8 additions & 12 deletions src/catalog/repository/mysql_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"fmt"
"log"
"strings"
"time"

"github.com/aws-containers/retail-store-sample-app/catalog/config"
"github.com/aws-containers/retail-store-sample-app/catalog/model"
Expand All @@ -46,11 +45,10 @@ var baseQuery = "SELECT product.product_id AS id, product.name, product.descript
type mySQLRepository struct {
db *sqlx.DB
readerDb *sqlx.DB
//logger log.Logger
}

func newMySQLRepository(config config.DatabaseConfiguration) (Repository, error) {
connectionString := fmt.Sprintf("%s:%s@tcp(%s)/%s", config.User, config.Password, config.Endpoint, config.Name)
connectionString := fmt.Sprintf("%s:%s@tcp(%s)/%s?timeout=%ds", config.User, config.Password, config.Endpoint, config.Name, config.ConnectTimeout)

if config.Migrate {
err := migrateMySQL(connectionString)
Expand All @@ -62,16 +60,15 @@ func newMySQLRepository(config config.DatabaseConfiguration) (Repository, error)

var readerDb *sqlx.DB

db, err := createConnection(connectionString)
db, err := createConnection(config.Endpoint, config.User, config.Password, config.Name, config.ConnectTimeout)
if err != nil {
log.Println("Error: Unable to connect to database", err)
return nil, err
}

if len(config.ReadEndpoint) > 0 {
readerConnectionString := fmt.Sprintf("%s:%s@tcp(%s)/%s", config.User, config.Password, config.ReadEndpoint, config.Name)

readerDb, err = createConnection(readerConnectionString)
readerDb, err = createConnection(config.ReadEndpoint, config.User, config.Password, config.Name, config.ConnectTimeout)
if err != nil {
log.Println("Error: Unable to connect to reader database", err)
return nil, err
Expand All @@ -86,15 +83,15 @@ func newMySQLRepository(config config.DatabaseConfiguration) (Repository, error)
}, nil
}

func createConnection(connectionString string) (*sqlx.DB, error) {
log.Printf("Connecting to %s\n", connectionString)
func createConnection(endpoint string, username string, password string, name string, timeout int) (*sqlx.DB, error) {
log.Printf("Connecting to %s/%s?timeout=%ds", endpoint, name, timeout)

connectionString := fmt.Sprintf("%s:%s@tcp(%s)/%s?timeout=%ds", username, password, endpoint, name, timeout)
db, err := splunksqlx.Open("mysql", connectionString)
if err != nil {
return nil, err
}

// Check if DB connection can be made, only for logging purposes, should not fail/exit
err = db.Ping()
if err != nil {
return nil, err
Expand All @@ -104,6 +101,8 @@ func createConnection(connectionString string) (*sqlx.DB, error) {
}

func migrateMySQL(connectionString string) error {
log.Println("Running database migration...")

m, err := migrate.New(
"file://db/migrations",
"mysql://"+connectionString,
Expand Down Expand Up @@ -155,9 +154,6 @@ func (s *mySQLRepository) List(tags []string, order string, pageNum, pageSize in
products[i].Tags = strings.Split(s.TagString, ",")
}

// DEMO: Change 0 to 850
time.Sleep(0 * time.Millisecond)

products = cut(products, pageNum, pageSize)

return products, nil
Expand Down

0 comments on commit 3452515

Please sign in to comment.