forked from zlyuancn/zconn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssdb.go
81 lines (66 loc) · 1.85 KB
/
ssdb.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
80
81
/*
-------------------------------------------------
Author : Zhang Fan
date: 2020/6/9
Description :
-------------------------------------------------
*/
package zconn
import (
"fmt"
"github.com/seefan/gossdb"
ssdbconf "github.com/seefan/gossdb/conf"
)
type ssdbConnector struct{}
var _ IConnector = (*ssdbConnector)(nil)
type SsdbConfig struct {
Host string
Port int
Password string
GetClientTimeout int // 获取客户端超时(毫秒)
MinPoolSize int // 最小连接池数
MaxPoolSize int // 最大连接池个数
RetryEnabled bool // 是否启用重试,设置为true时,如果请求失败会再重试一次
}
func (*ssdbConnector) NewEmptyConfig() interface{} {
return new(SsdbConfig)
}
func (*ssdbConnector) Connect(config interface{}) (instance interface{}, err error) {
conf := config.(*SsdbConfig)
pool, err := gossdb.NewPool(&ssdbconf.Config{
Host: conf.Host,
Port: conf.Port,
Password: conf.Password,
GetClientTimeout: conf.GetClientTimeout / 1e3,
MinPoolSize: conf.MinPoolSize,
MaxPoolSize: conf.MaxPoolSize,
RetryEnabled: conf.RetryEnabled,
})
if err != nil {
return nil, fmt.Errorf("连接失败: %s", err)
}
return pool, nil
}
func (*ssdbConnector) Close(instance interface{}) error {
c := instance.(*gossdb.Connectors)
c.Close()
return nil
}
func AddSsdb(config interface{}, conn_name ...string) {
AddConfig(Ssdb, config, conn_name...)
}
func GetSsdb(conn_name ...string) (*gossdb.Connectors, error) {
c, ok := GetConn(Ssdb, conn_name...)
if !ok {
return nil, ErrNoConn
}
if !c.IsConnect() {
return nil, ErrConnNotConnected
}
return c.Instance().(*gossdb.Connectors), nil
}
func MustSsdb(conn_name ...string) *gossdb.Connectors {
c, err := GetSsdb(conn_name...)
panicOnErr(err)
return c
}