@@ -22,20 +22,16 @@ type ArgsCommonHub struct {
22
22
}
23
23
24
24
type commonHub struct {
25
- filter filters.EventFilter
26
- subscriptionMapper dispatcher.SubscriptionMapperHandler
27
- mutDispatchers sync.RWMutex
28
- dispatchers map [uuid.UUID ]dispatcher.EventDispatcher
29
- register chan dispatcher.EventDispatcher
30
- unregister chan dispatcher.EventDispatcher
31
- broadcast chan data.BlockEvents
32
- broadcastRevert chan data.RevertBlock
33
- broadcastFinalized chan data.FinalizedBlock
34
- broadcastTxs chan data.BlockTxs
35
- broadcastBlockEventsWithOrder chan data.BlockEventsWithOrder
36
- broadcastScrs chan data.BlockScrs
37
- closeChan chan struct {}
38
- cancelFunc func ()
25
+ filter filters.EventFilter
26
+ subscriptionMapper dispatcher.SubscriptionMapperHandler
27
+ mutDispatchers sync.RWMutex
28
+ dispatchers map [uuid.UUID ]dispatcher.EventDispatcher
29
+ register chan dispatcher.EventDispatcher
30
+ unregister chan dispatcher.EventDispatcher
31
+
32
+ cancelFunc func ()
33
+ closeChan chan struct {}
34
+ mutState sync.RWMutex
39
35
}
40
36
41
37
// NewCommonHub creates a new commonHub instance
@@ -46,19 +42,13 @@ func NewCommonHub(args ArgsCommonHub) (*commonHub, error) {
46
42
}
47
43
48
44
return & commonHub {
49
- mutDispatchers : sync.RWMutex {},
50
- filter : args .Filter ,
51
- subscriptionMapper : args .SubscriptionMapper ,
52
- dispatchers : make (map [uuid.UUID ]dispatcher.EventDispatcher ),
53
- register : make (chan dispatcher.EventDispatcher ),
54
- unregister : make (chan dispatcher.EventDispatcher ),
55
- broadcast : make (chan data.BlockEvents ),
56
- broadcastRevert : make (chan data.RevertBlock ),
57
- broadcastFinalized : make (chan data.FinalizedBlock ),
58
- broadcastTxs : make (chan data.BlockTxs ),
59
- broadcastBlockEventsWithOrder : make (chan data.BlockEventsWithOrder ),
60
- broadcastScrs : make (chan data.BlockScrs ),
61
- closeChan : make (chan struct {}),
45
+ mutDispatchers : sync.RWMutex {},
46
+ filter : args .Filter ,
47
+ subscriptionMapper : args .SubscriptionMapper ,
48
+ dispatchers : make (map [uuid.UUID ]dispatcher.EventDispatcher ),
49
+ register : make (chan dispatcher.EventDispatcher ),
50
+ unregister : make (chan dispatcher.EventDispatcher ),
51
+ closeChan : make (chan struct {}),
62
52
}, nil
63
53
}
64
54
@@ -73,42 +63,26 @@ func checkArgs(args ArgsCommonHub) error {
73
63
return nil
74
64
}
75
65
76
- // Run is launched as a goroutine and listens for events on the exposed channels
77
- // TODO: use protection from triggering multiple times
78
- func (ch * commonHub ) Run () error {
66
+ // RegisterListener creates a goroutine and listens for WS register events
67
+ func (ch * commonHub ) RegisterListener () error {
68
+ ch .mutState .Lock ()
69
+ defer ch .mutState .Unlock ()
70
+
71
+ if ch .cancelFunc != nil {
72
+ return common .ErrLoopAlreadyStarted
73
+ }
74
+
79
75
var ctx context.Context
80
76
ctx , ch .cancelFunc = context .WithCancel (context .Background ())
81
77
82
- go ch .run (ctx )
78
+ go ch .registerListener (ctx )
83
79
84
80
return nil
85
81
}
86
82
87
- func (ch * commonHub ) run (ctx context.Context ) {
83
+ func (ch * commonHub ) registerListener (ctx context.Context ) {
88
84
for {
89
85
select {
90
- case <- ctx .Done ():
91
- log .Debug ("commonHub is stopping..." )
92
- return
93
-
94
- case events := <- ch .broadcast :
95
- ch .handleBroadcast (events )
96
-
97
- case revertEvent := <- ch .broadcastRevert :
98
- ch .handleRevertBroadcast (revertEvent )
99
-
100
- case finalizedEvent := <- ch .broadcastFinalized :
101
- ch .handleFinalizedBroadcast (finalizedEvent )
102
-
103
- case txsEvent := <- ch .broadcastTxs :
104
- ch .handleTxsBroadcast (txsEvent )
105
-
106
- case txsEvent := <- ch .broadcastBlockEventsWithOrder :
107
- ch .handleBlockEventsWithOrderBroadcast (txsEvent )
108
-
109
- case scrsEvent := <- ch .broadcastScrs :
110
- ch .handleScrsBroadcast (scrsEvent )
111
-
112
86
case dispatcherClient := <- ch .register :
113
87
ch .registerDispatcher (dispatcherClient )
114
88
@@ -123,59 +97,6 @@ func (ch *commonHub) Subscribe(event data.SubscribeEvent) {
123
97
ch .subscriptionMapper .MatchSubscribeEvent (event )
124
98
}
125
99
126
- // Broadcast handles block events pushed by producers into the broadcast channel
127
- // Upon reading the channel, the hub notifies the registered dispatchers, if any
128
- func (ch * commonHub ) Broadcast (events data.BlockEvents ) {
129
- select {
130
- case ch .broadcast <- events :
131
- case <- ch .closeChan :
132
- }
133
- }
134
-
135
- // BroadcastRevert handles revert event pushed by producers into the broadcast channel
136
- // Upon reading the channel, the hub notifies the registered dispatchers, if any
137
- func (ch * commonHub ) BroadcastRevert (event data.RevertBlock ) {
138
- select {
139
- case ch .broadcastRevert <- event :
140
- case <- ch .closeChan :
141
- }
142
- }
143
-
144
- // BroadcastFinalized handles finalized event pushed by producers into the broadcast channel
145
- // Upon reading the channel, the hub notifies the registered dispatchers, if any
146
- func (ch * commonHub ) BroadcastFinalized (event data.FinalizedBlock ) {
147
- select {
148
- case ch .broadcastFinalized <- event :
149
- case <- ch .closeChan :
150
- }
151
- }
152
-
153
- // BroadcastTxs handles block txs event pushed by producers into the broadcast channel
154
- // Upon reading the channel, the hub notifies the registered dispatchers, if any
155
- func (ch * commonHub ) BroadcastTxs (event data.BlockTxs ) {
156
- select {
157
- case ch .broadcastTxs <- event :
158
- case <- ch .closeChan :
159
- }
160
- }
161
-
162
- // BroadcastScrs handles block scrs event pushed by producers into the broadcast channel
163
- // Upon reading the channel, the hub notifies the registered dispatchers, if any
164
- func (ch * commonHub ) BroadcastScrs (event data.BlockScrs ) {
165
- select {
166
- case ch .broadcastScrs <- event :
167
- case <- ch .closeChan :
168
- }
169
- }
170
-
171
- // BroadcastBlockEventsWithOrder handles full block events pushed by producers into the channel
172
- func (ch * commonHub ) BroadcastBlockEventsWithOrder (event data.BlockEventsWithOrder ) {
173
- select {
174
- case ch .broadcastBlockEventsWithOrder <- event :
175
- case <- ch .closeChan :
176
- }
177
- }
178
-
179
100
// RegisterEvent will send event to a receive-only channel used to register dispatchers
180
101
func (ch * commonHub ) RegisterEvent (event dispatcher.EventDispatcher ) {
181
102
select {
@@ -192,7 +113,8 @@ func (ch *commonHub) UnregisterEvent(event dispatcher.EventDispatcher) {
192
113
}
193
114
}
194
115
195
- func (ch * commonHub ) handleBroadcast (blockEvents data.BlockEvents ) {
116
+ // Publish will publish logs and events to dispatcher
117
+ func (ch * commonHub ) Publish (blockEvents data.BlockEvents ) {
196
118
subscriptions := ch .subscriptionMapper .Subscriptions ()
197
119
198
120
for _ , subscription := range subscriptions {
@@ -220,7 +142,8 @@ func (ch *commonHub) handlePushBlockEvents(blockEvents data.BlockEvents, subscri
220
142
ch .mutDispatchers .RUnlock ()
221
143
}
222
144
223
- func (ch * commonHub ) handleRevertBroadcast (revertBlock data.RevertBlock ) {
145
+ // PublishRevert will publish revert event to dispatcher
146
+ func (ch * commonHub ) PublishRevert (revertBlock data.RevertBlock ) {
224
147
subscriptions := ch .subscriptionMapper .Subscriptions ()
225
148
226
149
dispatchersMap := make (map [uuid.UUID ]data.RevertBlock )
@@ -242,7 +165,8 @@ func (ch *commonHub) handleRevertBroadcast(revertBlock data.RevertBlock) {
242
165
}
243
166
}
244
167
245
- func (ch * commonHub ) handleFinalizedBroadcast (finalizedBlock data.FinalizedBlock ) {
168
+ // PublishFinalized will publish finalized event to dispatcher
169
+ func (ch * commonHub ) PublishFinalized (finalizedBlock data.FinalizedBlock ) {
246
170
subscriptions := ch .subscriptionMapper .Subscriptions ()
247
171
248
172
dispatchersMap := make (map [uuid.UUID ]data.FinalizedBlock )
@@ -264,7 +188,8 @@ func (ch *commonHub) handleFinalizedBroadcast(finalizedBlock data.FinalizedBlock
264
188
}
265
189
}
266
190
267
- func (ch * commonHub ) handleTxsBroadcast (blockTxs data.BlockTxs ) {
191
+ // PublishTxs will publish txs event to dispatcher
192
+ func (ch * commonHub ) PublishTxs (blockTxs data.BlockTxs ) {
268
193
subscriptions := ch .subscriptionMapper .Subscriptions ()
269
194
270
195
dispatchersMap := make (map [uuid.UUID ]data.BlockTxs )
@@ -286,7 +211,8 @@ func (ch *commonHub) handleTxsBroadcast(blockTxs data.BlockTxs) {
286
211
}
287
212
}
288
213
289
- func (ch * commonHub ) handleBlockEventsWithOrderBroadcast (blockTxs data.BlockEventsWithOrder ) {
214
+ // PublishBlockEventsWithOrder will publish block events with order to dispatcher
215
+ func (ch * commonHub ) PublishBlockEventsWithOrder (blockTxs data.BlockEventsWithOrder ) {
290
216
subscriptions := ch .subscriptionMapper .Subscriptions ()
291
217
292
218
dispatchersMap := make (map [uuid.UUID ]data.BlockEventsWithOrder )
@@ -308,7 +234,7 @@ func (ch *commonHub) handleBlockEventsWithOrderBroadcast(blockTxs data.BlockEven
308
234
}
309
235
}
310
236
311
- func (ch * commonHub ) handleScrsBroadcast (blockScrs data.BlockScrs ) {
237
+ func (ch * commonHub ) PublishScrs (blockScrs data.BlockScrs ) {
312
238
subscriptions := ch .subscriptionMapper .Subscriptions ()
313
239
314
240
dispatchersMap := make (map [uuid.UUID ]data.BlockScrs )
0 commit comments