-
Notifications
You must be signed in to change notification settings - Fork 35
/
sync.go
221 lines (194 loc) · 4.35 KB
/
sync.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
package beehive
import (
"encoding/gob"
"sync"
bhgob "github.com/kandoo/beehive/gob"
)
var (
// ErrSyncStopped returned when the sync handler is stopped before receiving
// the response.
ErrSyncStopped = bhgob.Error("sync: stopped")
// ErrSyncNoSuchRequest returned when we cannot find the request for that
// response.
ErrSyncNoSuchRequest = bhgob.Error("sync: request not found")
// ErrSyncDuplicateResponse returned when there is a duplicate repsonse to the
// sync request.
ErrSyncDuplicateResponse = bhgob.Error("sync: duplicate response")
)
// syncReq represents a generic sync request.
type syncReq struct {
ID uint64
Data interface{} // Data of the request. Must be registered in gob.
}
// Type returns the type of this request. It is unique for each data type.
func (r syncReq) Type() string {
if r.Data == nil {
return "sync-req"
}
return "sync-req-" + MsgType(r.Data)
}
// syncRes represents a generic sync reponse.
type syncRes struct {
ID uint64 // ID is the request ID.
Data interface{} // Data of the response. Must be registered in gob.
Err error // Err is error, if any.
}
// Type returns the type of this response. It is unique for each data type.
func (r syncRes) Type() string {
if r.Data == nil {
return "syncRes"
}
return "syncRes-" + MsgType(r.Data)
}
type syncReqAndChan struct {
req syncReq
ch chan syncRes
}
// syncDetached is a generic DetachedHandler for sync request processing, and
// also provides Handle, HandleFunc, and Process for the clients.
type syncDetached struct {
sync.Mutex
reqs map[uint64]chan syncRes
reqch chan syncReqAndChan
done chan chan struct{}
}
// newSync creates a sync detached for the application that retrieves its
// requests from ch.
func newSync(a App, ch chan syncReqAndChan) *syncDetached {
s := &syncDetached{
reqs: make(map[uint64]chan syncRes),
reqch: ch,
done: make(chan chan struct{}),
}
a.Detached(s)
return s
}
// Start is to implement DetachedHandler.
func (s *syncDetached) Start(ctx RcvContext) {
for {
select {
case ch := <-s.done:
s.drain()
ch <- struct{}{}
case rnc := <-s.reqch:
s.enque(rnc.req.ID, rnc.ch)
ctx.Emit(rnc.req)
}
}
}
// Stop is to implement DetachedHandler.
func (s *syncDetached) Stop(ctx RcvContext) {
ack := make(chan struct{})
s.done <- ack
<-ack
}
// Rcv is to implement DetachedHandler.
func (s *syncDetached) Rcv(msg Msg, ctx RcvContext) error {
res := msg.Data().(syncRes)
ch, err := s.deque(res.ID)
if err != nil {
return err
}
ch <- res
return nil
}
func (s *syncDetached) drain() {
for id, ch := range s.reqs {
s.Lock()
ch <- syncRes{
ID: id,
Err: ErrSyncStopped,
}
delete(s.reqs, id)
}
s.Unlock()
}
func (s *syncDetached) enque(id uint64, ch chan syncRes) {
s.Lock()
s.reqs[id] = ch
s.Unlock()
}
func (s *syncDetached) deque(id uint64) (chan syncRes, error) {
s.Lock()
ch, ok := s.reqs[id]
s.Unlock()
if !ok {
return nil, ErrSyncNoSuchRequest
}
delete(s.reqs, id)
return ch, nil
}
type syncRcvContext struct {
RcvContext
id uint64
from uint64
replied bool
}
func (ctx *syncRcvContext) Reply(msg Msg, replyData interface{}) error {
if msg.From() != ctx.from {
return ctx.RcvContext.Reply(msg, replyData)
}
if ctx.replied {
return ErrSyncDuplicateResponse
}
ctx.replied = true
r := syncRes{
ID: ctx.id,
Data: replyData,
}
return ctx.RcvContext.Reply(msg, r)
}
func (ctx *syncRcvContext) DeferReply(msg Msg) Repliable {
ctx.replied = true
return Repliable{
From: msg.From(),
SyncID: ctx.id,
}
}
type syncHandler struct {
handler Handler
}
func (h syncHandler) Rcv(m Msg, ctx RcvContext) error {
req := m.Data().(syncReq)
sm := msg{
MsgData: req.Data,
MsgFrom: m.From(),
MsgTo: m.To(),
}
sc := syncRcvContext{
RcvContext: ctx,
id: req.ID,
from: m.From(),
}
err := h.handler.Rcv(sm, &sc)
if err != nil {
ctx.AbortTx()
r := syncRes{
ID: req.ID,
Err: bhgob.Error(err.Error()),
}
ctx.Reply(m, r)
return err
}
if !sc.replied {
r := syncRes{
ID: req.ID,
}
ctx.Reply(m, r)
}
return nil
}
func (h syncHandler) Map(m Msg, ctx MapContext) MappedCells {
s := msg{
MsgData: m.Data().(syncReq).Data,
MsgFrom: m.From(),
MsgTo: m.To(),
}
return h.handler.Map(s, ctx)
}
func init() {
gob.Register(syncReq{})
gob.Register(syncRes{})
}
var _ DetachedHandler = &syncDetached{}
var _ Handler = syncHandler{}