Skip to content

Commit f22af9d

Browse files
backend/postgres: Add StatementTimeout and loose argument
1 parent a89c73b commit f22af9d

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

backend/postgres/config.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import (
66
"fmt"
77
"io/ioutil"
88
"net/url"
9+
"strconv"
910
"sync"
11+
"time"
1012
)
1113

1214
type DBRole string
@@ -48,6 +50,10 @@ type Config struct {
4850

4951
Role DBRole
5052

53+
StatementTimeout time.Duration
54+
55+
ConnectionArguments map[string]string
56+
5157
certOnce sync.Once
5258
certErr error
5359
}
@@ -157,6 +163,17 @@ func (c *Config) DSN() (string, error) {
157163
q.Add("application_name", c.ApplicationName)
158164
}
159165

166+
if c.StatementTimeout > 0 {
167+
q.Add(
168+
"statement_timeout",
169+
strconv.Itoa(int(c.StatementTimeout.Milliseconds())),
170+
)
171+
}
172+
173+
for k, v := range c.ConnectionArguments {
174+
q.Add(k, v)
175+
}
176+
160177
u := url.URL{
161178
Scheme: "postgres",
162179
User: c.userInfo(),

backend/postgres/config_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package postgres
22

33
import (
44
"testing"
5+
"time"
56

67
"github.com/stretchr/testify/assert"
78
)
@@ -19,6 +20,10 @@ func TestDSN(t *testing.T) {
1920
c: &Config{DBName: "foobar", CACertFile: "foobar", ApplicationName: "buz"},
2021
dsn: "postgres://localhost:5432/foobar?application_name=buz&sslmode=verify-ca&sslrootcert=foobar&sslsni=0",
2122
},
23+
{
24+
c: &Config{DBName: "foobar", ConnectionArguments: map[string]string{"foo": "bar"}, StatementTimeout: 2 * time.Second},
25+
dsn: "postgres://localhost:5432/foobar?foo=bar&sslmode=disable&sslsni=0&statement_timeout=2000",
26+
},
2227
} {
2328
dsn, err := tt.c.DSN()
2429

0 commit comments

Comments
 (0)