-
Notifications
You must be signed in to change notification settings - Fork 503
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support/db, services/horizon/internal: Configure postgres client conn…
…ection timeouts for read only db (#4390) Configure postgres client connection timeouts for read only database sessions
- Loading branch information
Showing
6 changed files
with
146 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,9 @@ package db | |
import ( | ||
"context" | ||
"database/sql" | ||
"net/url" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/Masterminds/squirrel" | ||
|
@@ -44,6 +47,9 @@ var ( | |
// ErrBadConnection is an error returned when driver returns `bad connection` | ||
// error. | ||
ErrBadConnection = errors.New("bad connection") | ||
// ErrStatementTimeout is an error returned by Session methods when request has | ||
// been cancelled due to a statement timeout. | ||
ErrStatementTimeout = errors.New("canceling statement due to statement timeout") | ||
) | ||
|
||
// Conn represents a connection to a single database. | ||
|
@@ -163,8 +169,59 @@ func pingDB(db *sqlx.DB) error { | |
return errors.Wrapf(err, "failed to connect to DB after %v attempts", maxDBPingAttempts) | ||
} | ||
|
||
type ClientConfig struct { | ||
Key string | ||
Value string | ||
} | ||
|
||
func StatementTimeout(timeout time.Duration) ClientConfig { | ||
return ClientConfig{ | ||
Key: "statement_timeout", | ||
Value: strconv.FormatInt(timeout.Milliseconds(), 10), | ||
} | ||
} | ||
|
||
func IdleTransactionTimeout(timeout time.Duration) ClientConfig { | ||
return ClientConfig{ | ||
Key: "idle_in_transaction_session_timeout", | ||
Value: strconv.FormatInt(timeout.Milliseconds(), 10), | ||
} | ||
} | ||
|
||
func augmentDSN(dsn string, clientConfigs []ClientConfig) string { | ||
parsed, err := url.Parse(dsn) | ||
// dsn can either be a postgres url like "postgres://postgres:[email protected]:5432" | ||
// or, it can be a white space separated string of key value pairs like | ||
// "host=localhost port=5432 user=bob password=secret" | ||
if err != nil || parsed.Scheme == "" { | ||
// if dsn does not parse as a postgres url, we assume it must be take | ||
// the form of a white space separated string | ||
parts := []string{dsn} | ||
for _, config := range clientConfigs { | ||
// do not override if the key is already present in dsn | ||
if strings.Contains(dsn, config.Key+"=") { | ||
continue | ||
} | ||
parts = append(parts, config.Key+"="+config.Value) | ||
} | ||
return strings.Join(parts, " ") | ||
} | ||
|
||
q := parsed.Query() | ||
for _, config := range clientConfigs { | ||
// do not override if the key is already present in dsn | ||
if len(q.Get(config.Key)) > 0 { | ||
continue | ||
} | ||
q.Set(config.Key, config.Value) | ||
} | ||
parsed.RawQuery = q.Encode() | ||
return parsed.String() | ||
} | ||
|
||
// Open the database at `dsn` and returns a new *Session using it. | ||
func Open(dialect, dsn string) (*Session, error) { | ||
func Open(dialect, dsn string, clientConfigs ...ClientConfig) (*Session, error) { | ||
dsn = augmentDSN(dsn, clientConfigs) | ||
db, err := sqlx.Open(dialect, dsn) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "open failed") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters