Skip to content

Commit

Permalink
feat: database pool configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
vm-001 committed Nov 18, 2024
1 parent 117929a commit 0ea013a
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 9 deletions.
4 changes: 4 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ func (app *Application) NodeID() string {
return config.NODE
}

func (app *Application) Config() *config.Config {
return app.cfg

Check warning on line 160 in app/app.go

View check run for this annotation

Codecov / codecov/patch

app/app.go#L159-L160

Added lines #L159 - L160 were not covered by tests
}

// Start starts application
func (app *Application) Start() error {
app.mux.Lock()
Expand Down
2 changes: 2 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ database:
username: webhookx
password:
database: webhookx
max_pool_size: 40 # The maximum number of connections
max_lifetime: 1800 # The maximum lifetime (in seconds) of a connection

redis:
host: localhost
Expand Down
12 changes: 7 additions & 5 deletions config/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (
)

type DatabaseConfig struct {
Host string `yaml:"host" default:"localhost"`
Port uint32 `yaml:"port" default:"5432"`
Username string `yaml:"username" default:"webhookx"`
Password string `yaml:"password" default:""`
Database string `yaml:"database" default:"webhookx"`
Host string `yaml:"host" default:"localhost"`
Port uint32 `yaml:"port" default:"5432"`
Username string `yaml:"username" default:"webhookx"`
Password string `yaml:"password" default:""`
Database string `yaml:"database" default:"webhookx"`
MaxPoolSize uint32 `yaml:"max_pool_size" default:"40" envconfig:"MAX_POOL_SIZE"`
MaxLifeTime uint `yaml:"max_life_time" default:"1800" envconfig:"MAX_LIFETIME"`
}

func (cfg DatabaseConfig) GetDSN() string {
Expand Down
8 changes: 4 additions & 4 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/webhookx-io/webhookx/db/dao"
"github.com/webhookx-io/webhookx/db/transaction"
"go.uber.org/zap"
"time"
)

type DB struct {
Expand All @@ -33,10 +34,9 @@ type DB struct {

func initSqlxDB(cfg *config.DatabaseConfig) (*sqlx.DB, error) {
db, err := sql.Open("postgres", cfg.GetDSN())
// db.SetMaxOpenConns(100)
// db.SetMaxIdleConns(100)
// db.SetConnMaxLifetime(time.Hour)
// db.SetConnMaxIdleTime(time.Hour)
db.SetMaxOpenConns(int(cfg.MaxPoolSize))
db.SetMaxIdleConns(int(cfg.MaxPoolSize))
db.SetConnMaxLifetime(time.Second * time.Duration(cfg.MaxLifeTime))

Check warning on line 39 in db/db.go

View check run for this annotation

Codecov / codecov/patch

db/db.go#L37-L39

Added lines #L37 - L39 were not covered by tests
if err != nil {
return nil, err
}
Expand Down
38 changes: 38 additions & 0 deletions test/cfg/cfg_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package cfg

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/stretchr/testify/assert"
"github.com/webhookx-io/webhookx/app"
"github.com/webhookx-io/webhookx/test/helper"
"github.com/webhookx-io/webhookx/utils"
"testing"
)

var _ = Describe("Configuration", Ordered, func() {

var app *app.Application

BeforeAll(func() {
app = utils.Must(helper.Start(map[string]string{
"WEBHOOKX_DATABASE_MAX_POOL_SIZE": "0",
"WEBHOOKX_DATABASE_MAX_LIFETIME": "3600",
}))
})

AfterAll(func() {
app.Stop()
})

It("database configuration", func() {
assert.EqualValues(GinkgoT(), 0, app.Config().Database.MaxPoolSize)
assert.EqualValues(GinkgoT(), 3600, app.Config().Database.MaxLifeTime)
})

})

func TestConfiguration(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Configuration Suite")
}

0 comments on commit 0ea013a

Please sign in to comment.