-
Notifications
You must be signed in to change notification settings - Fork 0
/
interest.go
294 lines (253 loc) · 6.64 KB
/
interest.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package dndm
import (
"context"
"io"
"slices"
"sync"
"github.com/itohio/dndm/errors"
"google.golang.org/protobuf/proto"
)
var (
_ Interest = (*LocalInterest)(nil)
_ InterestInternal = (*LocalInterest)(nil)
_ Interest = (*FanInInterest)(nil)
_ Interest = (*InterestRouter)(nil)
_ Interest = (*interestWrapper)(nil)
)
// InterestCallback is a function type used for callbacks upon adding an Interest.
type InterestCallback func(interest Interest, ep Endpoint) error
// Interest defines the behavior for components interested in receiving named data.
// It allows closing of the interest, setting closure callbacks, and accessing routes and message channels.
//
// User should consume C of the interest until it is closed or no longer needed.
// Messages will be delivered only when a corresponding Intent is discovered.
type Interest interface {
io.Closer
OnClose(func()) Interest
Route() Route
// C returns a channel that contains messages. Users should typecast to specific message type that
// was registered with the interest.
C() <-chan proto.Message
}
// RemoteInterest extends Interest with the ability to retrieve the peer involved in the interest.
type RemoteInterest interface {
Interest
Peer() Peer
}
// InterestInternal extends Interest with additional internal management capabilities.
type InterestInternal interface {
Interest
Ctx() context.Context
MsgC() chan<- proto.Message
}
// LocalInterest manages a local interest for receiving data based on a specific route.
type LocalInterest struct {
Base
route Route
msgC chan proto.Message
}
// NewInterest creates a new LocalInterest with a specified context, route, and buffer size.
func NewInterest(ctx context.Context, route Route, size int) *LocalInterest {
ret := &LocalInterest{
Base: NewBase(),
route: route,
msgC: make(chan proto.Message, size),
}
ret.Init(ctx)
ret.OnClose(func() {
ret.cancel(nil)
close(ret.msgC)
})
return ret
}
func (t *LocalInterest) OnClose(f func()) Interest {
t.AddOnClose(f)
return t
}
func (i *LocalInterest) Route() Route {
return i.route
}
func (i *LocalInterest) C() <-chan proto.Message {
return i.msgC
}
func (i *LocalInterest) MsgC() chan<- proto.Message {
return i.msgC
}
// FanInInterest aggregates multiple interests, routing incoming messages to a single channel.
type FanInInterest struct {
li *LocalInterest
mu sync.RWMutex
wg sync.WaitGroup
interests []Interest
cancels []context.CancelFunc
onRecv func(ctx context.Context, msg proto.Message) error
}
// NewFanInInterest creates a new FanInInterest with specified context, route, size, and initial interests.
func NewFanInInterest(ctx context.Context, route Route, size int, interests ...Interest) (*FanInInterest, error) {
ret := &FanInInterest{
li: NewInterest(ctx, route, size),
}
for _, i := range interests {
if err := ret.AddInterest(i); err != nil {
return nil, err
}
}
return ret, nil
}
func (i *FanInInterest) Close() error {
err := i.li.Close()
i.wg.Wait()
return err
}
func (i *FanInInterest) OnClose(f func()) Interest {
i.li.AddOnClose(f)
return i
}
func (i *FanInInterest) Route() Route {
return i.li.Route()
}
func (i *FanInInterest) C() <-chan proto.Message {
return i.li.C()
}
func (i *FanInInterest) Ctx() context.Context {
return i.li.Ctx()
}
// AddInterest registers an interest and sets up the routing.
func (i *FanInInterest) AddInterest(interest Interest) error {
if !i.Route().Equal(interest.Route()) {
return errors.ErrInvalidRoute
}
i.mu.Lock()
defer i.mu.Unlock()
if idx := slices.Index(i.interests, interest); idx >= 0 {
return nil
}
ctx, cancel := context.WithCancel(i.Ctx())
i.interests = append(i.interests, interest)
i.cancels = append(i.cancels, cancel)
i.wg.Add(1)
interest.OnClose(cancel)
go i.recvRunner(ctx, interest)
return nil
}
func (i *FanInInterest) RemoveInterest(interest Interest) {
i.mu.Lock()
defer i.mu.Unlock()
idx := slices.Index(i.interests, interest)
if idx < 0 {
return
}
i.cancels[idx]()
i.interests = slices.Delete(i.interests, idx, idx+1)
i.cancels = slices.Delete(i.cancels, idx, idx+1)
}
func (i *FanInInterest) recvRunner(ctx context.Context, interest Interest) {
defer i.wg.Done()
for {
select {
case <-i.Ctx().Done():
return
case <-ctx.Done():
return
case msg := <-interest.C():
if i.onRecv != nil {
err := i.onRecv(ctx, msg)
if err != nil {
i.RemoveInterest(interest)
return
}
continue
}
select {
case <-i.Ctx().Done():
return
case <-ctx.Done():
return
case i.li.MsgC() <- msg:
}
}
}
}
type interestWrapper struct {
Base
router *InterestRouter
c chan proto.Message
}
func (w *interestWrapper) OnClose(f func()) Interest {
w.Base.AddOnClose(f)
return w
}
func (w *interestWrapper) Route() Route {
return w.router.Route()
}
func (w *interestWrapper) C() <-chan proto.Message {
return w.c
}
// InterestRouter manages a collection of interests, directing incoming messages to multiple subscribers.
type InterestRouter struct {
*FanInInterest
size int
mu sync.RWMutex
wrappers []*interestWrapper
}
// NewInterestRouter initializes a new InterestRouter with a context, route, size, and initial interests.
func NewInterestRouter(ctx context.Context, route Route, size int, interests ...Interest) (*InterestRouter, error) {
fii, err := NewFanInInterest(ctx, route, size, interests...)
if err != nil {
return nil, err
}
ret := &InterestRouter{
FanInInterest: fii,
size: size,
}
fii.onRecv = ret.routeMsg
return ret, nil
}
// Wrap returns a wrapped interest that collects messages from all registered interests.
func (i *InterestRouter) Wrap() *interestWrapper {
ret := &interestWrapper{
Base: NewBaseWithCtx(i.Ctx()),
router: i,
c: make(chan proto.Message, i.size),
}
i.addWrapper(ret)
return ret
}
func (i *InterestRouter) addWrapper(w *interestWrapper) {
i.mu.Lock()
defer i.mu.Unlock()
w.OnClose(func() {
i.removeWrapper(w)
close(w.c)
})
i.wrappers = append(i.wrappers, w)
}
func (i *InterestRouter) removeWrapper(w *interestWrapper) error {
i.mu.Lock()
defer i.mu.Unlock()
idx := slices.Index(i.wrappers, w)
w.router = nil
if idx >= 0 {
i.wrappers = slices.Delete(i.wrappers, idx, idx+1)
}
if len(i.wrappers) > 0 {
return nil
}
return i.Close()
}
// routeMsg routes message received by some interest to all wrappers.
func (i *InterestRouter) routeMsg(ctx context.Context, msg proto.Message) error {
i.mu.Lock()
defer i.mu.Unlock()
for _, w := range i.wrappers {
select {
case <-ctx.Done():
return ctx.Err()
case <-i.Ctx().Done():
return i.Ctx().Err()
case w.c <- msg:
default:
}
}
return nil
}