-
Notifications
You must be signed in to change notification settings - Fork 20
/
engine_subscribers.go
271 lines (228 loc) · 7.9 KB
/
engine_subscribers.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
// Copyright (c) 2022 Gobalsky Labs Limited
//
// Use of this software is governed by the Business Source License included
// in the LICENSE.VEGA file and at https://www.mariadb.com/bsl11.
//
// Change Date: 18 months from the later of the date of the first publicly
// available Distribution of this version of the repository, and 25 June 2022.
//
// On the date above, in accordance with the Business Source License, use
// of this software will be governed by version 3 or later of the GNU General
// Public License.
package oracles
import (
"context"
"fmt"
"sync"
"time"
"code.vegaprotocol.io/vega/core/types"
)
// OnMatchedOracleData describes the callback function used when an oracle data matches the spec.
type OnMatchedOracleData func(ctx context.Context, data OracleData) error
// OracleSpecPredicate describes the predicate used to filter the subscribers.
// When returning true, all the subscribers associated to the matching
// OracleSpec are collected.
// The order between specs and subscribers is preserved.
type OracleSpecPredicate func(spec OracleSpec) (bool, error)
// OracleSubscriptionPredicate describes the predicate used to check if any
// of the currently existing subscriptions expects the public keys inside
// the incoming OracleSpec object.
type OracleSubscriptionPredicate func(spec OracleSpec) bool
// SubscriptionID is a unique identifier referencing the subscription of an
// OnMatchedOracleData to an OracleSpec.
type SubscriptionID uint64
// Unsubscriber is a closure that is created at subscription step in order to
// provide the ability to unsubscribe at any conveninent moment.
type Unsubscriber func(context.Context, SubscriptionID)
// updatedSubscription wraps all useful information about an updated
// subscription.
type updatedSubscription struct {
subscriptionID SubscriptionID
spec types.OracleSpec
specActivatedAt time.Time
}
// filterResult describes the result of the filter operation.
type filterResult struct {
// oracleSpecIDs lists all the OracleSpec ID that matched the filter
// predicate.
oracleSpecIDs []OracleSpecID
// subscribers list all the subscribers associated to the matched
// OracleSpec.
subscribers []OnMatchedOracleData
}
// hasMatched returns true if filter has matched the predicated.
func (r filterResult) hasMatched() bool {
return len(r.oracleSpecIDs) > 0
}
// specSubscriptions wraps the subscribers (in form of OnMatchedOracleData) to
// the OracleSpec.
type specSubscriptions struct {
mu sync.RWMutex
lastSubscriptionID SubscriptionID
subscriptions []*specSubscription
// subscriptionsMatrix maps a SubscriptionID to an OracleSpecID to speed up
// the retrieval of the OnMatchedOracleData into the subscriptions.
subscriptionsMatrix map[SubscriptionID]OracleSpecID
}
// newSpecSubscriptions initialises the subscription handler.
func newSpecSubscriptions() *specSubscriptions {
return &specSubscriptions{
subscriptions: []*specSubscription{},
subscriptionsMatrix: map[SubscriptionID]OracleSpecID{},
}
}
// hasAnySubscribers checks if any of the subscriptions contains public keys that
// match the given ones by the predicate.
// Returns fast on the first match.
func (s *specSubscriptions) hasAnySubscribers(predicate OracleSubscriptionPredicate) bool {
s.mu.RLock()
defer s.mu.RUnlock()
for _, subscription := range s.subscriptions {
if predicate(subscription.spec) {
return true
}
}
return false
}
// filterSubscribers collects the subscribers that match the predicate on the
// OracleSpec.
// The order between specs and subscribers is preserved.
func (s *specSubscriptions) filterSubscribers(predicate OracleSpecPredicate) (*filterResult, error) {
s.mu.RLock()
defer s.mu.RUnlock()
result := &filterResult{
oracleSpecIDs: []OracleSpecID{},
subscribers: []OnMatchedOracleData{},
}
for _, subscription := range s.subscriptions {
matched, err := predicate(subscription.spec)
if err != nil {
return nil, err
}
if !matched {
continue
}
result.oracleSpecIDs = append(result.oracleSpecIDs, subscription.spec.id)
for _, subscriber := range subscription.subscribers {
result.subscribers = append(result.subscribers, subscriber.cb)
}
}
return result, nil
}
func (s *specSubscriptions) addSubscriber(spec OracleSpec, cb OnMatchedOracleData, tm time.Time) updatedSubscription {
s.mu.Lock()
defer s.mu.Unlock()
_, subscription := s.getSubscription(spec.id)
if subscription == nil {
subscription = s.createSubscription(spec, tm)
}
subscriptionID := s.nextSubscriptionID()
subscription.addSubscriber(subscriptionID, cb)
s.subscriptionsMatrix[subscriptionID] = spec.id
return updatedSubscription{
subscriptionID: subscriptionID,
specActivatedAt: subscription.specActivatedAt,
spec: *spec.OriginalSpec,
}
}
func (s *specSubscriptions) removeSubscriber(subscriptionID SubscriptionID) (updatedSubscription, bool) {
s.mu.Lock()
defer s.mu.Unlock()
specID, ok := s.subscriptionsMatrix[subscriptionID]
if !ok {
panic(fmt.Sprintf("unknown subscriber ID %d", subscriptionID))
}
index, subscription := s.getSubscription(specID)
subscription.removeSubscriber(subscriptionID)
delete(s.subscriptionsMatrix, subscriptionID)
hasNoMoreSubscriber := subscription.hasNoMoreSubscriber()
if hasNoMoreSubscriber {
s.removeSubscriptionFromIndex(index)
}
return updatedSubscription{
subscriptionID: subscriptionID,
specActivatedAt: subscription.specActivatedAt,
spec: *subscription.spec.OriginalSpec,
}, hasNoMoreSubscriber
}
// Internal usage.
func (s *specSubscriptions) removeSubscriptionFromIndex(index int) {
copy(s.subscriptions[index:], s.subscriptions[index+1:])
lastIndex := len(s.subscriptions) - 1
s.subscriptions[lastIndex] = nil
s.subscriptions = s.subscriptions[:lastIndex]
}
// Internal usage.
func (s *specSubscriptions) createSubscription(spec OracleSpec, tm time.Time) *specSubscription {
subscription := newOracleSpecSubscription(spec, tm)
s.subscriptions = append(s.subscriptions, subscription)
return subscription
}
// Internal usage.
func (s *specSubscriptions) getSubscription(id OracleSpecID) (int, *specSubscription) {
for i, subscription := range s.subscriptions {
if subscription.spec.id == id {
return i, subscription
}
}
return -1, nil
}
// nextSubscriptionID computes the next SubscriptionID
// Internal usage.
func (s *specSubscriptions) nextSubscriptionID() SubscriptionID {
s.lastSubscriptionID++
return s.lastSubscriptionID
}
// specSubscription groups all OnMatchedOracleData callbacks by
// OracleSpec.
type specSubscription struct {
spec OracleSpec
specActivatedAt time.Time
subscribers []*specSubscriber
}
type specSubscriber struct {
id SubscriptionID
cb OnMatchedOracleData
}
// Internal usage.
func newOracleSpecSubscription(spec OracleSpec, activationTime time.Time) *specSubscription {
return &specSubscription{
spec: spec,
specActivatedAt: activationTime,
subscribers: []*specSubscriber{},
}
}
// Internal usage.
func (s *specSubscription) addSubscriber(id SubscriptionID, cb OnMatchedOracleData) {
s.subscribers = append(s.subscribers, &specSubscriber{
id: id,
cb: cb,
})
}
// Internal usage.
func (s *specSubscription) removeSubscriber(id SubscriptionID) {
index, _ := s.getSubscriber(id)
s.removeSubscriberFromIndex(index)
}
// hasNoMoreSubscriber returns true if there is no subscriber for the associated
// OracleSpec, false otherwise.
// Internal usage.
func (s *specSubscription) hasNoMoreSubscriber() bool {
return len(s.subscribers) == 0
}
// Internal usage.
func (s *specSubscription) getSubscriber(id SubscriptionID) (int, *specSubscriber) {
for i, subscriber := range s.subscribers {
if subscriber.id == id {
return i, subscriber
}
}
return -1, nil
}
// Internal usage.
func (s *specSubscription) removeSubscriberFromIndex(index int) {
copy(s.subscribers[index:], s.subscribers[index+1:])
lastIndex := len(s.subscribers) - 1
s.subscribers[lastIndex] = nil
s.subscribers = s.subscribers[:lastIndex]
}