-
-
Notifications
You must be signed in to change notification settings - Fork 109
/
parse_opts.go
67 lines (58 loc) · 1.98 KB
/
parse_opts.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package sqlcon
import (
"net/url"
"strconv"
"strings"
"time"
"github.com/sirupsen/logrus"
)
// ParseConnectionOptions parses values for max_conns, max_idle_conns, max_conn_lifetime from DSNs.
func ParseConnectionOptions(l logrus.FieldLogger, dsn string) (maxConns int, maxIdleConns int, maxConnLifetime time.Duration) {
maxConns = maxParallelism() * 2
maxIdleConns = maxParallelism()
maxConnLifetime = time.Duration(0)
parts := strings.Split(dsn, "?")
if len(parts) != 2 {
l.
WithField("sql_max_connections", maxConns).
WithField("sql_max_idle_connections", maxIdleConns).
WithField("sql_max_connection_lifetime", maxConnLifetime).
Debugf("No SQL connection options have been defined, falling back to default connection options.")
return
}
query, err := url.ParseQuery(parts[1])
if err != nil {
l.
WithField("sql_max_connections", maxConns).
WithField("sql_max_idle_connections", maxIdleConns).
WithField("sql_max_connection_lifetime", maxConnLifetime).
WithError(err).
Warnf("Unable to parse SQL DSN query, falling back to default connection options.")
return
}
if v := query.Get("max_conns"); v != "" {
s, err := strconv.ParseInt(v, 10, 64)
if err != nil {
l.WithError(err).Warnf(`SQL DSN query parameter "max_conns" value %v could not be parsed to int, falling back to default value %d`, v, maxConns)
} else {
maxConns = int(s)
}
}
if v := query.Get("max_idle_conns"); v != "" {
s, err := strconv.ParseInt(v, 10, 64)
if err != nil {
l.WithError(err).Warnf(`SQL DSN query parameter "max_idle_conns" value %v could not be parsed to int, falling back to default value %d`, v, maxIdleConns)
} else {
maxIdleConns = int(s)
}
}
if v := query.Get("max_conn_lifetime"); v != "" {
s, err := time.ParseDuration(v)
if err != nil {
l.WithError(err).Warnf(`SQL DSN query parameter "max_conn_lifetime" value %v could not be parsed to int, falling back to default value %d`, v, maxConnLifetime)
} else {
maxConnLifetime = s
}
}
return
}