-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmetafeed-finder.js
332 lines (303 loc) · 9.03 KB
/
metafeed-finder.js
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// SPDX-FileCopyrightText: 2021 Andre 'Staltz' Medeiros
//
// SPDX-License-Identifier: LGPL-3.0-only
const pull = require('pull-stream')
const debug = require('debug')('ssb:replication-scheduler')
const pushable = require('pull-pushable')
const detectSsbNetworkErrorSeverity = require('ssb-network-errors')
const { where, type, live, toPullStream } = require('ssb-db2/operators')
const { validateMetafeedAnnounce } = require('ssb-meta-feeds/validate')
class ReadyGate {
constructor() {
this.waiting = new Set()
this.ready = false
}
onReady(cb) {
if (this.ready) cb()
else this.waiting.add(cb)
}
setReady() {
this.ready = true
for (const cb of this.waiting) cb()
this.waiting.clear()
}
}
module.exports = class MetafeedFinder {
constructor(ssb, opts, batchLimit = 8, period = 500) {
this._ssb = ssb
this._myDebugId = ssb.id.substring(0, 5)
this._opts = opts
this._period = period
this._batchLimit = batchLimit
this._map = new Map() // mainFeedId => rootMetaFeedId
this._inverseMap = new Map() // rootMetaFeedId => mainFeedId
this._requestsByMainfeedId = new Map() // mainFeedId => Array<Calback>
this._retryables = new Set() // mainFeedIds
this._latestRequestTime = 0
this._timer = null
this._liveStream = pushable()
this._onLoaded = new ReadyGate()
// If at least one hops template is configured, then load
if (
this._opts.partialReplication &&
Object.values(this._opts.partialReplication).some((templ) => !!templ)
) {
if (!this._ssb.db || !this._ssb.db.query) {
throw new Error(
'ssb-replication-scheduler expects ssb-db2 to be installed, to use partial replication'
)
}
this._loadAllFromLog()
this._monitorConnectedPeers()
}
}
fetch(mainFeedId, cb) {
this._onLoaded.onReady(() => {
if (this._map.has(mainFeedId)) {
const metaFeedId = this._map.get(mainFeedId)
cb(null, metaFeedId)
} else if (mainFeedId === this._ssb.id) {
this._ssb.metafeeds.findOrCreate((err, rootMF) => {
if (err) cb(err)
else if (!rootMF) cb(null, null)
else {
const metaFeedId = rootMF.id
this._map.set(mainFeedId, metaFeedId)
this._inverseMap.set(metaFeedId, mainFeedId)
cb(null, metaFeedId)
}
})
} else {
this._request(mainFeedId, cb)
}
})
}
get(mainFeedId) {
return this._map.get(mainFeedId)
}
getInverse(metaFeedId) {
return this._inverseMap.get(metaFeedId)
}
liveStream() {
return this._liveStream
}
_loadAllFromLog() {
pull(
this._ssb.db.query(where(type('metafeed/announce')), toPullStream()),
pull.filter(this._validateMetafeedAnnounce),
pull.drain(
(msg) => {
this._updateMapsFromMsgValue(msg.value)
},
() => {
debug(
'%s loaded Map of all known main=>rootMF from disk, total %d',
this._myDebugId,
this._map.size
)
this._onLoaded.setReady()
this._startLiveStream()
}
)
)
}
_startLiveStream() {
pull(
this._ssb.db.query(
where(type('metafeed/announce')),
live(),
toPullStream()
),
pull.filter(this._validateMetafeedAnnounce),
pull.drain((msg) => {
this._updateMapsFromMsgValue(msg.value)
this._liveStream.push(this._pluckFromAnnounceMsg(msg.value))
})
)
}
_monitorConnectedPeers() {
if (!this._ssb.conn) {
console.warn(
'No ssb-conn installed, ssb-replication-scheduler will ' +
' miss some useful data from connected peers regarding metafeeds'
)
return
}
pull(
this._ssb.conn.hub().listen(),
pull.filter((ev) => ev.type === 'connected'),
pull.drain((ev) => {
if (this._retryables.size > 0) {
this._retryWithPeer(ev.details.rpc)
}
})
)
}
_validateMetafeedAnnounce(msg) {
const err = validateMetafeedAnnounce(msg)
if (err) {
console.warn(err)
return false
} else {
return true
}
}
_pluckFromAnnounceMsg(msgVal) {
const mainFeedId = msgVal.author
const metaFeedId = msgVal.content.metafeed
return [mainFeedId, metaFeedId]
}
_updateMapsFromMsgValue(msgVal) {
const [mainFeedId, metaFeedId] = this._pluckFromAnnounceMsg(msgVal)
this._map.set(mainFeedId, metaFeedId)
this._inverseMap.set(metaFeedId, mainFeedId)
this._retryables.delete(mainFeedId)
}
_request(mainFeedId, cb) {
const callbacks = this._requestsByMainfeedId.get(mainFeedId) || []
callbacks.push(cb)
this._requestsByMainfeedId.set(mainFeedId, callbacks)
this._latestRequestTime = Date.now()
if (this._requestsByMainfeedId.size >= this._batchLimit) {
this._flush()
} else {
this._scheduleDebouncedFlush()
}
}
_persist(msgVal) {
this._ssb.db.addOOO(msgVal, (err) => {
if (err) {
debug(
'%s failed to addOOO for a metafeed/announce: %s',
this._myDebugId,
err.message || err
)
}
})
}
async _forEachNeighborPeer(run) {
for (const peerId of Object.keys(this._ssb.peers)) {
if (peerId === this._ssb.id) continue
for (const rpc of this._ssb.peers[peerId]) {
const goToNext = await new Promise((resolve) => run(rpc, resolve))
if (!goToNext) return
}
}
}
_scheduleDebouncedFlush() {
if (this._period === 0) {
this._flush()
return
}
if (this._timer) return // Timer is already enabled
this._timer = setInterval(() => {
// Turn off the timer if there is nothing to flush
if (this._requestsByMainfeedId.size === 0) {
clearInterval(this._timer)
this._timer = null
}
// Flush if enough time has passed
else if (Date.now() - this._latestRequestTime > this._period)
this._flush()
}, this._period * 0.5)
if (this._timer.unref) this._timer.unref()
}
_makeQL1(mapOrSet) {
const query = {
op: 'or',
args: [],
}
for (const mainFeedId of mapOrSet.keys()) {
query.args.push({
op: 'and',
args: [
{ op: 'type', string: 'metafeed/announce' },
{ op: 'author', feed: mainFeedId },
],
})
}
return query
}
async _flush() {
if (this._requestsByMainfeedId.size === 0) return
const requests = new Map(this._requestsByMainfeedId)
this._requestsByMainfeedId.clear()
await this._forEachNeighborPeer((rpc, goToNextNeighbor) => {
debug(
'%s "getSubset" for peer %s for metafeed/announce messages',
this._myDebugId,
rpc.id
)
pull(
rpc.getSubset(this._makeQL1(requests), { querylang: 'ssb-ql-1' }),
pull.filter((value) => this._validateMetafeedAnnounce({ value })),
pull.drain(
(msgVal) => {
this._updateMapsFromMsgValue(msgVal)
const [mainFeedId, metaFeedId] = this._pluckFromAnnounceMsg(msgVal)
this._liveStream.push([mainFeedId, metaFeedId])
this._persist(msgVal)
if (requests.has(mainFeedId)) {
const callbacks = requests.get(mainFeedId)
requests.delete(mainFeedId)
for (const cb of callbacks) cb(null, metaFeedId)
}
if (requests.size === 0) {
goToNextNeighbor(false)
return false // abort this drain
}
},
(err) => {
if (err && detectSsbNetworkErrorSeverity(err) >= 2) {
debug(
'%s failed "getSubset" muxrpc with peer %s because: %s',
this._myDebugId,
rpc.id,
err.message || err
)
}
goToNextNeighbor(true)
}
)
)
})
if (requests.size > 0) {
// We couldn't find metaFeedIds for some mainFeedIds, so we assume there
// none. Note, this may give false negatives depending on who you're
// connected to!
for (const [mainFeedId, callbacks] of requests.entries()) {
this._retryables.add(mainFeedId)
for (const cb of callbacks) cb(null, null)
}
requests.clear()
}
}
_retryWithPeer(rpc) {
debug(
'%s "getSubset" retry on peer %s for metafeed/announce messages',
this._myDebugId,
rpc.id
)
pull(
rpc.getSubset(this._makeQL1(this._retryables), { querylang: 'ssb-ql-1' }),
pull.filter((value) => this._validateMetafeedAnnounce({ value })),
pull.drain(
(msgVal) => {
this._updateMapsFromMsgValue(msgVal)
this._liveStream.push(this._pluckFromAnnounceMsg(msgVal))
this._persist(msgVal)
},
(err) => {
if (err && detectSsbNetworkErrorSeverity(err) >= 2) {
debug(
'%s failed "getSubset" muxrpc retry with peer %s because: %s',
this._myDebugId,
rpc.id,
err.message || err
)
}
}
)
)
}
}