Skip to content

Commit

Permalink
Adjust the amount of concurrent postgres connections
Browse files Browse the repository at this point in the history
And respect cgroups when setting GOMAXPROCS
  • Loading branch information
DMarby committed Jun 27, 2020
1 parent 7b71e0b commit edbe175
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 4 deletions.
4 changes: 4 additions & 0 deletions cmd/image-service/main.go
Expand Up @@ -22,6 +22,7 @@ import (
api "github.com/DMarby/picsum-photos/internal/imageapi"

"github.com/jamiealquiza/envy"
"go.uber.org/automaxprocs/maxprocs"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -68,6 +69,9 @@ func main() {
log := logger.New(*loglevel)
defer log.Sync()

// Set GOMAXPROCS
maxprocs.Set(maxprocs.Logger(log.Infof))

// Set up context for shutting down
shutdownCtx, shutdown := context.WithCancel(context.Background())
defer shutdown()
Expand Down
9 changes: 7 additions & 2 deletions cmd/picsum-photos/main.go
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/DMarby/picsum-photos/internal/logger"

"github.com/jamiealquiza/envy"
"go.uber.org/automaxprocs/maxprocs"
"go.uber.org/zap"
)

Expand All @@ -42,7 +43,8 @@ var (
databaseFilePath = flag.String("database-file-path", "./test/fixtures/file/metadata.json", "path to the database file")

// Database - Postgresql
databasePostgresqlAddress = flag.String("database-postgresql-address", "postgresql://postgres@127.0.0.1/postgres", "postgresql address")
databasePostgresqlAddress = flag.String("database-postgresql-address", "postgresql://postgres@127.0.0.1/postgres", "postgresql address")
databasePostgresqlMaxConns = flag.Int("database-postgresql-max-conns", 0, "postgresql max connections")

// HMAC
hmacKey = flag.String("hmac-key", "", "hmac key to use for authentication between services")
Expand All @@ -59,6 +61,9 @@ func main() {
log := logger.New(*loglevel)
defer log.Sync()

// Set GOMAXPROCS
maxprocs.Set(maxprocs.Logger(log.Infof))

// Set up context for shutting down
shutdownCtx, shutdown := context.WithCancel(context.Background())
defer shutdown()
Expand Down Expand Up @@ -144,7 +149,7 @@ func setupBackends() (database database.Provider, err error) {
case "file":
database, err = fileDatabase.New(*databaseFilePath)
case "postgresql":
database, err = postgresql.New(*databasePostgresqlAddress)
database, err = postgresql.New(*databasePostgresqlAddress, *databasePostgresqlMaxConns)
default:
err = fmt.Errorf("invalid database backend")
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -15,6 +15,7 @@ require (
github.com/spf13/cobra v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/twmb/murmur3 v1.1.3
go.uber.org/automaxprocs v1.3.0
go.uber.org/zap v1.15.0
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
golang.org/x/lint v0.0.0-20200302205851-738671d3881b // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Expand Up @@ -355,6 +355,8 @@ go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.6.0 h1:Ezj3JGmsOnG1MoRWQkPBsKLe9DwWD9QeXzTRzzldNVk=
go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
go.uber.org/automaxprocs v1.3.0 h1:II28aZoGdaglS5vVNnspf28lnZpXScxtIozx1lAjdb0=
go.uber.org/automaxprocs v1.3.0/go.mod h1:9CWT6lKIep8U41DDaPiH6eFscnTyjfTANNQNx6LrIcA=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
go.uber.org/multierr v1.5.0 h1:KCa4XfM8CWFCpxXRGok+Q0SS/0XBhMDbHHGABQLvD2A=
go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU=
Expand Down
7 changes: 6 additions & 1 deletion internal/database/postgresql/postgresql.go
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"fmt"
"math"
"math/rand"
"time"

Expand All @@ -27,13 +28,17 @@ type Provider struct {
}

// New establishes the database connection and returns a new Provider instance
func New(address string) (*Provider, error) {
func New(address string, maxConns int) (*Provider, error) {
// Establish the database connection
db, err := sqlx.Open("pgx", address)
if err != nil {
return nil, err
}

// Limit the maximum open and idle database connections
db.SetMaxOpenConns(maxConns)
db.SetMaxIdleConns(int(math.Ceil(float64(maxConns) / 2)))

// Use Unsafe so that the app doesn't fail if we add new columns to the database
return &Provider{
db: db.Unsafe(),
Expand Down
2 changes: 1 addition & 1 deletion internal/database/postgresql/postgresql_test.go
Expand Up @@ -116,7 +116,7 @@ func TestPostgresql(t *testing.T) {

// mustInitialize connects to and migrates the database
func mustInitialize(t *testing.T, migrationsURL string) *postgresql.Provider {
db, err := postgresql.New(address)
db, err := postgresql.New(address, 0)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit edbe175

Please sign in to comment.