Skip to content

Commit

Permalink
Add max_db_conn_count (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
zxia-wish authored Aug 4, 2021
1 parent 28cc28b commit 2608762
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ To connect to a Postgres database, serveral fields have to be set in the configr
- `database`: the name of the database
- `username`: the username of the user with necessary permission to connect/read/write, and
- `password`: the password of the user (ommited if Vault integration is turned on)
- `max_db_conn_count`: maximum database connections allowed (defaulted to be 128)

Additionally, you may choose to toggle integration with HasiCorp Vault. This allows Eventmaster to obtain database password from Vault instead of leaving them as plain-text in the filesystem. Only v2 is supported currently. To do so, set the following fields:
- `vault`: object containing Vault config
Expand Down
23 changes: 16 additions & 7 deletions postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ type VaultConfig struct {
}

type PostgresConfig struct {
Addr string `json:"addr"`
Port int `json:"port"`
Database string `json:"database"`
ServiceName string `json:"service_name"`
Username string `json:"username"`
Password string `json:"password"`
Vault VaultConfig `json:"vault"`
Addr string `json:"addr"`
Port int `json:"port"`
Database string `json:"database"`
ServiceName string `json:"service_name"`
Username string `json:"username"`
Password string `json:"password"`
Vault VaultConfig `json:"vault"`
MaxDbConnCount int `json:"max_dc_conn_count`
}

func getPasswordFromVault(c VaultConfig) (*string, error) {
Expand Down Expand Up @@ -107,6 +108,14 @@ func NewPostgresStore(c PostgresConfig) (*PostgresStore, error) {
log.Errorf(err.Error())
return nil, errors.Wrap(err, "Error creating postgres session")
}

connCount := 128
if c.MaxDbConnCount != 0 {
connCount = c.MaxDbConnCount
}
db.SetMaxOpenConns(connCount)
log.Infof("Set max DB connection count to %d", connCount)

log.Infof("Successfully connected to postgres %s", host)

return &PostgresStore{
Expand Down

0 comments on commit 2608762

Please sign in to comment.