-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathoptions.go
79 lines (71 loc) · 2.66 KB
/
options.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
68
69
70
71
72
73
74
75
76
77
78
79
package postgres
import (
"fmt"
"math"
"github.com/kubemq-io/kubemq-targets/config"
)
const (
defaultMaxIdleConnections = 10
defaultMaxOpenConnections = 100
defaultConnectionMaxLifetimeSeconds = 3600
)
type options struct {
credentials string
useProxy bool
instanceConnectionName string
dbUser string
dbName string
dbPassword string
connection string
// maxIdleConnections sets the maximum number of connections in the idle connection pool
maxIdleConnections int
// maxOpenConnections sets the maximum number of open connections to the database.
maxOpenConnections int
// connectionMaxLifetimeSeconds sets the maximum amount of time a connection may be reused.
connectionMaxLifetimeSeconds int
}
func parseOptions(cfg config.Spec) (options, error) {
o := options{}
var err error
o.useProxy = cfg.Properties.ParseBool("use_proxy", true)
if o.useProxy {
o.instanceConnectionName, err = cfg.Properties.MustParseString("instance_connection_name")
if err != nil {
return options{}, fmt.Errorf("error parsing instance_connection_name string, %w", err)
}
o.dbUser, err = cfg.Properties.MustParseString("db_user")
if err != nil {
return options{}, fmt.Errorf("error parsing db_user string , , %w", err)
}
o.dbName, err = cfg.Properties.MustParseString("db_name")
if err != nil {
return options{}, fmt.Errorf("error parsing db_name string, %w", err)
}
o.dbPassword, err = cfg.Properties.MustParseString("db_password")
if err != nil {
return options{}, fmt.Errorf("error parsing db_password string, %w", err)
}
o.credentials, err = cfg.Properties.MustParseString("credentials")
if err != nil {
return options{}, err
}
} else {
o.connection, err = cfg.Properties.MustParseString("connection")
if err != nil {
return options{}, fmt.Errorf("error parsing connection string, %w", err)
}
}
o.maxIdleConnections, err = cfg.Properties.ParseIntWithRange("max_idle_connections", defaultMaxIdleConnections, 1, math.MaxInt32)
if err != nil {
return options{}, fmt.Errorf("error parsing max idle connections value, %w", err)
}
o.maxOpenConnections, err = cfg.Properties.ParseIntWithRange("max_open_connections", defaultMaxOpenConnections, 1, math.MaxInt32)
if err != nil {
return options{}, fmt.Errorf("error parsing max open connections value, %w", err)
}
o.connectionMaxLifetimeSeconds, err = cfg.Properties.ParseIntWithRange("connection_max_lifetime_seconds", defaultConnectionMaxLifetimeSeconds, 1, math.MaxInt32)
if err != nil {
return options{}, fmt.Errorf("error parsing connection max lifetime seconds value, %w", err)
}
return o, nil
}