-
Notifications
You must be signed in to change notification settings - Fork 0
/
sink.go
260 lines (183 loc) · 5.11 KB
/
sink.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package gorpc
import (
"bytes"
"sync"
"time"
"github.com/gsdocker/gserrors"
"github.com/gsdocker/gslogger"
"github.com/gsrpc/gorpc/timer"
)
// Sink .
type Sink interface {
Channel
AddService(dispatcher Dispatcher)
RemoveService(dispatcher Dispatcher)
ClearServices()
MessageReceived(message *Message) error
}
type _Sink struct {
gslogger.Log // Mixin Log APIs
sync.RWMutex // mutex
name string // name
timeout time.Duration // timeout
seqID uint32 // sequence id
dispatchers map[uint16]Dispatcher // register dispatchers
promises map[uint32]Promise // rpc promise
channel MessageChannel // channel
timeWheel *timer.Wheel // pipeline
}
// NewSink .
func NewSink(name string, channel MessageChannel, timeWheel *timer.Wheel, timeout time.Duration) Sink {
return &_Sink{
Log: gslogger.Get("sink"),
name: name,
timeout: timeout,
dispatchers: make(map[uint16]Dispatcher),
promises: make(map[uint32]Promise),
channel: channel,
timeWheel: timeWheel,
}
}
// Register .
func (sink *_Sink) AddService(dispatcher Dispatcher) {
sink.Lock()
defer sink.Unlock()
sink.dispatchers[dispatcher.ID()] = dispatcher
}
// Unreigster .
func (sink *_Sink) RemoveService(dispatcher Dispatcher) {
sink.Lock()
defer sink.Unlock()
if dispatcher, ok := sink.dispatchers[dispatcher.ID()]; ok && dispatcher == dispatcher {
delete(sink.dispatchers, dispatcher.ID())
}
}
// ClearServices .
func (sink *_Sink) ClearServices() {
sink.Lock()
defer sink.Unlock()
sink.dispatchers = make(map[uint16]Dispatcher)
}
func (sink *_Sink) Promise() (Promise, uint32) {
sink.Lock()
defer sink.Unlock()
for {
seqID := sink.seqID
sink.seqID++
_, ok := sink.promises[seqID]
if ok {
continue
}
promise := NewPromise(sink.timeWheel, sink.timeout, func() {
sink.Lock()
defer sink.Unlock()
delete(sink.promises, seqID)
})
sink.promises[seqID] = promise
return promise, seqID
}
}
// Post .
func (sink *_Sink) Post(call *Request) error {
var buff bytes.Buffer
err := WriteRequest(&buff, call)
if err != nil {
return err
}
message := NewMessage()
message.Code = CodeRequest
message.Content = buff.Bytes()
err = sink.channel.SendMessage(message)
if err != nil {
return err
}
sink.V("%s post request(%d:%d:%d)", sink.name, call.ID, call.Service, call.Method)
return nil
}
// Send .
func (sink *_Sink) Send(call *Request) (Future, error) {
sink.V("%s send request(%d:%d:%d)", sink.name, call.ID, call.Service, call.Method)
promise, id := sink.Promise()
call.ID = id
var buff bytes.Buffer
err := WriteRequest(&buff, call)
if err != nil {
promise.Cancel()
return nil, err
}
message := NewMessage()
message.Code = CodeRequest
message.Content = buff.Bytes()
err = sink.channel.SendMessage(message)
if err != nil {
promise.Cancel()
return nil, err
}
sink.V("%s send request(%d:%d:%d) -- success", sink.name, call.ID, call.Service, call.Method)
return promise, nil
}
func (sink *_Sink) dispatchResponse(response *Response) {
sink.Lock()
defer sink.Unlock()
if promise, ok := sink.promises[response.ID]; ok {
go promise.Notify(response, nil)
delete(sink.promises, response.ID)
return
}
sink.W("%s unhandle response(%d) %s", sink.name, response.ID, gserrors.Newf(nil, ""))
}
func (sink *_Sink) dispatch(id uint16) (dispatcher Dispatcher, ok bool) {
sink.RLock()
defer sink.RUnlock()
dispatcher, ok = sink.dispatchers[id]
return
}
func (sink *_Sink) dispatchRequest(message *Message) error {
request, err := ReadRequest(bytes.NewBuffer(message.Content))
if err != nil {
sink.E("[%s] unmarshal request error\n%s", sink.name, err)
return err
}
sink.V("%s received request(%d:%d:%d)", sink.name, request.ID, request.Service, request.Method)
if dispatcher, ok := sink.dispatch(request.Service); ok {
response, err := dispatcher.Dispatch(request)
if err != nil {
sink.E("dispatch request(%d)(%d:%d) error\n%s", request.ID, request.Service, request.Method, err)
return nil
}
if response == nil {
return nil
}
var buff bytes.Buffer
err = WriteResponse(&buff, response)
if err != nil {
sink.E("marshal request(%d)(%d:%d)'s response error\n%s", request.ID, request.Service, request.Method, err)
return nil
}
message.Code = CodeResponse
message.Content = buff.Bytes()
sink.channel.SendMessage(message)
sink.V("[%s] send response(%d)(%d)", sink.name, response.ID, response.Exception)
return nil
}
sink.W("[%s] unhandle request(%d)(%d:%d)", sink.name, request.ID, request.Service, request.Method)
return nil
}
func (sink *_Sink) MessageReceived(message *Message) error {
switch message.Code {
case CodeRequest:
sink.dispatchRequest(message)
return nil
case CodeResponse:
response, err := ReadResponse(bytes.NewBuffer(message.Content))
if err != nil {
sink.E("[%s] unmarshal response error\n%s", sink.name, err)
return err
}
sink.dispatchResponse(response)
return nil
default:
sink.W("[%s] unsupport message(%s)", sink.name, message.Code)
return nil
}
}