Skip to content

Commit

Permalink
feat: add configurable sql connection parameters
Browse files Browse the repository at this point in the history
Allows the user to configure the SQL connection pool's max idle and open
connections as well as a connections maximum lifetime
  • Loading branch information
mikhailswift committed Sep 27, 2023
1 parent d6c37a3 commit 37a9977
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cmd/archivista/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func main() {
logrus.Fatalf("error initializing storage clients: %+v", err)
}

entClient, err := sqlstore.NewEntClient(cfg.SQLStoreBackend, cfg.SQLStoreConnectionString)
entClient, err := sqlstore.NewEntClient(cfg.SQLStoreBackend, cfg.SQLStoreConnectionString, cfg.SQLStoreMaxIdleConnections, cfg.SQLStoreMaxOpenConnections, cfg.SQLStoreConnectionMaxLifetime)
if err != nil {
logrus.Fatalf("could not create ent client: %+v", err)
}
Expand Down
15 changes: 10 additions & 5 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"errors"
"os"
"strings"
"time"

"github.com/kelseyhightower/envconfig"
"github.com/sirupsen/logrus"
Expand All @@ -28,11 +29,15 @@ type Config struct {
LogLevel string `default:"INFO" desc:"Log level" split_words:"true"`
CORSAllowOrigins []string `default:"" desc:"Comma separated list of origins to allow CORS requests from" split_words:"true"`

EnableSPIFFE bool `default:"TRUE" desc:"*** Enable SPIFFE support" split_words:"true"`
SPIFFEAddress string `default:"unix:///tmp/spire-agent/public/api.sock" desc:"SPIFFE server address" split_words:"true"`
SPIFFETrustedServerId string `default:"" desc:"Trusted SPIFFE server ID; defaults to any" split_words:"true"`
SQLStoreConnectionString string `default:"root:example@tcp(db)/testify" desc:"SQL store connection string" split_words:"true"`
SQLStoreBackend string `default:"MYSQL" desc:"SQL backend to use. Options are MYSQL, PSQL" split_words:"true"`
EnableSPIFFE bool `default:"TRUE" desc:"*** Enable SPIFFE support" split_words:"true"`
SPIFFEAddress string `default:"unix:///tmp/spire-agent/public/api.sock" desc:"SPIFFE server address" split_words:"true"`
SPIFFETrustedServerId string `default:"" desc:"Trusted SPIFFE server ID; defaults to any" split_words:"true"`

SQLStoreConnectionString string `default:"root:example@tcp(db)/testify" desc:"SQL store connection string" split_words:"true"`
SQLStoreBackend string `default:"MYSQL" desc:"SQL backend to use. Options are MYSQL, PSQL" split_words:"true"`
SQLStoreMaxIdleConnections int `default:"10" desc:"Maximum numver of connections in the idle connection pool" split_words:"true"`
SQLStoreMaxOpenConnections int `default:"100" desc:"Maximum number of open connections to the database" split_words:"true"`
SQLStoreConnectionMaxLifetime time.Duration `default:"3m" desc:"Maximum amount of time a connection may be reused" split_words:"true"`

StorageBackend string `default:"" desc:"Backend to use for attestation storage. Options are FILE, BLOB, or empty string for disabled." split_words:"true"`
FileServeOn string `default:"" desc:"What address to serve files on. Only valid when using FILE storage backend." split_words:"true"`
Expand Down
8 changes: 4 additions & 4 deletions internal/metadatastorage/sqlstore/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (

// NewEntClient creates an ent client for use in the sqlmetadata store.
// Valid backends are MYSQL and PSQL.
func NewEntClient(sqlBackend string, connectionString string) (*ent.Client, error) {
func NewEntClient(sqlBackend string, connectionString string, maxxIdleConns, maxOpenConns int, connMaxxLifetime time.Duration) (*ent.Client, error) {
var entDialect string
switch strings.ToUpper(sqlBackend) {
case "MYSQL":
Expand All @@ -56,9 +56,9 @@ func NewEntClient(sqlBackend string, connectionString string) (*ent.Client, erro
}

db := drv.DB()
db.SetMaxIdleConns(10)
db.SetMaxOpenConns(100)
db.SetConnMaxLifetime(3 * time.Minute)
db.SetMaxIdleConns(maxxIdleConns)
db.SetMaxOpenConns(maxOpenConns)
db.SetConnMaxLifetime(connMaxxLifetime)
sqlcommentDrv := sqlcomment.NewDriver(drv,
sqlcomment.WithDriverVerTag(),
sqlcomment.WithTags(sqlcomment.Tags{
Expand Down

0 comments on commit 37a9977

Please sign in to comment.