-
Notifications
You must be signed in to change notification settings - Fork 1
/
proxy.go
161 lines (134 loc) · 3.22 KB
/
proxy.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package app
import (
"context"
"database/sql"
"github.com/cnlisea/ant/app/mq/message"
"github.com/cnlisea/ant/app/proxy"
"github.com/gomodule/redigo/redis"
"github.com/nacos-group/nacos-sdk-go/v2/clients/naming_client"
"go.mongodb.org/mongo-driver/mongo"
)
type ProxyConfig struct {
a *App
}
func (pc *ProxyConfig) GetCfg(key ...string) interface{} {
var k string
if key != nil && len(key) > 0 {
k = key[0]
}
return pc.a.Config(k)
}
func (a *App) ProxyConfig() proxy.Config {
return &ProxyConfig{
a: a,
}
}
type ProxyRpcClientPool struct {
a *App
groupName string
}
func (prc *ProxyRpcClientPool) GetClient(name string, serviceName string) (proxy.RpcClient, error) {
return prc.a.NetRpcClient(prc.groupName, name, serviceName)
}
func (prc *ProxyRpcClientPool) GetClientHash(name string, serviceName string) (proxy.RpcClient, error) {
return prc.a.NetRpcClient(prc.groupName, name, serviceName, prc.a.NetRpcClientWithHashSelector())
}
func (prc *ProxyRpcClientPool) GetClientSoftState(name string, serviceName string) (proxy.RpcClient, error) {
return prc.a.NetRpcClient(prc.groupName, name, serviceName, prc.a.NetRpcClientWithSoftState())
}
func (a *App) ProxyRpcClient(groupName ...string) proxy.RpcClientPool {
var name string
if groupName != nil && len(groupName) > 0 {
name = groupName[0]
}
return &ProxyRpcClientPool{
a: a,
groupName: name,
}
}
type ProxyDBMySQL struct {
a *App
}
func (pdm *ProxyDBMySQL) GetDB(name ...string) *sql.DB {
instance := pdm.a.DBMySqlInstance(name...)
if instance == nil {
return nil
}
return instance.GetConn(pdm.a.Context())
}
func (a *App) ProxyDBMySQL() proxy.DBMySQL {
return &ProxyDBMySQL{
a: a,
}
}
type ProxyDBRedis struct {
a *App
}
func (pdr *ProxyDBRedis) GetDB(name ...string) redis.Conn {
instance := pdr.a.DBRedisInstance(name...)
if instance == nil {
return nil
}
return instance.GetConn(pdr.a.Context())
}
func (a *App) ProxyDBRedis() proxy.DBRedis {
return &ProxyDBRedis{
a: a,
}
}
type ProxyDBMongo struct {
a *App
}
func (pdm *ProxyDBMongo) GetDB(name ...string) *mongo.Database {
instance := pdm.a.DBMongoInstance(name...)
if instance == nil {
return nil
}
return instance.GetConn(pdm.a.Context())
}
func (a *App) ProxyDBMongo() proxy.DBMongo {
return &ProxyDBMongo{
a: a,
}
}
type ProxyMQ struct {
a *App
}
func (pm *ProxyMQ) SendMsg(ctx context.Context, name string, topic string, msg []byte) error {
instance := pm.a.MQProducerInstance(name)
if instance == nil {
return nil
}
return instance.SendMsg(ctx, topic, &message.Producer{
Data: msg,
})
}
func (pm *ProxyMQ) SendMsgSharding(ctx context.Context, name string, key string, topic string, msg []byte) error {
instance := pm.a.MQProducerInstance(name)
if instance == nil {
return nil
}
return instance.SendMsg(ctx, topic, &message.Producer{
ShardingKey: key,
Data: msg,
})
}
func (a *App) ProxyMQ() proxy.MQ {
return &ProxyMQ{
a: a,
}
}
type ProxyDiscovery struct {
a *App
}
func (pd *ProxyDiscovery) Instance() naming_client.INamingClient {
return pd.a.discovery
}
func (pd *ProxyDiscovery) ClusterName() string {
return pd.a.DiscoveryClusterName()
}
func (a *App) ProxyDiscovery() proxy.Discovery {
return &ProxyDiscovery{
a: a,
}
}