This repository has been archived by the owner on Jul 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
event.go
193 lines (167 loc) · 4.48 KB
/
event.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
package event
import (
"sync"
"github.com/medibloc/go-medibloc/common"
"github.com/medibloc/go-medibloc/util/logging"
)
// TODO @ggomma check whether event emitter exists
const (
// TopicLibBlock latest irreversible block.
TopicLibBlock = "chain.latestIrreversibleBlock"
// TopicNewTailBlock new tail block set
TopicNewTailBlock = "chain.newTailBlock"
// TopicPendingTransaction Pending transactions in a pending pool.
TopicPendingTransaction = "chain.pendingTransaction"
// TopicFutureTransaction future transactions in a future pool
TopicFutureTransaction = "chain.futureTransaction"
// TopicRevertBlock revert block
TopicRevertBlock = "chain.revertBlock"
// TopicTransactionExecutionResult transaction execution result
TopicTransactionExecutionResult = "chain.transactionResult"
// TopicBlockAccepted new block accepted on the chain
TopicAcceptedBlock = "chain.acceptedBlock"
// TopicInvalidBLock block is invalid
TopicInvalidBlock = "chain.invalidBlock"
)
// Type for account transaction result
const (
TypeAccountTransactionExecution = "executedTransaction"
TypeAccountTransactionPending = "pendingTransaction"
TypeAccountTransactionFuture = "futureTransaction"
TypeAccountTransactionDeleted = "deletedTransaction"
)
// Event structure
type Event struct {
Topic string
Data string
Type string
}
// Subscriber structure
type Subscriber struct {
eventCh chan *Event
topics []string
}
func topicList() map[string]bool {
var topicList = make(map[string]bool)
topicList[TopicLibBlock] = true
topicList[TopicNewTailBlock] = true
topicList[TopicPendingTransaction] = true
topicList[TopicFutureTransaction] = true
topicList[TopicRevertBlock] = true
topicList[TopicTransactionExecutionResult] = true
topicList[TopicAcceptedBlock] = true
topicList[TopicInvalidBlock] = true
return topicList
}
// NewSubscriber creates new event subscriber
func NewSubscriber(size int, topics []string) (*Subscriber, error) {
topicList := topicList()
for _, topic := range topics {
if common.IsHexAddress(topic) {
continue
}
if topicList[topic] != true {
return nil, ErrWrongEventTopic
}
}
eventCh := make(chan *Event, size)
return &Subscriber{
eventCh: eventCh,
topics: topics,
}, nil
}
// EventChan returns event channel
func (s *Subscriber) EventChan() chan *Event {
return s.eventCh
}
// Emitter structure
type Emitter struct {
eventSubscribers *sync.Map
eventCh chan *Event
quitCh chan bool
size int
}
// NewEventEmitter creates new event emitter
func NewEventEmitter(size int) *Emitter {
return &Emitter{
eventSubscribers: new(sync.Map),
eventCh: make(chan *Event, size),
quitCh: make(chan bool),
size: size,
}
}
/*
NewEventEmitter
- eventSubscribers
- topic #1
- subscriber #1 : true
- subscriber #2 : true
- topic #2
- subscriber #2 : true
*/
// Start event loop
func (e *Emitter) Start() {
logging.Console().Info("Starting Event Emitter")
go e.loop()
}
// Stop event loop
func (e *Emitter) Stop() {
logging.Console().Info("Stopping Event Emitter")
e.quitCh <- true
}
// Trigger event
func (e *Emitter) Trigger(event *Event) {
e.eventCh <- event
}
// Register event channel
func (e *Emitter) Register(subscribers ...*Subscriber) {
for _, subscriber := range subscribers {
for _, topic := range subscriber.topics {
m, _ := e.eventSubscribers.LoadOrStore(topic, new(sync.Map))
m.(*sync.Map).Store(subscriber, true)
}
}
}
// Deregister event channel
func (e *Emitter) Deregister(subscribers ...*Subscriber) {
for _, subscriber := range subscribers {
for _, topic := range subscriber.topics {
m, _ := e.eventSubscribers.Load(topic)
// If the topic doesn't hold any subscriber
if m == nil {
continue
}
m.(*sync.Map).Delete(subscriber)
}
}
}
func (e *Emitter) loop() {
logging.Console().Info("Started Event Emitter")
// timerChan := time.NewTicker(time.Second).C
// defer timerChan.Stop()
for {
select {
// case <- timerChan:
// TODO : Metrics
case <-e.quitCh:
logging.Console().Info("Stopped Event Emitter")
return
case event := <-e.eventCh:
topic := event.Topic
subscribers, ok := e.eventSubscribers.Load(topic)
// If topic subscriber doesn't exist, continue
if !ok {
continue
}
subs, _ := subscribers.(*sync.Map)
subs.Range(func(key, value interface{}) bool {
select {
case key.(*Subscriber).eventCh <- event:
default:
logging.Console().Warn("timeout to dispatch event")
}
return true
})
}
}
}