-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn.go
194 lines (186 loc) · 4.56 KB
/
conn.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package client
import (
goContext "context"
"errors"
"fmt"
"github.com/fushiliang321/go-core/consul"
"github.com/fushiliang321/go-core/context"
"github.com/fushiliang321/go-core/exception"
"github.com/fushiliang321/go-core/helper/logger"
"github.com/fushiliang321/go-core/helper/serialize"
"google.golang.org/grpc"
"google.golang.org/grpc/connectivity"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/metadata"
"sync"
)
type ClientConn struct {
serviceName string
serviceNode *consul.ServiceNode
cc *grpc.ClientConn
ccInUse bool //连接是否被使用
multiplex bool //是否复用连接
multiplexNum uint //连接已复用次数
isCloseClient bool //客户端是否关闭
sync.RWMutex
}
const maxMultiplexNum = 1000 //连接最大复用次数
func dial(serviceName string) (*grpc.ClientConn, *consul.ServiceNode, error) {
defer func() {
if err := recover(); err != nil {
logger.Error("grpc dial exception:", fmt.Sprint(err))
exception.Listener("grpc dial exception", err)
}
}()
node, err := consul.GetNode(serviceName, consul.GrpcProtocol)
if err != nil {
return nil, nil, err
}
conn, err := grpc.Dial(node.Address+":"+node.Port, grpc.WithTransportCredentials(insecure.NewCredentials()))
return conn, node, err
}
func (cc *ClientConn) Invoke(ctx goContext.Context, method string, args, reply any, opts ...grpc.CallOption) (err error) {
var con *grpc.ClientConn
if cc.multiplex {
//连接复用
cc.RLock()
defer cc.RUnlock()
if cc.isCloseClient {
return errors.New("The client is closed ")
}
if cc.serviceNode != nil && cc.serviceNode.IsRemove {
cc.Close()
}
if cc.cc == nil {
cc.RUnlock()
cc.Lock()
if cc.cc == nil {
err = func() error {
defer func() {
recover()
}()
cc.cc, cc.serviceNode, err = dial(cc.serviceName)
if err != nil {
return err
}
cc.multiplexNum = 0
return nil
}()
}
cc.Unlock()
cc.RLock()
if err != nil {
return err
}
}
con = cc.cc
defer func() {
cc.multiplexNum++
if cc.multiplexNum < maxMultiplexNum || (cc.serviceNode != nil && cc.serviceNode.IsRemove) {
return
}
//超出复用次数或服务节点信息被移除就关闭连接
cc.cc = nil
cc.serviceNode = nil
con.Close()
}()
} else {
//不复用连接的情况下 每次调用都会重新连接
con, _, err = dial(cc.serviceName)
if err != nil {
return err
}
cc.cc = con
defer func() {
cc.cc = nil
con.Close()
}()
}
if ctx == nil {
ctx = goContext.Background()
}
contextData := context.GetAll()
if contextData != nil && len(contextData) > 0 {
var str string
if str, err = serialize.JsonEncode(contextData); err == nil {
ctx = metadata.AppendToOutgoingContext(ctx, "contextData", str)
}
}
err = con.Invoke(ctx, method, args, reply, opts...)
if err != nil {
logger.Warn("grpc client Invoke error:", err)
}
return err
}
func (cc *ClientConn) NewStream(ctx goContext.Context, desc *grpc.StreamDesc, method string, opts ...grpc.CallOption) (grpc.ClientStream, error) {
if cc.cc == nil {
return nil, errors.New("连接不存在")
}
return cc.cc.NewStream(ctx, desc, method, opts...)
}
func (cc *ClientConn) GetState() connectivity.State {
if cc.cc == nil {
return connectivity.Shutdown
}
return cc.cc.GetState()
}
func (cc *ClientConn) Target() string {
if cc.cc == nil {
return ""
}
return cc.cc.Target()
}
func (cc *ClientConn) WaitForStateChange(ctx goContext.Context, sourceState connectivity.State) bool {
if cc.cc == nil {
return false
}
return cc.cc.WaitForStateChange(ctx, sourceState)
}
func (cc *ClientConn) Connect() {
if cc.cc == nil {
return
}
cc.cc.Connect()
}
func (cc *ClientConn) Close() error {
defer func() {
cc.serviceNode = nil
cc.cc = nil
if err := recover(); err != nil {
logger.Error("conn close error:", fmt.Sprint(err))
}
}()
if cc.cc == nil {
return nil
}
err := cc.cc.Close()
if err != nil {
return err
}
return nil
}
func (cc *ClientConn) ResetConnectBackoff() {
if cc.cc == nil {
return
}
cc.cc.ResetConnectBackoff()
}
func (cc *ClientConn) GetMethodConfig(method string) grpc.MethodConfig {
if cc.cc == nil {
return grpc.MethodConfig{}
}
return cc.cc.GetMethodConfig(method)
}
func GetConn(serviceName string, multiplex bool) (*ClientConn, error) {
defer func() {
if err := recover(); err != nil {
logger.Error("server middleware exception", fmt.Sprint(err))
exception.Listener("grpc conn exception", err)
}
}()
return &ClientConn{
serviceName: serviceName,
cc: nil,
multiplex: multiplex,
}, nil
}