From ff1db4e2a4c3c1bf9af349dd39c247860aff7a7e Mon Sep 17 00:00:00 2001 From: iRedMail <2048991+iredmail@users.noreply.github.com> Date: Tue, 25 Jan 2022 19:58:19 +0800 Subject: [PATCH] Public sqlite db options: MaxOpenConns, MaxIdleConns, ConnMaxLifetime. (#310) * Public sqlite db options: MaxOpenConns, MaxIdleConns, ConnMaxLifetime. * Update sqlite3/README.md. * Add comment lines for the public options. * Typo * Set default values for MaxIdleConns, MaxOpenConns, ConnMaxLifetime. --- sqlite3/README.md | 41 +++++++++++++++++++++++++++++++++-------- sqlite3/config.go | 36 ++++++++++++++++++++++++++++-------- sqlite3/sqlite3.go | 6 +++--- 3 files changed, 64 insertions(+), 19 deletions(-) diff --git a/sqlite3/README.md b/sqlite3/README.md index 6e5714d0d..35d40bd7a 100644 --- a/sqlite3/README.md +++ b/sqlite3/README.md @@ -41,10 +41,13 @@ store := sqlite3.New() // Initialize custom config store := sqlite3.New(sqlite3.Config{ - Database: "./fiber.sqlite3", - Table: "fiber_storage", - Reset: false, - GCInterval: 10 * time.Second, + Database: "./fiber.sqlite3", + Table: "fiber_storage", + Reset: false, + GCInterval: 10 * time.Second, + MaxOpenConns: 100, + MaxIdleConns: 100, + ConnMaxLifetime: 1 * time.Second, }) ``` @@ -70,15 +73,37 @@ type Config struct { // // Optional. Default is 10 * time.Second GCInterval time.Duration + + // ////////////////////////////////// + // Adaptor related config options // + // ////////////////////////////////// + + // MaxIdleConns sets the maximum number of connections in the idle connection pool. + // + // Optional. Default is 100. + MaxIdleConns int + + // MaxOpenConns sets the maximum number of open connections to the database. + // + // Optional. Default is 100. + MaxOpenConns int + + // ConnMaxLifetime sets the maximum amount of time a connection may be reused. + // + // Optional. Default is 1 second. + ConnMaxLifetime time.Duration } ``` ### Default Config ```go var ConfigDefault = Config{ - Database: "./fiber.sqlite3", - Table: "fiber_storage", - Reset: false, - GCInterval: 10 * time.Second, + Database: "./fiber.sqlite3", + Table: "fiber_storage", + Reset: false, + GCInterval: 10 * time.Second, + MaxOpenConns: 100, + MaxIdleConns: 100, + ConnMaxLifetime: 1 * time.Second, } ``` diff --git a/sqlite3/config.go b/sqlite3/config.go index b203391a6..83c4ecade 100644 --- a/sqlite3/config.go +++ b/sqlite3/config.go @@ -24,13 +24,24 @@ type Config struct { // Optional. Default is 10 * time.Second GCInterval time.Duration - //////////////////////////////////// + // ////////////////////////////////// // Adaptor related config options // - //////////////////////////////////// + // ////////////////////////////////// - maxIdleConns int - maxOpenConns int - connMaxLifetime time.Duration + // MaxIdleConns sets the maximum number of connections in the idle connection pool. + // + // Optional. Default is 100. + MaxIdleConns int + + // MaxOpenConns sets the maximum number of open connections to the database. + // + // Optional. Default is 100. + MaxOpenConns int + + // ConnMaxLifetime sets the maximum amount of time a connection may be reused. + // + // Optional. Default is 1 second. + ConnMaxLifetime time.Duration } // ConfigDefault is the default config @@ -42,9 +53,9 @@ var ConfigDefault = Config{ GCInterval: 10 * time.Second, // Adaptor related config options - maxOpenConns: 100, - maxIdleConns: 100, - connMaxLifetime: 1 * time.Second, + MaxOpenConns: 100, + MaxIdleConns: 100, + ConnMaxLifetime: 1 * time.Second, } // Helper function to set default values @@ -67,5 +78,14 @@ func configDefault(config ...Config) Config { if int(cfg.GCInterval.Seconds()) <= 0 { cfg.GCInterval = ConfigDefault.GCInterval } + if cfg.MaxIdleConns <= 0 { + cfg.MaxIdleConns = ConfigDefault.MaxIdleConns + } + if cfg.MaxOpenConns <= 0 { + cfg.MaxOpenConns = ConfigDefault.MaxOpenConns + } + if cfg.ConnMaxLifetime == 0 { + cfg.ConnMaxLifetime = ConfigDefault.ConnMaxLifetime + } return cfg } diff --git a/sqlite3/sqlite3.go b/sqlite3/sqlite3.go index 725ddb6c9..0f1f79612 100644 --- a/sqlite3/sqlite3.go +++ b/sqlite3/sqlite3.go @@ -47,9 +47,9 @@ func New(config ...Config) *Storage { } // Set database options - db.SetMaxOpenConns(cfg.maxOpenConns) - db.SetMaxIdleConns(cfg.maxIdleConns) - db.SetConnMaxLifetime(cfg.connMaxLifetime) + db.SetMaxOpenConns(cfg.MaxOpenConns) + db.SetMaxIdleConns(cfg.MaxIdleConns) + db.SetConnMaxLifetime(cfg.ConnMaxLifetime) // Ping database if err := db.Ping(); err != nil {