-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdsn.go
70 lines (65 loc) · 1.7 KB
/
dsn.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
package tablestore
import (
"github.com/aliyun/aliyun-tablestore-go-sdk/tablestore"
"net/url"
"strconv"
"strings"
"time"
)
type connectionConfig struct {
endPoint string
instanceName string
accessKeyId string
accessKeySecret string
otsConfig *tablestore.TableStoreConfig
}
func parseDSN(dsn string) (*connectionConfig, error) {
parse, err := url.Parse(dsn)
if err != nil {
return nil, err
}
// parse identity
cfg := &connectionConfig{}
cfg.accessKeyId = parse.User.Username()
cfg.accessKeySecret, _ = parse.User.Password()
cfg.endPoint = parse.Scheme + "://" + parse.Host
cfg.instanceName = parse.Path
if strings.HasPrefix(cfg.instanceName, "/") {
// remove leading slash
cfg.instanceName = cfg.instanceName[1:]
}
cfg.otsConfig = tablestore.NewDefaultTableStoreConfig()
// parse options
if val := parse.Query().Get(RetryTimes); val != "" {
retryTimes, err := strconv.ParseUint(val, 10, 32)
if err != nil {
return nil, err
}
cfg.otsConfig.RetryTimes = uint(retryTimes)
}
if val := parse.Query().Get(MaxRetryTime); val != "" {
cfg.otsConfig.MaxRetryTime, err = time.ParseDuration(val)
if err != nil {
return nil, err
}
}
if val := parse.Query().Get(ConnectionTimeout); val != "" {
cfg.otsConfig.HTTPTimeout.ConnectionTimeout, err = time.ParseDuration(val)
if err != nil {
return nil, err
}
}
if val := parse.Query().Get(RequestTimeout); val != "" {
cfg.otsConfig.HTTPTimeout.RequestTimeout, err = time.ParseDuration(val)
if err != nil {
return nil, err
}
}
if val := parse.Query().Get(MaxIdleConnections); val != "" {
cfg.otsConfig.MaxIdleConnections, err = strconv.Atoi(val)
if err != nil {
return nil, err
}
}
return cfg, err
}