-
Notifications
You must be signed in to change notification settings - Fork 0
/
dndm.go
175 lines (154 loc) · 3.71 KB
/
dndm.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
package dndm
import (
"log/slog"
"sync"
"google.golang.org/protobuf/proto"
)
// CauseCloser interface for objects that accepts closure reason
type CauseCloser interface {
CloseCause(e error) error
}
type Router struct {
Base
mu sync.Mutex
log *slog.Logger
size int
endpoints []Endpoint
intentRouters map[string]*IntentRouter
interestRouters map[string]*InterestRouter
}
func New(opts ...Option) (*Router, error) {
opt := defaultOptions()
if err := opt.Config(opts...); err != nil {
return nil, err
}
ret := &Router{
Base: NewBaseWithCtx(opt.ctx),
log: opt.logger.With("module", "router"),
endpoints: make([]Endpoint, len(opt.endpoints)),
intentRouters: make(map[string]*IntentRouter),
interestRouters: make(map[string]*InterestRouter),
size: opt.size,
}
for i, t := range opt.endpoints {
log := opt.logger.With("endpoint", t.Name())
if err := t.Init(ret.ctx, log, func(intent Intent, ep Endpoint) error { return nil }, ret.addInterest); err != nil {
return nil, err
}
ret.endpoints[i] = t
}
return ret, nil
}
func (d *Router) addInterest(interest Interest, t Endpoint) error {
route := interest.Route()
go func() {
d.mu.Lock()
defer d.mu.Unlock()
ir, ok := d.interestRouters[route.ID()]
if ok {
ir.AddInterest(interest)
return
}
ir, err := NewInterestRouter(d.ctx, route,
d.size,
interest,
)
ir.OnClose(func() {
d.mu.Lock()
defer d.mu.Unlock()
delete(d.interestRouters, route.ID())
})
if err != nil {
panic(err)
}
d.interestRouters[route.ID()] = ir
}()
return nil
}
func (d *Router) removeInterest(interest Interest, t Endpoint) error {
route := interest.Route()
go func() {
d.mu.Lock()
defer d.mu.Unlock()
ir, ok := d.interestRouters[route.ID()]
if !ok {
return
}
ir.RemoveInterest(interest)
}()
return nil
}
// Publish delivers data to an interested party. It may advertise the availability of the data if no interest is found.
func (d *Router) Publish(path string, msg proto.Message, opt ...PubOpt) (Intent, error) {
route, err := NewRoute(path, msg)
if err != nil {
return nil, err
}
d.mu.Lock()
defer d.mu.Unlock()
intents := make([]Intent, 0, len(d.endpoints))
// Advertise intents even if we are already publishing
for _, t := range d.endpoints {
intent, err := t.Publish(route)
if err != nil {
closeAll(intents...)
return nil, err
}
intents = append(intents, intent)
}
ir, ok := d.intentRouters[route.ID()]
if ok {
return ir.Wrap(), nil
}
ir, err = NewIntentRouter(d.ctx, route,
d.size,
intents...,
)
ir.OnClose(func() {
d.mu.Lock()
defer d.mu.Unlock()
delete(d.intentRouters, route.ID())
})
if err != nil {
return nil, err
}
d.intentRouters[route.ID()] = ir
return ir.Wrap(), nil
}
// Subscribe advertises an interest in a specific message type on particular path.
func (d *Router) Subscribe(path string, msg proto.Message, opt ...SubOpt) (Interest, error) {
route, err := NewRoute(path, msg)
if err != nil {
return nil, err
}
d.mu.Lock()
defer d.mu.Unlock()
// Advertise interests anyway (even if we are already subscribed)
interests := make([]Interest, 0, len(d.endpoints))
for _, t := range d.endpoints {
interest, err := t.Subscribe(route)
if err != nil {
closeAll(interests...)
return nil, err
}
interests = append(interests, interest)
}
ir, ok := d.interestRouters[route.ID()]
if ok {
return ir.Wrap(), nil
}
ir, err = NewInterestRouter(d.ctx, route,
d.size,
interests...,
)
ir.OnClose(func() {
d.mu.Lock()
defer d.mu.Unlock()
delete(d.interestRouters, route.ID())
})
if err != nil {
return nil, err
}
d.interestRouters[route.ID()] = ir
return ir.Wrap(), nil
}