forked from zlyuancn/zconn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnector.go
71 lines (61 loc) · 1.87 KB
/
connector.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
/*
-------------------------------------------------
Author : Zhang Fan
date: 2020/6/6
Description :
-------------------------------------------------
*/
package zconn
import (
"fmt"
)
// 连接器
type IConnector interface {
// 创建一个空的配置结构
NewEmptyConfig() interface{}
// 根据配置结构进行连接, 返回一个连接实例
//
// 注意, conf 一定是带指针的
Connect(config interface{}) (instance interface{}, err error)
// 关闭连接实例
Close(instance interface{}) error
}
type ConnType string
const (
// 在这里定义连接器类型
Xorm ConnType = "xorm"
Gorm = "gorm"
Es6 = "es6"
Es7 = "es7"
Etcd3 = "etcd"
Mongo = "mongo"
Redis = "redis"
Ssdb = "ssdb"
KafkaProducer = "kafka_producer"
GrpcConn = "grpc_conn"
)
var connectorRegistry map[ConnType]IConnector
func init() {
connectorRegistry = make(map[ConnType]IConnector)
// 在这里注册连接器
RegistryConnector(Xorm, new(xormConnector))
RegistryConnector(Gorm, new(gormConnector))
RegistryConnector(Es6, new(es6Connector))
RegistryConnector(Es7, new(es7Connector))
RegistryConnector(Etcd3, new(etcd3Connector))
RegistryConnector(Mongo, new(mongoConnector))
RegistryConnector(Redis, new(redisConnector))
RegistryConnector(Ssdb, new(ssdbConnector))
RegistryConnector(KafkaProducer, new(kafkaProducerConnector))
RegistryConnector(GrpcConn, new(grpcConnConnector))
}
// 注册自定义连接器
func RegistryConnector(conntype ConnType, connector IConnector) {
connectorRegistry[conntype] = connector
}
func mustGetConnector(conntype ConnType) IConnector {
if connector, ok := connectorRegistry[conntype]; ok {
return connector
}
panic(fmt.Errorf("未注册的conn类型<%v>", conntype))
}