Browse Source

add able to configure DB connection limits in sql backend (#176)

* add able to configure DB connection limits in sql backend

* fix formatting in changed code

* improve MaxConnLifetime configuration parameter

* hotfix config option in comment
Lord Alfred 6 years ago
parent
commit
ba6a72eb14
1 changed files with 31 additions and 6 deletions
  1. 31 6
      backends/p_sql.go

+ 31 - 6
backends/p_sql.go

@@ -4,6 +4,7 @@ import (
 	"database/sql"
 	"database/sql"
 	"fmt"
 	"fmt"
 	"strings"
 	"strings"
+	"time"
 
 
 	"github.com/flashmob/go-guerrilla/mail"
 	"github.com/flashmob/go-guerrilla/mail"
 
 
@@ -25,6 +26,12 @@ import (
 //               : sql_driver string - database driver name, eg. mysql
 //               : sql_driver string - database driver name, eg. mysql
 //               : sql_dsn string - driver-specific data source name
 //               : sql_dsn string - driver-specific data source name
 //               : primary_mail_host string - primary host name
 //               : primary_mail_host string - primary host name
+//               : sql_max_open_conns - sets the maximum number of open connections
+//               : to the database. The default is 0 (unlimited)
+//               : sql_max_idle_conns - sets the maximum number of connections in the
+//               : idle connection pool. The default is 2
+//               : sql_max_conn_lifetime - sets the maximum amount of time
+//               : a connection may be reused
 // --------------:-------------------------------------------------------------------
 // --------------:-------------------------------------------------------------------
 // Input         : e.Data
 // Input         : e.Data
 //               : e.DeliveryHeader generated by ParseHeader() processor
 //               : e.DeliveryHeader generated by ParseHeader() processor
@@ -40,12 +47,15 @@ func init() {
 }
 }
 
 
 type SQLProcessorConfig struct {
 type SQLProcessorConfig struct {
-	Table       string `json:"mail_table"`
-	Driver      string `json:"sql_driver"`
-	DSN         string `json:"sql_dsn"`
-	SQLInsert   string `json:"sql_insert,omitempty"`
-	SQLValues   string `json:"sql_values,omitempty"`
-	PrimaryHost string `json:"primary_mail_host"`
+	Table           string `json:"mail_table"`
+	Driver          string `json:"sql_driver"`
+	DSN             string `json:"sql_dsn"`
+	SQLInsert       string `json:"sql_insert,omitempty"`
+	SQLValues       string `json:"sql_values,omitempty"`
+	PrimaryHost     string `json:"primary_mail_host"`
+	MaxConnLifetime string `json:"sql_max_conn_lifetime,omitempty"`
+	MaxOpenConns    int    `json:"sql_max_open_conns,omitempty"`
+	MaxIdleConns    int    `json:"sql_max_idle_conns,omitempty"`
 }
 }
 
 
 type SQLProcessor struct {
 type SQLProcessor struct {
@@ -60,6 +70,21 @@ func (s *SQLProcessor) connect() (*sql.DB, error) {
 		Log().Error("cannot open database: ", err)
 		Log().Error("cannot open database: ", err)
 		return nil, err
 		return nil, err
 	}
 	}
+
+	if s.config.MaxOpenConns != 0 {
+		db.SetMaxOpenConns(s.config.MaxOpenConns)
+	}
+	if s.config.MaxIdleConns != 0 {
+		db.SetMaxIdleConns(s.config.MaxIdleConns)
+	}
+	if s.config.MaxConnLifetime != "" {
+		t, err := time.ParseDuration(s.config.MaxConnLifetime)
+		if err != nil {
+			return nil, err
+		}
+		db.SetConnMaxLifetime(t)
+	}
+
 	// do we have permission to access the table?
 	// do we have permission to access the table?
 	_, err = db.Query("SELECT mail_id FROM " + s.config.Table + " LIMIT 1")
 	_, err = db.Query("SELECT mail_id FROM " + s.config.Table + " LIMIT 1")
 	if err != nil {
 	if err != nil {