-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfeed_orchestrator.js
More file actions
294 lines (272 loc) · 10.2 KB
/
Copy pathfeed_orchestrator.js
File metadata and controls
294 lines (272 loc) · 10.2 KB
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
import { apds } from 'apds'
import { send } from './send.js'
import { queueSend } from './network_queue.js'
import { noteInterest } from './sync.js'
import { isBlockedAuthor } from './moderation.js'
import { attachCachedRows, getFeedRow, upsertFeedRow } from './feed_row_cache.js'
import { adaptiveConcurrency } from './adaptive_concurrency.js'
import { perfMeasure } from './perf.js'
const HOME_SEED_COUNT = 3
const HOME_BACKFILL_DEPTH = 6
const COMMUNITY_QUERY_CONCURRENCY = adaptiveConcurrency({ base: 4, min: 1, max: 8, type: 'network' })
const HOME_EXPAND_CONCURRENCY = adaptiveConcurrency({ base: 3, min: 1, max: 6 })
const FEED_ROWS_POLL_MS = 5000
const FEED_ROWS_ENABLED = false
const feedRowsEnabled = () => FEED_ROWS_ENABLED
const parseOpenedTimestamp = (opened) => {
if (!opened || opened.length < 13) { return 0 }
const ts = Number.parseInt(opened.substring(0, 13), 10)
return Number.isNaN(ts) ? 0 : ts
}
const isHash = (value) => typeof value === 'string' && value.length === 44
const getOpenedFromQuery = async (hash) => {
if (!hash) { return null }
const query = await apds.query(hash)
if (Array.isArray(query) && query[0] && query[0].opened) {
return query[0].opened
}
if (query && query.opened) {
return query.opened
}
return null
}
const mapLimit = async (items, limit, worker) => {
if (!Array.isArray(items) || !items.length) { return [] }
const results = new Array(items.length)
let cursor = 0
const lanes = Math.max(1, Math.min(limit, items.length))
const runLane = async () => {
while (true) {
const index = cursor
if (index >= items.length) { return }
cursor += 1
results[index] = await worker(items[index], index)
}
}
await Promise.all(Array.from({ length: lanes }, () => runLane()))
return results
}
const expandSeedChain = async (startHash, isActive) => {
if (!startHash || !isActive()) { return [] }
const discovered = []
const visited = new Set([startHash])
let cursor = startHash
let depth = 0
while (cursor && depth < HOME_BACKFILL_DEPTH && isActive()) {
const sig = await apds.get(cursor)
if (!sig) {
queueSend(cursor, { priority: 'low' })
break
}
const opened = await getOpenedFromQuery(cursor)
if (!opened || opened.length < 14) { break }
const contentHash = opened.substring(13)
const content = await apds.get(contentHash)
if (!content) {
queueSend(contentHash, { priority: 'low' })
break
}
const yaml = await apds.parseYaml(content)
const previous = yaml?.previous
if (!isHash(previous) || visited.has(previous)) { break }
queueSend(previous, { priority: 'low' })
const prevOpened = await getOpenedFromQuery(previous)
const ts = parseOpenedTimestamp(prevOpened)
discovered.push({ hash: previous, opened: prevOpened, ts, row: getFeedRow(previous) })
visited.add(previous)
cursor = previous
depth += 1
}
return discovered
}
const expandHomeLog = async (log, isActive) => {
if (!Array.isArray(log) || !log.length || !isActive()) { return log || [] }
const entries = [...log]
const seen = new Set(entries.map(entry => entry?.hash).filter(Boolean))
const seeds = entries.slice(0, HOME_SEED_COUNT)
const chains = await mapLimit(seeds, HOME_EXPAND_CONCURRENCY, async (seed) => {
return expandSeedChain(seed?.hash, isActive)
})
for (const chain of chains) {
if (!Array.isArray(chain) || !chain.length) { continue }
for (const entry of chain) {
if (!entry?.hash || seen.has(entry.hash)) { continue }
entries.push(entry)
seen.add(entry.hash)
}
}
return entries
}
export class FeedOrchestrator {
constructor({ src, signal = null, isActive = () => true, store }) {
this.src = src
this.signal = signal
this.isActive = isActive
this.store = store
this.pollTimer = null
}
stop() {
if (this.pollTimer) {
clearTimeout(this.pollTimer)
this.pollTimer = null
}
}
async fetchFeedRows({ kind = 'home', key = '', since = 0, limit = 40, timeoutMs = 1800 } = {}) {
if (!feedRowsEnabled()) { return { rows: [], nextSince: since } }
const timeoutController = new AbortController()
let externalAbort = null
if (this.signal) {
if (this.signal.aborted) { return { rows: [], nextSince: since } }
externalAbort = () => timeoutController.abort()
this.signal.addEventListener('abort', externalAbort, { once: true })
}
const timer = setTimeout(() => timeoutController.abort(), timeoutMs)
try {
let path = '/feed-rows/home'
if (kind === 'author') {
path = '/feed-rows/author/' + encodeURIComponent(key || '')
} else if (kind === 'alias') {
path = '/feed-rows/alias/' + encodeURIComponent(key || '')
}
const url = new URL(path, window.location.origin)
url.searchParams.set('since', String(since))
url.searchParams.set('limit', String(limit))
const res = await fetch(url.toString(), { cache: 'no-store', signal: timeoutController.signal })
if (!res.ok) { return { rows: [], nextSince: since } }
const data = await res.json().catch(() => null)
const rows = Array.isArray(data?.rows) ? data.rows : []
const nextSince = Number.isFinite(data?.nextSince) ? data.nextSince : since
return { rows, nextSince }
} catch {
return { rows: [], nextSince: since }
} finally {
clearTimeout(timer)
if (this.signal && externalAbort) {
this.signal.removeEventListener('abort', externalAbort)
}
}
}
async mergeRows(rows) {
if (!this.isActive()) { return 0 }
const withCache = attachCachedRows(rows || [])
withCache.forEach((row) => {
if (row?.hash) { upsertFeedRow(row) }
})
return this.store.upsertMany(withCache)
}
async startHome() {
let log = await perfMeasure('route.query', async () => apds.query(), 'home')
if (!this.isActive()) { return { log: [] } }
log = attachCachedRows(log || [])
if (!this.isActive()) { return { log } }
// Keep local feed as source of truth for initial paint. Remote feed rows are additive only.
if (!feedRowsEnabled()) {
void this.runHomeBackfill(log || [])
return { log }
}
const since = this.store.getSince() || 0
void (async () => {
const seedRowsResult = await perfMeasure(
'route.feedRows.home',
async () => this.fetchFeedRows({ kind: 'home', since, limit: 50 }),
'home'
)
if (!this.isActive()) { return }
await this.mergeRows(seedRowsResult.rows || [])
if (!this.isActive()) { return }
this.startHomePolling(seedRowsResult.nextSince || this.store.getSince() || since)
})()
void this.runHomeBackfill(log || [])
return { log }
}
startHomePolling(initialSince = 0) {
if (!feedRowsEnabled()) { return }
this.stop()
const state = { since: initialSince }
const tick = async () => {
if (!this.isActive()) { return }
const result = await this.fetchFeedRows({
kind: 'home',
since: state.since,
limit: 30,
timeoutMs: 2200
})
if (!this.isActive()) { return }
state.since = Number.isFinite(result.nextSince) ? Math.max(state.since, result.nextSince) : state.since
await this.mergeRows(result.rows || [])
if (!this.isActive()) { return }
this.pollTimer = setTimeout(() => { void tick() }, FEED_ROWS_POLL_MS)
}
this.pollTimer = setTimeout(() => { void tick() }, FEED_ROWS_POLL_MS)
}
async runHomeBackfill(log) {
if (!this.isActive()) { return }
const expanded = await expandHomeLog(log, this.isActive)
if (!this.isActive()) { return }
await this.store.upsertMany(expanded || [])
}
async startAuthor(pubkey) {
const log = await perfMeasure('route.query', async () => apds.query(pubkey), 'pubkey')
if (!this.isActive()) { return { log: [] } }
const withCachedRows = attachCachedRows(log || [])
if (!feedRowsEnabled()) {
return { log: withCachedRows || [] }
}
const since = this.store.getSince() || 0
void (async () => {
const seedRowsResult = await perfMeasure(
'route.feedRows.author',
async () => this.fetchFeedRows({ kind: 'author', key: pubkey, since, limit: 50 }),
'pubkey'
)
if (!this.isActive()) { return }
await this.mergeRows(seedRowsResult.rows || [])
})()
return { log: withCachedRows || [] }
}
async startAlias(alias) {
const ar = await perfMeasure(
'route.directory.fetch',
async () => fetch('https://pub.wiredove.net/' + alias).then(r => r.json()),
'community'
)
if (!this.isActive()) { return { query: [], primaryKey: null } }
if (ar) { localStorage.setItem(alias, JSON.stringify(ar)) }
const pubkeys = Array.isArray(ar) ? ar : []
const batched = await perfMeasure('route.community.queryBatch', async () => mapLimit(pubkeys, COMMUNITY_QUERY_CONCURRENCY, async (pubkey) => {
if (!this.isActive()) { return [] }
if (await isBlockedAuthor(pubkey)) { return [] }
await noteInterest(pubkey)
await send(pubkey)
const q = await apds.query(pubkey)
const rows = Array.isArray(q) ? q : (q ? [q] : [])
const withCachedRows = attachCachedRows(rows)
await this.store.upsertMany(withCachedRows)
return withCachedRows
}), 'community')
if (!this.isActive()) { return { query: [], primaryKey: null } }
const query = batched.flat()
if (!feedRowsEnabled()) {
const primaryKey = Array.isArray(ar) && ar.length ? ar[0] : null
return { query, primaryKey }
}
const since = this.store.getSince() || 0
void (async () => {
const seedRowsResult = await perfMeasure(
'route.feedRows.alias',
async () => this.fetchFeedRows({ kind: 'alias', key: alias, since, limit: 50 }),
'community'
)
if (!this.isActive()) { return }
await this.mergeRows(seedRowsResult.rows || [])
})()
const primaryKey = Array.isArray(ar) && ar.length ? ar[0] : null
return { query, primaryKey }
}
async startSearch(queryHash) {
const log = await perfMeasure('route.query', async () => apds.query(queryHash), 'search')
if (!this.isActive()) { return { log: [] } }
const withCachedRows = attachCachedRows(log || [])
return { log: withCachedRows || [] }
}
}