Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase stability by exposing DB connection and pool properties #324

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/caconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,13 @@ type CAInfo struct {

// CAConfigDB is the database part of the server's config
type CAConfigDB struct {
Type string `def:"sqlite3" help:"Type of database; one of: sqlite3, postgres, mysql"`
Datasource string `def:"fabric-ca-server.db" help:"Data source which is database specific"`
TLS tls.ClientTLSConfig
Type string `def:"sqlite3" help:"Type of database; one of: sqlite3, postgres, mysql"`
Datasource string `def:"fabric-ca-server.db" help:"Data source which is database specific"`
TLS tls.ClientTLSConfig
MaxIdleConns int `def:"2" help:"Maximum number of connections in the idle pool; default is 2"`
MaxOpenConns int `def:"0" help:"Maximum number of open database connections; default is infinite or zero"`
ConnMaxIdleTime time.Duration `def:"15m" help:"Maximum amount of time a connection may be idle; default is infinite or zero"`
ConnMaxLifetime time.Duration `def:"30m" help:"Maximum amount of time a connection may be reused; default is infinite or zero"`
}

// Implements Stringer interface for CAConfigDB
Expand Down
29 changes: 29 additions & 0 deletions lib/server/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ type FabricCADB interface {
Rebind(query string) string
MustBegin() *sqlx.Tx
Close() error
SetMaxIdleConns(n int)
SetMaxOpenConns(n int)
SetConnMaxLifetime(d time.Duration)
SetConnMaxIdleTime(d time.Duration)
PingContext(ctx context.Context) error
}

Expand All @@ -54,7 +57,10 @@ type SqlxDB interface {
Rebind(query string) string
MustBegin() *sqlx.Tx
Close() error
SetMaxIdleConns(n int)
SetMaxOpenConns(n int)
SetConnMaxIdleTime(d time.Duration)
SetConnMaxLifetime(d time.Duration)
PingContext(ctx context.Context) error
}

Expand Down Expand Up @@ -169,11 +175,34 @@ func (db *DB) Close() error {
return db.DB.Close()
}

// SetMaxIdleConns sets the maximum number of connections in the idle connection pool.
// The default is 2
// See database/sql/sql.go:SetMaxIdleConns for further information
func (db *DB) SetMaxIdleConns(n int) {
db.DB.SetMaxIdleConns(n)
}

// SetMaxOpenConns sets number of max open connections
func (db *DB) SetMaxOpenConns(n int) {
db.DB.SetMaxOpenConns(n)
}

// SetConnMaxIdleTime defines the maximum length of time a connection can be idle for before being
// optionally closed lazily before reuse.
func (db *DB) SetConnMaxIdleTime(d time.Duration) {
db.DB.SetConnMaxIdleTime(d)
}

// SetConnMaxLifetime sets the lifetime of the open connections.
// 0 is the default duration set in the underlying database/sql library meaning
// connections have an infinite life and are not closed based on a connection's age.
// Setting this to a non-zero value is useful for recycling connections
//
// Works for each Postgres, MySQL, and SQLite since they all use sqlx and database/sql
func (db *DB) SetConnMaxLifetime(d time.Duration) {
db.DB.SetConnMaxLifetime(d)
}

// PingContext pings the database
func (db *DB) PingContext(ctx context.Context) error {
return db.DB.PingContext(ctx)
Expand Down
109 changes: 109 additions & 0 deletions lib/server/db/mocks/SqlxDB.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.