-
Notifications
You must be signed in to change notification settings - Fork 211
/
msgserver.go
247 lines (222 loc) · 7.96 KB
/
msgserver.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
// Package server is used to wrap the p2p services to define multiple req-res messages under one protocol.
package server
import (
"container/list"
"fmt"
"github.com/spacemeshos/go-spacemesh/log"
"github.com/spacemeshos/go-spacemesh/p2p/p2pcrypto"
"github.com/spacemeshos/go-spacemesh/p2p/service"
"runtime"
"sync"
"sync/atomic"
"time"
)
// MessageType is a uint32 used to distinguish between server messages inside a single protocol.
type MessageType uint32
// Message is helper type for `MessegeServer` messages.
type Message interface {
service.DirectMessage
Data() service.Data
}
func extractPayload(m Message) []byte {
data := m.Data().(*service.DataMsgWrapper)
return data.Payload
}
// Item is queue entry used to match responds to sent requests.
type Item struct {
id uint64
timestamp time.Time
}
// ResponseHandlers contains handlers for received response handlers
type ResponseHandlers struct {
okCallback func(msg []byte)
failCallBack func(err error)
}
// MessageServer is a request-response multiplexer on top of the p2p layer. it provides a way to register
// message types on top of a protocol and declare request and response handlers. it matches incoming responses to requests.
type MessageServer struct {
log.Log
ReqID uint64 //request id
name string //server name
network Service
pendMutex sync.RWMutex
pendingQueue *list.List //queue of pending messages
resHandlers map[uint64]ResponseHandlers //response handlers by request ReqID
msgRequestHandlers map[MessageType]func(message Message) []byte //request handlers by request type
ingressChannel chan service.DirectMessage //chan to relay messages into the server
requestLifetime time.Duration //time a request can stay in the pending queue until evicted
workerCount sync.WaitGroup
workerLimiter chan struct{}
exit chan struct{}
}
// Service is the subset of method used by MessageServer for p2p communications.
type Service interface {
RegisterDirectProtocolWithChannel(protocol string, ingressChannel chan service.DirectMessage) chan service.DirectMessage
SendWrappedMessage(nodeID p2pcrypto.PublicKey, protocol string, payload *service.DataMsgWrapper) error
}
// NewMsgServer registers a protocol and returns a new server to declare request and response handlers on.
func NewMsgServer(network Service, name string, requestLifetime time.Duration, c chan service.DirectMessage, logger log.Log) *MessageServer {
p := &MessageServer{
Log: logger,
name: name,
resHandlers: make(map[uint64]ResponseHandlers),
pendingQueue: list.New(),
network: network,
ingressChannel: network.RegisterDirectProtocolWithChannel(name, c),
msgRequestHandlers: make(map[MessageType]func(message Message) []byte),
requestLifetime: requestLifetime,
exit: make(chan struct{}),
workerLimiter: make(chan struct{}, runtime.NumCPU()),
}
go p.readLoop()
return p
}
// Close stops the MessageServer
func (p *MessageServer) Close() {
p.exit <- struct{}{}
<-p.exit
p.workerCount.Wait()
}
// readLoop reads incoming messages and matches them to requests or responses.
func (p *MessageServer) readLoop() {
timer := time.NewTicker(p.requestLifetime + time.Millisecond*100)
defer timer.Stop()
for {
select {
case <-p.exit:
p.Debug("shutting down protocol ", p.name)
close(p.exit)
return
case <-timer.C:
go p.cleanStaleMessages()
case msg, ok := <-p.ingressChannel:
p.Debug("new msg received from channel")
if !ok {
p.Error("read loop channel was closed")
return
}
p.workerCount.Add(1)
p.workerLimiter <- struct{}{}
go func(msg Message) {
p.handleMessage(msg)
<-p.workerLimiter
p.workerCount.Done()
}(msg.(Message))
}
}
}
// clean stale messages after requests life time expire.
func (p *MessageServer) cleanStaleMessages() {
for {
p.pendMutex.RLock()
elem := p.pendingQueue.Front()
p.pendMutex.RUnlock()
if elem != nil {
item := elem.Value.(Item)
if time.Since(item.timestamp) > p.requestLifetime {
p.Debug("cleanStaleMessages remove request ", item.id)
p.pendMutex.RLock()
foo, okFoo := p.resHandlers[item.id]
p.pendMutex.RUnlock()
if okFoo {
foo.failCallBack(fmt.Errorf("response timeout"))
}
p.removeFromPending(item.id)
} else {
p.Debug("cleanStaleMessages no more stale messages")
return
}
} else {
p.Debug("cleanStaleMessages queue empty")
return
}
}
}
func (p *MessageServer) removeFromPending(reqID uint64) {
var next *list.Element
p.pendMutex.Lock()
for e := p.pendingQueue.Front(); e != nil; e = next {
next = e.Next()
if reqID == e.Value.(Item).id {
p.pendingQueue.Remove(e)
p.Debug("removed request ", e.Value.(Item).id)
break
}
}
p.Debug("delete request result %v handler", reqID)
delete(p.resHandlers, reqID)
p.pendMutex.Unlock()
}
func (p *MessageServer) handleMessage(msg Message) {
data := msg.Data().(*service.DataMsgWrapper)
if data.Req {
p.handleRequestMessage(msg, data)
} else {
p.handleResponseMessage(data)
}
}
func (p *MessageServer) handleRequestMessage(msg Message, data *service.DataMsgWrapper) {
p.Debug("handleRequestMessage start")
foo, okFoo := p.msgRequestHandlers[MessageType(data.MsgType)]
if !okFoo {
p.With().Error("handler missing for request", log.Uint64("request_id", data.ReqID), log.String("protocol", p.name), log.Uint32("msg_type", data.MsgType))
return
}
p.Debug("handle request type %v", data.MsgType)
rmsg := &service.DataMsgWrapper{MsgType: data.MsgType, ReqID: data.ReqID, Payload: foo(msg)}
if sendErr := p.network.SendWrappedMessage(msg.Sender(), p.name, rmsg); sendErr != nil {
p.Error("Error sending response message: %s", sendErr)
}
p.Debug("handleRequestMessage close")
}
func (p *MessageServer) handleResponseMessage(headers *service.DataMsgWrapper) {
//get and remove from pendingMap
p.Log.With().Debug("handleResponseMessage", log.Uint64("req_id", headers.ReqID))
p.pendMutex.RLock()
foo, okFoo := p.resHandlers[headers.ReqID]
p.pendMutex.RUnlock()
p.removeFromPending(headers.ReqID)
if okFoo {
foo.okCallback(headers.Payload)
} else {
p.Error("Cant find handler %v", headers.ReqID)
}
p.Debug("handleResponseMessage close")
}
// RegisterMsgHandler sets the handler to act on a specific message request.
func (p *MessageServer) RegisterMsgHandler(msgType MessageType, reqHandler func(message Message) []byte) {
p.msgRequestHandlers[msgType] = reqHandler
}
func handlerFromBytesHandler(in func(msg []byte) []byte) func(message Message) []byte {
return func(message Message) []byte {
payload := extractPayload(message)
return in(payload)
}
}
// RegisterBytesMsgHandler sets the handler to act on a specific message request.
func (p *MessageServer) RegisterBytesMsgHandler(msgType MessageType, reqHandler func([]byte) []byte) {
p.RegisterMsgHandler(msgType, handlerFromBytesHandler(reqHandler))
}
// SendRequest sends a request of a specific message.
func (p *MessageServer) SendRequest(msgType MessageType, payload []byte, address p2pcrypto.PublicKey, resHandler func(msg []byte), timeoutHandler func(err error)) error {
reqID := p.newReqID()
p.pendMutex.Lock()
p.resHandlers[reqID] = ResponseHandlers{resHandler, timeoutHandler}
p.pendingQueue.PushBack(Item{id: reqID, timestamp: time.Now()})
p.pendMutex.Unlock()
msg := &service.DataMsgWrapper{Req: true, ReqID: reqID, MsgType: uint32(msgType), Payload: payload}
if sendErr := p.network.SendWrappedMessage(address, p.name, msg); sendErr != nil {
p.With().Error("sending message failed",
log.Uint32("msg_type", uint32(msgType)),
address.Field("recipient"),
log.Int("msglen", len(payload)),
log.Err(sendErr))
p.removeFromPending(reqID)
return sendErr
}
p.Log.With().Debug("sent request", log.Uint64("req_id", reqID))
return nil
}
func (p *MessageServer) newReqID() uint64 {
return atomic.AddUint64(&p.ReqID, 1)
}