From 6721187e2ecf31a79016ceabb0a24889fcd766cf Mon Sep 17 00:00:00 2001 From: reimda Date: Fri, 29 Jul 2022 11:53:08 -0600 Subject: [PATCH] feat(outputs.sql): Add settings for go sql.DB settings (#11551) --- plugins/outputs/sql/README.md | 14 ++++++++++++++ plugins/outputs/sql/sample.conf | 14 ++++++++++++++ plugins/outputs/sql/sql.go | 30 +++++++++++++++++++++++------- 3 files changed, 51 insertions(+), 7 deletions(-) diff --git a/plugins/outputs/sql/README.md b/plugins/outputs/sql/README.md index af7ccf73f..c6bca7772 100644 --- a/plugins/outputs/sql/README.md +++ b/plugins/outputs/sql/README.md @@ -114,6 +114,20 @@ convert settings. ## the unsigned option. This is useful for a database like ClickHouse where ## the unsigned value should use a value like "uint64". # conversion_style = "unsigned_suffix" + + ## Maximum amount of time a connection may be idle. "0s" means connections are + ## never closed due to idle time. + # connection_max_idle_time = "0s" + + ## Maximum amount of time a connection may be reused. "0s" means connections + ## are never closed due to age. + # connection_max_lifetime = "0s" + + ## Maximum number of connections in the idle connection pool. 0 means unlimited. + # connection_max_idle = 2 + + ## Maximum number of open connections to the database. 0 means unlimited. + # connection_max_open = 0 ``` ## Driver-specific information diff --git a/plugins/outputs/sql/sample.conf b/plugins/outputs/sql/sample.conf index 424701306..c9528d5e9 100644 --- a/plugins/outputs/sql/sample.conf +++ b/plugins/outputs/sql/sample.conf @@ -51,3 +51,17 @@ ## the unsigned option. This is useful for a database like ClickHouse where ## the unsigned value should use a value like "uint64". # conversion_style = "unsigned_suffix" + + ## Maximum amount of time a connection may be idle. "0s" means connections are + ## never closed due to idle time. + # connection_max_idle_time = "0s" + + ## Maximum amount of time a connection may be reused. "0s" means connections + ## are never closed due to age. + # connection_max_lifetime = "0s" + + ## Maximum number of connections in the idle connection pool. 0 means unlimited. + # connection_max_idle = 2 + + ## Maximum number of open connections to the database. 0 means unlimited. + # connection_max_open = 0 diff --git a/plugins/outputs/sql/sql.go b/plugins/outputs/sql/sql.go index 420141339..f4efd21c3 100644 --- a/plugins/outputs/sql/sql.go +++ b/plugins/outputs/sql/sql.go @@ -6,6 +6,7 @@ import ( _ "embed" "fmt" "strings" + "time" //Register sql drivers _ "github.com/ClickHouse/clickhouse-go" // clickhouse @@ -34,13 +35,17 @@ type ConvertStruct struct { } type SQL struct { - Driver string - DataSourceName string - TimestampColumn string - TableTemplate string - TableExistsTemplate string - InitSQL string `toml:"init_sql"` - Convert ConvertStruct + Driver string + DataSourceName string + TimestampColumn string + TableTemplate string + TableExistsTemplate string + InitSQL string `toml:"init_sql"` + Convert ConvertStruct + ConnectionMaxIdleTime time.Duration + ConnectionMaxLifetime time.Duration + ConnectionMaxIdle int + ConnectionMaxOpen int db *gosql.DB Log telegraf.Logger `toml:"-"` @@ -62,6 +67,11 @@ func (p *SQL) Connect() error { return err } + db.SetConnMaxIdleTime(p.ConnectionMaxIdleTime) + db.SetConnMaxLifetime(p.ConnectionMaxLifetime) + db.SetMaxIdleConns(p.ConnectionMaxIdle) + db.SetMaxOpenConns(p.ConnectionMaxOpen) + if p.InitSQL != "" { _, err = db.Exec(p.InitSQL) if err != nil { @@ -277,5 +287,11 @@ func newSQL() *SQL { Bool: "BOOL", ConversionStyle: "unsigned_suffix", }, + // Defaults for the connection settings (ConnectionMaxIdleTime, + // ConnectionMaxLifetime, ConnectionMaxIdle, and ConnectionMaxOpen) + // mirror the golang defaults. As of go 1.18 all of them default to 0 + // except max idle connections which is 2. See + // https://pkg.go.dev/database/sql#DB.SetMaxIdleConns + ConnectionMaxIdle: 2, } }