-
Notifications
You must be signed in to change notification settings - Fork 504
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support/db, services/horizon/internal: Configure postgres client connection timeouts for read only db #4390
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -91,6 +91,10 @@ func (s *Session) Commit() error { | |
log.Debug("sql: commit") | ||
s.tx = nil | ||
s.txOptions = nil | ||
|
||
if knownErr := s.replaceWithKnownError(err, context.Background()); knownErr != nil { | ||
return knownErr | ||
} | ||
return err | ||
} | ||
|
||
|
@@ -231,6 +235,10 @@ func (s *Session) NoRows(err error) bool { | |
// replaceWithKnownError tries to replace Postgres error with package error. | ||
// Returns a new error if the err is known. | ||
func (s *Session) replaceWithKnownError(err error, ctx context.Context) error { | ||
if err == nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good extra fix, coulda been some bad ptr refs happening here |
||
return nil | ||
} | ||
|
||
switch { | ||
case ctx.Err() == context.Canceled: | ||
return ErrCancelled | ||
|
@@ -243,6 +251,8 @@ func (s *Session) replaceWithKnownError(err error, ctx context.Context) error { | |
return ErrConflictWithRecovery | ||
case strings.Contains(err.Error(), "driver: bad connection"): | ||
return ErrBadConnection | ||
case strings.Contains(err.Error(), "pq: canceling statement due to statement timeout"): | ||
return ErrStatementTimeout | ||
default: | ||
return nil | ||
} | ||
|
@@ -305,6 +315,10 @@ func (s *Session) Rollback() error { | |
log.Debug("sql: rollback") | ||
s.tx = nil | ||
s.txOptions = nil | ||
|
||
if knownErr := s.replaceWithKnownError(err, context.Background()); knownErr != nil { | ||
return knownErr | ||
} | ||
return err | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -129,3 +129,34 @@ func TestSession(t *testing.T) { | |
assert.Equal("$1 = $2 = $3 = ?", out) | ||
} | ||
} | ||
|
||
func TestStatementTimeout(t *testing.T) { | ||
assert := assert.New(t) | ||
db := dbtest.Postgres(t).Load(testSchema) | ||
defer db.Close() | ||
|
||
sess, err := Open(db.Dialect, db.DSN, StatementTimeout(50*time.Millisecond)) | ||
assert.NoError(err) | ||
defer sess.Close() | ||
|
||
var count int | ||
err = sess.GetRaw(context.Background(), &count, "SELECT pg_sleep(2), COUNT(*) FROM people") | ||
assert.ErrorIs(err, ErrStatementTimeout) | ||
} | ||
|
||
func TestIdleTransactionTimeout(t *testing.T) { | ||
assert := assert.New(t) | ||
db := dbtest.Postgres(t).Load(testSchema) | ||
defer db.Close() | ||
|
||
sess, err := Open(db.Dialect, db.DSN, IdleTransactionTimeout(50*time.Millisecond)) | ||
assert.NoError(err) | ||
defer sess.Close() | ||
|
||
assert.NoError(sess.Begin()) | ||
<-time.After(100 * time.Millisecond) | ||
|
||
var count int | ||
err = sess.GetRaw(context.Background(), &count, "SELECT COUNT(*) FROM people") | ||
assert.ErrorIs(err, ErrBadConnection) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. interesting, a force closed tx, invalidates the whole connection, really good test to show that effect! |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice addition, should be interesting to see how often this StatementTimeout kicks in!