forked from TarsCloud/TarsGo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadapter.go
executable file
·204 lines (182 loc) · 5.21 KB
/
adapter.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
195
196
197
198
199
200
201
202
203
204
package tars
import (
"context"
"fmt"
"sync"
"sync/atomic"
"time"
"github.com/TarsCloud/TarsGo/tars/protocol/res/basef"
"github.com/TarsCloud/TarsGo/tars/protocol/res/endpointf"
"github.com/TarsCloud/TarsGo/tars/protocol/res/requestf"
"github.com/TarsCloud/TarsGo/tars/transport"
"github.com/TarsCloud/TarsGo/tars/util/rtimer"
)
var reconnectMsg = "_reconnect_"
// AdapterProxy : Adapter proxy
type AdapterProxy struct {
resp sync.Map
point *endpointf.EndpointF
tarsClient *transport.TarsClient
conf *transport.TarsClientConf
comm *Communicator
obj *ServantProxy
failCount int32
lastFailCount int32
sendCount int32
successCount int32
status bool // true for good
lastSuccessTime int64
lastBlockTime int64
lastCheckTime int64
count int
closed bool
}
// NewAdapterProxy : Construct an adapter proxy
func NewAdapterProxy(point *endpointf.EndpointF, comm *Communicator) *AdapterProxy {
c := &AdapterProxy{}
c.comm = comm
c.point = point
proto := "tcp"
if point.Istcp == 0 {
proto = "udp"
}
conf := &transport.TarsClientConf{
Proto: proto,
//NumConnect: netthread,
QueueLen: comm.Client.ClientQueueLen,
IdleTimeout: comm.Client.ClientIdleTimeout,
ReadTimeout: comm.Client.ClientReadTimeout,
WriteTimeout: comm.Client.ClientWriteTimeout,
DialTimeout: comm.Client.ClientDialTimeout,
}
c.conf = conf
c.tarsClient = transport.NewTarsClient(fmt.Sprintf("%s:%d", point.Host, point.Port), c, conf)
c.status = true
return c
}
// ParsePackage : Parse packet from bytes
func (c *AdapterProxy) ParsePackage(buff []byte) (int, int) {
return c.obj.proto.ParsePackage(buff)
}
// Recv : Recover read channel when closed for timeout
func (c *AdapterProxy) Recv(pkg []byte) {
defer func() {
// TODO readCh has a certain probability to be closed after the load, and we need to recover
// Maybe there is a better way
if err := recover(); err != nil {
TLOG.Error("recv pkg painc:", err)
}
}()
packet, err := c.obj.proto.ResponseUnpack(pkg)
if err != nil {
TLOG.Error("decode packet error", err.Error())
return
}
if packet.IRequestId == 0 {
go c.onPush(packet)
return
}
if packet.CPacketType == basef.TARSONEWAY {
return
}
chIF, ok := c.resp.Load(packet.IRequestId)
if ok {
ch := chIF.(chan *requestf.ResponsePacket)
select {
case ch <- packet:
//after conf.ReadTimeout, release this goroutine to make sure response package is received by Tars_Invoke().
case <-rtimer.After(c.conf.ReadTimeout):
TLOG.Errorf("response timeout, write channel error, now time :%v, RequestId:%v",
time.Now().UnixNano()/1e6, packet.IRequestId)
}
} else {
TLOG.Errorf("response timeout, req has been drop, now time :%v, RequestId:%v",
time.Now().UnixNano()/1e6, packet.IRequestId)
}
}
// Send : Send packet
func (c *AdapterProxy) Send(req *requestf.RequestPacket) error {
TLOG.Debug("send req:", req.IRequestId)
c.sendAdd()
sbuf, err := c.obj.proto.RequestPack(req)
if err != nil {
TLOG.Debug("protocol wrong:", req.IRequestId)
return err
}
return c.tarsClient.Send(sbuf)
}
// GetPoint : Get an endpoint
func (c *AdapterProxy) GetPoint() *endpointf.EndpointF {
return c.point
}
// Close : Close the client
func (c *AdapterProxy) Close() {
c.tarsClient.Close()
c.closed = true
}
func (c *AdapterProxy) sendAdd() {
atomic.AddInt32(&c.sendCount, 1)
}
func (c *AdapterProxy) succssAdd() {
now := time.Now().Unix()
atomic.SwapInt64(&c.lastSuccessTime, now)
atomic.AddInt32(&c.successCount, 1)
atomic.SwapInt32(&c.lastFailCount, 0)
}
func (c *AdapterProxy) failAdd() {
atomic.AddInt32(&c.lastFailCount, 1)
atomic.AddInt32(&c.failCount, 1)
}
func (c *AdapterProxy) reset() {
now := time.Now().Unix()
atomic.SwapInt32(&c.sendCount, 0)
atomic.SwapInt32(&c.successCount, 0)
atomic.SwapInt32(&c.failCount, 0)
atomic.SwapInt32(&c.lastFailCount, 0)
atomic.SwapInt64(&c.lastBlockTime, now)
atomic.SwapInt64(&c.lastCheckTime, now)
c.status = true
}
func (c *AdapterProxy) checkActive() (firstTime bool, needCheck bool) {
if c.closed {
return false, false
}
now := time.Now().Unix()
if c.status {
//check if healthy
if (now-c.lastSuccessTime) >= failInterval && c.lastFailCount >= fainN {
c.status = false
c.lastBlockTime = now
return true, false
}
if (now - c.lastCheckTime) >= checkTime {
if c.failCount >= overN && (float32(c.failCount)/float32(c.sendCount)) >= failRatio {
c.status = false
c.lastBlockTime = now
return true, false
}
c.lastCheckTime = now
return false, false
}
return false, false
}
if (now - c.lastBlockTime) >= tryTimeInterval {
c.lastBlockTime = now
if err := c.tarsClient.ReConnect(); err != nil {
return false, false
}
return false, true
}
return false, false
}
func (c *AdapterProxy) onPush(pkg *requestf.ResponsePacket) {
if pkg.SResultDesc == reconnectMsg {
TLOG.Infof("reconnect %s:%d", c.point.Host, c.point.Port)
oldClient := c.tarsClient
c.tarsClient = transport.NewTarsClient(fmt.Sprintf("%s:%d", c.point.Host, c.point.Port), c, c.conf)
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*ClientIdleTimeout)
defer cancel()
oldClient.GraceClose(ctx) // grace shutdown
}
//TODO: support push msg
}