-
Notifications
You must be signed in to change notification settings - Fork 0
/
overseer.go
226 lines (205 loc) · 6.04 KB
/
overseer.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
package main
import (
"crypto/rand"
"errors"
"fmt"
"math/big"
"overseer/eventbus"
"sync"
"github.com/avast/retry-go"
logging "github.com/sirupsen/logrus"
)
const SECP256k1GeneratorOrder = "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141"
type Overseer struct {
eventBus eventbus.Bus
middlewareMap sync.Map
Subsystems map[string]*BaseSubsystem
}
func NewOverseer(eventBus eventbus.Bus, baseSubsystems ...*BaseSubsystem) *Overseer {
overseer := &Overseer{
Subsystems: make(map[string]*BaseSubsystem),
}
overseer.SetEventBus(eventBus)
overseer.SetupMethodRouting()
for _, baseSubsystem := range baseSubsystems {
overseer.RegisterSubsystem(baseSubsystem)
}
return overseer
}
func (s *Overseer) AddMiddleware(middleware *func(string, interface{}) interface{}) {
s.middlewareMap.Store(middleware, middleware)
s.eventBus.AddMiddleware(middleware)
}
func (s *Overseer) RemoveMiddleware(m *func(string, interface{}) interface{}) {
middleware, ok := s.middlewareMap.Load(m)
if !ok {
return
}
s.eventBus.RemoveMiddleware(middleware.(*func(string, interface{}) interface{}))
s.middlewareMap.Delete(middleware)
}
func (s *Overseer) AddRequestMiddleware(middleware *func(methodRequest MethodRequest) MethodRequest) {
requestMiddleware := func(topic string, inter interface{}) interface{} {
if methodRequest, ok := inter.(MethodRequest); ok {
return (*middleware)(methodRequest)
}
return inter
}
s.middlewareMap.Store(middleware, &requestMiddleware)
s.eventBus.AddMiddleware(&requestMiddleware)
}
func (s *Overseer) AddResponseMiddleware(middleware *func(methodResponse MethodResponse) MethodResponse) {
responseMiddleware := func(topic string, inter interface{}) interface{} {
if methodResponse, ok := inter.(MethodResponse); ok {
return (*middleware)(methodResponse)
}
return inter
}
s.middlewareMap.Store(middleware, &responseMiddleware)
s.eventBus.AddMiddleware(&responseMiddleware)
}
func (s *Overseer) RemoveRequestMiddleware(middleware *func(methodRequest MethodRequest) MethodRequest) {
requestMiddleware, ok := s.middlewareMap.Load(middleware)
if !ok {
return
}
s.eventBus.RemoveMiddleware(requestMiddleware.(*func(string, interface{}) interface{}))
s.middlewareMap.Delete(middleware)
}
func (s *Overseer) RemoveResponseMiddleware(middleware *func(methodResponse MethodResponse) MethodResponse) {
responseMiddleware, ok := s.middlewareMap.Load(middleware)
if !ok {
return
}
s.eventBus.RemoveMiddleware(responseMiddleware.(*func(string, interface{}) interface{}))
s.middlewareMap.Delete(middleware)
}
func (s *Overseer) SetEventBus(e eventbus.Bus) {
s.eventBus = e
}
func (s *Overseer) RegisterSubsystem(bs *BaseSubsystem) {
s.Subsystems[bs.Name()] = bs
}
type MethodRequest struct {
Caller string
Subsystem string
Method string
ID string
Data []interface{}
}
func (s *Overseer) SetupMethodRouting() {
err := s.eventBus.SubscribeAsync("method", func(data interface{}) {
methodRequest, ok := data.(MethodRequest)
if !ok {
logging.Error("could not parse data for query")
return
}
var baseSubsystem *BaseSubsystem
err := retry.Do(func() error {
bs, ok := s.Subsystems[methodRequest.Subsystem]
if !ok {
logging.WithField("Subsystem", methodRequest.Subsystem).Error("could not find subsystem")
return fmt.Errorf("could not find subsystem %v", methodRequest.Subsystem)
}
if !bs.IsRunning() {
logging.WithFields(logging.Fields{
"Subsystem": methodRequest.Subsystem,
"data": data,
}).Error("Subsystem is not running")
return fmt.Errorf("subsystem %v is not running", methodRequest.Subsystem)
}
baseSubsystem = bs
return nil
})
if err != nil {
s.eventBus.Publish(methodRequest.ID, MethodResponse{
Error: err,
Data: nil,
})
} else {
go func() {
defer func() {
if err := recover(); err != nil {
logging.WithFields(logging.Fields{
"Caller": methodRequest.Caller,
"Method": methodRequest.Method,
"data": data,
"error": err,
}).Error("panicked during baseSubsystem.Call")
resp := MethodResponse{
Error: fmt.Errorf("%v", err),
Data: nil,
}
s.eventBus.Publish(methodRequest.ID, resp)
}
}()
data, err := baseSubsystem.Call(methodRequest.Method, methodRequest.Data...)
resp := MethodResponse{
Request: methodRequest,
Error: err,
Data: data,
}
s.eventBus.Publish(methodRequest.ID, resp)
}()
}
}, false)
if err != nil {
logging.WithError(err).Error("could not subscribe async")
}
}
type MethodResponse struct {
Request MethodRequest
Error error
Data interface{}
}
func AwaitTopic(eventBus eventbus.Bus, topic string) <-chan interface{} {
responseCh := make(chan interface{})
err := eventBus.SubscribeOnceAsync(topic, func(res interface{}) {
responseCh <- res
close(responseCh)
})
if err != nil {
logging.WithError(err).Error("could not subscribe async")
}
return responseCh
}
func SubsystemMethod(eventBus eventbus.Bus, caller string, subsystem string, method string, data ...interface{}) MethodResponse {
generatorOrder := new(big.Int)
_, ok := generatorOrder.SetString(SECP256k1GeneratorOrder, 0)
if !ok {
return MethodResponse{
Error: errors.New("could not parse SECP256k1GeneratorOrder"),
Data: nil,
}
}
nonce, err := rand.Int(rand.Reader, generatorOrder)
if err != nil {
return MethodResponse{
Error: errors.New("could not generate random nonce"),
Data: nil,
}
}
nonceStr := nonce.Text(16)
responseCh := AwaitTopic(eventBus, nonceStr)
eventBus.Publish("method", MethodRequest{
Caller: caller,
Subsystem: subsystem,
Method: method,
ID: nonceStr,
Data: data,
})
methodResponseInter := <-responseCh
methodResponse, ok := methodResponseInter.(MethodResponse)
if !ok {
return MethodResponse{
Error: errors.New("method response was not of MethodResponse type"),
Data: nil,
}
}
return methodResponse
}
func EmptyHandler(name string) func() {
return func() {
logging.WithField("name", name).Error("handler was not initialized")
}
}