Skip to content
Merged
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
1 change: 1 addition & 0 deletions config/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ const (
FileStorePath string = "FileStorePath"
SQLDriver string = "SQLDriver"
SQLDataSourceName string = "SQLDataSourceName"
SQLConnMaxLifetime string = "SQLConnMaxLifetime"
ValidateFieldsOutOfOrder string = "ValidateFieldsOutOfOrder"
)
10 changes: 10 additions & 0 deletions config/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,15 @@ The name of the database driver to use (see https://github.com/golang/go/wiki/SQ
SQLDataSourceName

The driver-specific data source name of the database to use. Only used with SqlStoreFactory.

SQLConnMaxLifetime

SetConnMaxLifetime sets the maximum duration of time that a database connection may be reused (see https://golang.org/pkg/database/sql/#DB.SetConnMaxLifetime). Defaults to zero, which causes connections to be reused forever. Only used with SqlStoreFactory.

If your database server has a config option to close inactive connections after some duration (e.g. MySQL "wait_timeout"), set SQLConnMaxLifetime to a value less than that duration.

Example Values:
SQLConnMaxLifetime=14400s # 14400 seconds
SQLConnMaxLifetime=2h45m # 2 hours and 45 minutes
*/
package config
12 changes: 12 additions & 0 deletions session_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"strconv"
"time"
)

//SessionSettings maps session settings to values with typed accessors.
Expand Down Expand Up @@ -61,6 +62,17 @@ func (s *SessionSettings) IntSetting(setting string) (int, error) {
return strconv.Atoi(stringVal)
}

//DurationSetting returns the requested setting parsed as a time.Duration. Returns an error if the setting is not set or cannot be parsed as a time.Duration.
func (s *SessionSettings) DurationSetting(setting string) (val time.Duration, err error) {
stringVal, err := s.Setting(setting)

if err != nil {
return
}

return time.ParseDuration(stringVal)
}

//BoolSetting returns the requested setting parsed as a boolean. Returns an errror if the setting is not set or cannot be parsed as a bool.
func (s SessionSettings) BoolSetting(setting string) (bool, error) {
stringVal, err := s.Setting(setting)
Expand Down
33 changes: 22 additions & 11 deletions sqlstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ type sqlStoreFactory struct {
}

type sqlStore struct {
sessionID SessionID
cache *memoryStore
sqlDriver string
sqlDataSourceName string
db *sql.DB
sessionID SessionID
cache *memoryStore
sqlDriver string
sqlDataSourceName string
sqlConnMaxLifetime time.Duration
db *sql.DB
}

// NewSQLStoreFactory returns a sql-based implementation of MessageStoreFactory
Expand All @@ -39,21 +40,31 @@ func (f sqlStoreFactory) Create(sessionID SessionID) (msgStore MessageStore, err
if err != nil {
return nil, err
}
return newSQLStore(sessionID, sqlDriver, sqlDataSourceName)
sqlConnMaxLifetime := 0 * time.Second
if sessionSettings.HasSetting(config.SQLConnMaxLifetime) {
sqlConnMaxLifetime, err = sessionSettings.DurationSetting(config.SQLConnMaxLifetime)
if err != nil {
return nil, err
}
}
return newSQLStore(sessionID, sqlDriver, sqlDataSourceName, sqlConnMaxLifetime)
}

func newSQLStore(sessionID SessionID, driver string, dataSourceName string) (store *sqlStore, err error) {
func newSQLStore(sessionID SessionID, driver string, dataSourceName string, connMaxLifetime time.Duration) (store *sqlStore, err error) {
store = &sqlStore{
sessionID: sessionID,
cache: &memoryStore{},
sqlDriver: driver,
sqlDataSourceName: dataSourceName,
sessionID: sessionID,
cache: &memoryStore{},
sqlDriver: driver,
sqlDataSourceName: dataSourceName,
sqlConnMaxLifetime: connMaxLifetime,
}
store.cache.Reset()

if store.db, err = sql.Open(store.sqlDriver, store.sqlDataSourceName); err != nil {
return nil, err
}
store.db.SetConnMaxLifetime(store.sqlConnMaxLifetime)

if err = store.db.Ping(); err != nil { // ensure immediate connection
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions sqlstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func (suite *SQLStoreTestSuite) SetupTest() {
[DEFAULT]
SQLDriver=%s
SQLDataSourceName=%s
SQLConnMaxLifetime=14400s

[SESSION]
BeginString=%s
Expand Down