forked from tidwall/evio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dubbo_agent.go
202 lines (173 loc) · 5.66 KB
/
dubbo_agent.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
package main
import (
"flag"
"sync"
"time"
"github.com/shabicheng/evio/logger"
dubbocodec "github.com/shabicheng/evio/codec"
dubbojson "github.com/shabicheng/evio/codec/jsonrpc"
)
var dubboPort = flag.Int("dubbo-port", 20889, "")
var dubboHost = flag.String("dubbo-host", "127.0.0.1", "")
var dubboConnCount = flag.Int("dubbo-conn", 2, "")
var GlobalLocalDubboAgent LocalDubboAgent
type LocalDubboAgent struct {
requestMap sync.Map // key id, val agent request
server *server
workerRespQueue chan *DubboReponse
connManager ConnectionManager
}
type DubboReponse dubbojson.Response
type DubboContext struct {
is InputStream
}
func CreateDubboEvent(loops int, workerQueue chan *DubboReponse) *Events {
events := &Events{}
events.NumLoops = loops
events.Serving = func(srv Server) (action Action) {
logger.Info("dubbo agent server started (loops: %d)", srv.NumLoops)
return
}
events.Opened = func(c Conn) (out []byte, opts Options, action Action) {
lastCtx := c.Context()
if lastCtx == nil {
c.SetContext(&DubboContext{})
}
logger.Info("dubbo agent opened: laddr: %v: raddr: %v", c.LocalAddr(), c.RemoteAddr())
return
}
// producer 向dubbo 发起的链接,在close的时候需要销毁
// 删除dubbo的连接
events.Closed = func(c Conn, err error) (action Action) {
logger.Info("dubbo agent closed: %s: %s", c.LocalAddr(), c.RemoteAddr())
GlobalLocalDubboAgent.connManager.DeleteConnection(c)
GlobalLocalDubboAgent.GetConnection()
return
}
events.Data = func(c Conn, in []byte) (out []byte, action Action) {
//logger.Info("Data: laddr: %v: raddr: %v, data", c.LocalAddr(), c.RemoteAddr(), string(in))
if in == nil {
return
}
agentContext := c.Context().(*DubboContext)
data := agentContext.is.Begin(in)
// process the pipeline
for {
//logger.Info("data %v\n", data)
leftover, resp, err := dubbojson.UnpackResponse(data)
//logger.Info("result %v %v %v \n", leftover, err, ready)
if err != nil {
if err == dubbocodec.ErrHeaderNotEnough {
// request not ready, yet
data = leftover
break
} else {
// bad thing happened
action = Close
break
}
}
//fmt.Printf("insert %v \n", *resp)
//AppendRequest(out, httpContext.req)
workerQueue <- (*DubboReponse)(resp)
// handle the request
data = leftover
}
agentContext.is.End(data)
return
}
return events
}
func (lda *LocalDubboAgent) SendDubboRequest(req *AgentRequest) {
attachments := make(map[string]string)
attachments["path"] = req.Interf
// send to dubbo agent
message := &dubbocodec.Message{
ID: int64(req.RequestID),
Type: dubbocodec.Request,
Interface: req.Interf, // Service
Method: req.Method,
Data: &dubbocodec.RpcInvocation{
Method: req.Method,
ParameterTypes: req.ParamType.String(),
Args: req.Param,
Attachments: attachments,
},
}
data, e := dubbojson.PackRequest(message)
if e != nil {
// do something
}
lda.requestMap.Store(req.RequestID, req)
lda.GetConnection().Send(data)
req.profileRemoteAgentSendDubboTime = time.Now()
}
func (lda *LocalDubboAgent) ServeConnectDubbo(loops int) error {
lda.workerRespQueue = make(chan *DubboReponse, 1000)
for i := 0; i < *providerDubboProcessors; i++ {
go func() {
for resp := range lda.workerRespQueue {
//fmt.Printf("insert %v \n", *resp)
obj, ok := GlobalLocalDubboAgent.requestMap.Load(uint64(resp.ID))
if !ok {
logger.Info("receive dubbo client's response, but no this req id %d", int(resp.ID))
continue
}
agentReq := obj.(*AgentRequest)
if !resp.Status.OK() {
logger.Info("receive dubbo client's response, but status not OK ", int(resp.ID), resp.Status.String())
// time.Sleep(time.Millisecond * 20)
// GlobalLocalDubboAgent.SendDubboRequest(agentReq)
// continue
}
//logger.Info("receive dubbo client's response, ", int(resp.ID), string(resp.Data))
agentReq.profileRemoteAgentGetDubboTime = time.Now()
if true {
// 直接打包成http
SendAgentRequest(agentReq.conn, 200, agentReq.RequestID, "", "", ParamType_Result, AppendResp(nil, "200 OK", "", string(resp.Data)))
} else {
SendAgentRequest(agentReq.conn, 200, agentReq.RequestID, agentReq.Interf, agentReq.Method, agentReq.ParamType, resp.Data)
}
GlobalLocalDubboAgent.requestMap.Delete(uint64(resp.ID))
agentReq.profileRemoteAgentSendAgentTime = time.Now()
ProfileLogger.Info(agentReq.profileRemoteAgentSendAgentTime.Sub(agentReq.profileRemoteAgentGetTime),
agentReq.profileRemoteAgentGetDubboTime.Sub(agentReq.profileRemoteAgentSendDubboTime),
agentReq.profileRemoteAgentSendAgentTime.Sub(agentReq.profileRemoteAgentSendDubboTime),
agentReq.profileRemoteAgentSendAgentTime.Sub(agentReq.profileRemoteAgentGetTime)-agentReq.profileRemoteAgentGetDubboTime.Sub(agentReq.profileRemoteAgentSendDubboTime))
}
}()
}
events := CreateDubboEvent(4, lda.workerRespQueue)
var err error
lda.server, err = ConnServe(*events)
return err
}
func (lda *LocalDubboAgent) GetConnection() Conn {
resultConn, connCount := lda.connManager.GetConnection()
createConnCount := *dubboConnCount - connCount
makeConn := func() (Conn, error) {
conn, err := outConnect(lda.server, *dubboHost, *dubboPort, &DubboContext{})
if err != nil {
logger.Warning("CONNECT_DUBBO_ERROR", err, *dubboPort, *dubboPort)
return nil, err
}
lda.connManager.AddConnection(conn)
return conn, nil
}
if connCount == 0 {
for true {
conn, err := makeConn()
if err == nil {
resultConn = conn
break
}
time.Sleep(100 * time.Millisecond)
}
createConnCount--
}
for createConnCount > 0 {
go makeConn()
createConnCount--
}
return resultConn.(Conn)
}