libp2r2p 0.12.9 → 0.12.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +57 -4
- package/idb-queue/index.js +128 -7
- package/package.json +1 -1
- package/private-channel/helpers/history.js +4 -1
- package/private-message/helpers/idempotency.js +57 -0
- package/private-message/index.js +16 -7
- package/private-messenger/file/README.md +2 -0
- package/private-messenger/file/index.js +19 -2
- package/private-messenger/helpers/fallback-race.js +7 -1
- package/private-messenger/helpers/send-routing.js +5 -1
- package/private-messenger/index.js +172 -13
- package/private-messenger/recovery/index.js +11 -2
- package/relay/helpers/retry-debug.js +6 -2
- package/relay/services/relay-connection.js +55 -7
- package/relay/services/relay-pool.js +98 -14
|
@@ -19,6 +19,7 @@ const COUNT_TIMEOUT_AFTER_FIRST_COUNT_MS = 500
|
|
|
19
19
|
const SEND_TIMEOUT_MS = 30000
|
|
20
20
|
const LIVE_OVERLAP_SECONDS = 600
|
|
21
21
|
const LIVE_PROGRESS_INTERVAL_MS = 60000
|
|
22
|
+
const DEFAULT_MAX_RATE_LIMIT_RETRY_WAIT_MS = 1500
|
|
22
23
|
|
|
23
24
|
// Returns a function that should be called for each received event (valid or invalid).
|
|
24
25
|
// Calls onSatisfied() and stops counting once the filter is fully satisfied per relay:
|
|
@@ -113,6 +114,36 @@ function validateRateLimitRetries (value) {
|
|
|
113
114
|
return value
|
|
114
115
|
}
|
|
115
116
|
|
|
117
|
+
function validateMaxRateLimitRetryWaitMs (value) {
|
|
118
|
+
if (value !== null && (!Number.isSafeInteger(value) || value < 0)) {
|
|
119
|
+
throw new ValidationError('INVALID_MAX_RATE_LIMIT_RETRY_WAIT_MS')
|
|
120
|
+
}
|
|
121
|
+
return value
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function effectiveRetryAt (value) {
|
|
125
|
+
const pending = [value]
|
|
126
|
+
const seen = new Set()
|
|
127
|
+
let retryAt = 0
|
|
128
|
+
while (pending.length) {
|
|
129
|
+
const current = pending.pop()
|
|
130
|
+
if (!current || typeof current !== 'object' || seen.has(current)) continue
|
|
131
|
+
seen.add(current)
|
|
132
|
+
for (const key of ['retryAt', 'effectiveRetryAt']) {
|
|
133
|
+
if (Number.isSafeInteger(current[key])) retryAt = Math.max(retryAt, current[key])
|
|
134
|
+
}
|
|
135
|
+
for (const key of ['cause', 'reason', 'error']) pending.push(current[key])
|
|
136
|
+
for (const key of ['errors', 'reports', 'relays']) {
|
|
137
|
+
if (Array.isArray(current[key])) pending.push(...current[key])
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return retryAt
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function retryWaitAllowed (retryAt, maxWaitMs, now = Date.now()) {
|
|
144
|
+
return maxWaitMs === null || Math.max(0, retryAt - now) <= maxWaitMs
|
|
145
|
+
}
|
|
146
|
+
|
|
116
147
|
function assertReadOptions (filter, timeouts) {
|
|
117
148
|
if (!filter || typeof filter !== 'object' || Array.isArray(filter)) throw new ValidationError('INVALID_FILTER')
|
|
118
149
|
for (const value of Object.values(timeouts)) {
|
|
@@ -167,10 +198,20 @@ export class RelayPool {
|
|
|
167
198
|
#readRetry
|
|
168
199
|
#options
|
|
169
200
|
|
|
170
|
-
constructor ({
|
|
201
|
+
constructor ({
|
|
202
|
+
_createRelay,
|
|
203
|
+
_isOnline,
|
|
204
|
+
_onOnline,
|
|
205
|
+
WebSocket: WebSocketImpl,
|
|
206
|
+
rateLimitCapMs = DEFAULT_RATE_LIMIT_CAP_MS,
|
|
207
|
+
rateLimitRetries = 1,
|
|
208
|
+
maxRateLimitRetryWaitMs = DEFAULT_MAX_RATE_LIMIT_RETRY_WAIT_MS,
|
|
209
|
+
...capacity
|
|
210
|
+
} = {}) {
|
|
171
211
|
this.#options = {
|
|
172
212
|
rateLimitCapMs: validateRateLimitCapMs(rateLimitCapMs),
|
|
173
|
-
rateLimitRetries: validateRateLimitRetries(rateLimitRetries)
|
|
213
|
+
rateLimitRetries: validateRateLimitRetries(rateLimitRetries),
|
|
214
|
+
maxRateLimitRetryWaitMs: validateMaxRateLimitRetryWaitMs(maxRateLimitRetryWaitMs)
|
|
174
215
|
}
|
|
175
216
|
this.#WebSocket = WebSocketImpl
|
|
176
217
|
this.#createRelay = _createRelay ?? (url => new RelayConnection(url, {
|
|
@@ -187,11 +228,14 @@ export class RelayPool {
|
|
|
187
228
|
|
|
188
229
|
setOptions (partial = {}) {
|
|
189
230
|
if (!partial || typeof partial !== 'object' || Array.isArray(partial)) throw new ValidationError('INVALID_RELAY_POOL_OPTIONS')
|
|
190
|
-
const unknown = Object.keys(partial).find(key => !['rateLimitCapMs', 'rateLimitRetries'].includes(key))
|
|
231
|
+
const unknown = Object.keys(partial).find(key => !['rateLimitCapMs', 'rateLimitRetries', 'maxRateLimitRetryWaitMs'].includes(key))
|
|
191
232
|
if (unknown) throw new ValidationError('UNKNOWN_RELAY_POOL_OPTION')
|
|
192
233
|
const next = { ...this.#options }
|
|
193
234
|
if (Object.hasOwn(partial, 'rateLimitCapMs')) next.rateLimitCapMs = validateRateLimitCapMs(partial.rateLimitCapMs)
|
|
194
235
|
if (Object.hasOwn(partial, 'rateLimitRetries')) next.rateLimitRetries = validateRateLimitRetries(partial.rateLimitRetries)
|
|
236
|
+
if (Object.hasOwn(partial, 'maxRateLimitRetryWaitMs')) {
|
|
237
|
+
next.maxRateLimitRetryWaitMs = validateMaxRateLimitRetryWaitMs(partial.maxRateLimitRetryWaitMs)
|
|
238
|
+
}
|
|
195
239
|
this.#options = next
|
|
196
240
|
for (const relay of this.#relays.values()) relay.setRateLimitCapMs?.(next.rateLimitCapMs)
|
|
197
241
|
return this.getOptions()
|
|
@@ -201,6 +245,12 @@ export class RelayPool {
|
|
|
201
245
|
return validateRateLimitRetries(value === undefined ? this.#options.rateLimitRetries : value)
|
|
202
246
|
}
|
|
203
247
|
|
|
248
|
+
#maxRetryWait (value) {
|
|
249
|
+
return validateMaxRateLimitRetryWaitMs(
|
|
250
|
+
value === undefined ? this.#options.maxRateLimitRetryWaitMs : value
|
|
251
|
+
)
|
|
252
|
+
}
|
|
253
|
+
|
|
204
254
|
// Injects the WebSocket implementation used by new pooled connections (e.g.
|
|
205
255
|
// a launcher-owned relay multiplexer). Existing connections keep the
|
|
206
256
|
// implementation they were created with.
|
|
@@ -293,24 +343,45 @@ export class RelayPool {
|
|
|
293
343
|
async #publishEvent (relay, event, getAuthEvent, signal, {
|
|
294
344
|
useBulkPacing,
|
|
295
345
|
rateLimitRetries,
|
|
346
|
+
maxRateLimitRetryWaitMs,
|
|
296
347
|
retrySignal,
|
|
297
348
|
deadlineAt
|
|
298
349
|
}) {
|
|
299
350
|
try {
|
|
300
|
-
await relay.publish(event, {
|
|
351
|
+
await relay.publish(event, {
|
|
352
|
+
signal,
|
|
353
|
+
useBulkPacing,
|
|
354
|
+
rateLimitRetries,
|
|
355
|
+
maxRateLimitRetryWaitMs,
|
|
356
|
+
retrySignal,
|
|
357
|
+
deadlineAt
|
|
358
|
+
})
|
|
301
359
|
return 'published'
|
|
302
360
|
} catch (error) {
|
|
303
361
|
const reason = error instanceof Error ? error : new Error(String(error))
|
|
304
362
|
if (!getAuthEvent || !requiresNip42Auth(reason)) throw reason
|
|
305
363
|
|
|
306
364
|
try {
|
|
307
|
-
await relay.authenticate(getAuthEvent, {
|
|
365
|
+
await relay.authenticate(getAuthEvent, {
|
|
366
|
+
signal,
|
|
367
|
+
rateLimitRetries,
|
|
368
|
+
maxRateLimitRetryWaitMs,
|
|
369
|
+
retrySignal,
|
|
370
|
+
deadlineAt
|
|
371
|
+
})
|
|
308
372
|
} catch (error) {
|
|
309
373
|
const authReason = error instanceof Error ? error : new Error(String(error))
|
|
310
374
|
if (retrySignal?.aborted && authReason.message?.startsWith('rate-limited:')) throw authReason
|
|
311
375
|
throw new Nip42AuthenticationError(authReason)
|
|
312
376
|
}
|
|
313
|
-
await relay.publish(event, {
|
|
377
|
+
await relay.publish(event, {
|
|
378
|
+
signal,
|
|
379
|
+
useBulkPacing,
|
|
380
|
+
rateLimitRetries,
|
|
381
|
+
maxRateLimitRetryWaitMs,
|
|
382
|
+
retrySignal,
|
|
383
|
+
deadlineAt
|
|
384
|
+
})
|
|
314
385
|
return 'published'
|
|
315
386
|
}
|
|
316
387
|
}
|
|
@@ -322,13 +393,15 @@ export class RelayPool {
|
|
|
322
393
|
timeout = COUNT_TIMEOUT_MS,
|
|
323
394
|
timeoutAfterFirstCount = COUNT_TIMEOUT_AFTER_FIRST_COUNT_MS,
|
|
324
395
|
signal,
|
|
325
|
-
rateLimitRetries
|
|
396
|
+
rateLimitRetries,
|
|
397
|
+
maxRateLimitRetryWaitMs
|
|
326
398
|
} = {}) {
|
|
327
399
|
if (!filter || typeof filter !== 'object' || Array.isArray(filter)) {
|
|
328
400
|
throw new ValidationError('COUNT_FILTER_REQUIRED')
|
|
329
401
|
}
|
|
330
402
|
if (signal?.aborted) throw new Error('Aborted')
|
|
331
403
|
const retries = this.#retryCount(rateLimitRetries)
|
|
404
|
+
const maxRetryWait = this.#maxRetryWait(maxRateLimitRetryWaitMs)
|
|
332
405
|
|
|
333
406
|
const urls = normalizedRelayUrls(relays)
|
|
334
407
|
if (!urls.length) {
|
|
@@ -425,9 +498,10 @@ export class RelayPool {
|
|
|
425
498
|
const handleError = (relay, error) => {
|
|
426
499
|
const reason = error instanceof Error ? error : new Error(String(error))
|
|
427
500
|
if (isResolved || !pending.has(relay)) return
|
|
428
|
-
const retryAt =
|
|
501
|
+
const retryAt = effectiveRetryAt(reason)
|
|
429
502
|
if (reason.message?.startsWith('rate-limited:') && retryAt &&
|
|
430
|
-
retryCounts.get(relay) < retries && (deadline === null || retryAt < deadline)
|
|
503
|
+
retryCounts.get(relay) < retries && (deadline === null || retryAt < deadline) &&
|
|
504
|
+
retryWaitAllowed(retryAt, maxRetryWait)) {
|
|
431
505
|
retryCounts.set(relay, retryCounts.get(relay) + 1)
|
|
432
506
|
retryErrors.set(relay, reason)
|
|
433
507
|
debugRateLimitRetry({
|
|
@@ -435,7 +509,9 @@ export class RelayPool {
|
|
|
435
509
|
operation: 'COUNT',
|
|
436
510
|
retryAttempt: retryCounts.get(relay),
|
|
437
511
|
maxRetries: retries,
|
|
438
|
-
retryAt
|
|
512
|
+
retryAt,
|
|
513
|
+
effectiveWaitMs: Math.max(0, retryAt - Date.now()),
|
|
514
|
+
maxWaitMs: maxRetryWait
|
|
439
515
|
})
|
|
440
516
|
const controller = new AbortController()
|
|
441
517
|
const combined = AbortSignal.any([countController.signal, controller.signal])
|
|
@@ -491,12 +567,14 @@ export class RelayPool {
|
|
|
491
567
|
deduplicateAcrossRelays = true,
|
|
492
568
|
queueTimeout = 30000,
|
|
493
569
|
rateLimitRetries,
|
|
570
|
+
maxRateLimitRetryWaitMs,
|
|
494
571
|
_admissions
|
|
495
572
|
} = {}) {
|
|
496
573
|
assertReadOptions(filter, { timeout, timeoutAfterFirstEose, queueTimeout })
|
|
497
574
|
if (typeof deduplicateAcrossRelays !== 'boolean') throw new ValidationError('INVALID_DEDUPLICATE_ACROSS_RELAYS')
|
|
498
575
|
if (signal?.aborted) throw new Error('Aborted')
|
|
499
576
|
const retries = this.#retryCount(rateLimitRetries)
|
|
577
|
+
const maxRetryWait = this.#maxRetryWait(maxRateLimitRetryWaitMs)
|
|
500
578
|
const urls = normalizedRelayUrls(relays)
|
|
501
579
|
const subscriptions = new Map()
|
|
502
580
|
const retryControllers = new Map()
|
|
@@ -580,15 +658,18 @@ export class RelayPool {
|
|
|
580
658
|
const scheduleRetry = (url, error) => {
|
|
581
659
|
if (isResolved || outcomes.has(url) || retryControllers.has(url) ||
|
|
582
660
|
retryCounts.get(url) >= retries || !error?.message?.startsWith('rate-limited:')) return false
|
|
583
|
-
const retryAt =
|
|
584
|
-
if (!retryAt || (deadlines.has(url) && retryAt >= deadlines.get(url))
|
|
661
|
+
const retryAt = effectiveRetryAt(error)
|
|
662
|
+
if (!retryAt || (deadlines.has(url) && retryAt >= deadlines.get(url)) ||
|
|
663
|
+
!retryWaitAllowed(retryAt, maxRetryWait)) return false
|
|
585
664
|
retryCounts.set(url, retryCounts.get(url) + 1)
|
|
586
665
|
debugRateLimitRetry({
|
|
587
666
|
relay: url,
|
|
588
667
|
operation: 'REQ',
|
|
589
668
|
retryAttempt: retryCounts.get(url),
|
|
590
669
|
maxRetries: retries,
|
|
591
|
-
retryAt
|
|
670
|
+
retryAt,
|
|
671
|
+
effectiveWaitMs: Math.max(0, retryAt - Date.now()),
|
|
672
|
+
maxWaitMs: maxRetryWait
|
|
592
673
|
})
|
|
593
674
|
const controller = new AbortController()
|
|
594
675
|
const combined = AbortSignal.any([admissionSignal, controller.signal])
|
|
@@ -1392,12 +1473,14 @@ export class RelayPool {
|
|
|
1392
1473
|
onRelayResult,
|
|
1393
1474
|
signal,
|
|
1394
1475
|
useBulkPacing = false,
|
|
1395
|
-
rateLimitRetries
|
|
1476
|
+
rateLimitRetries,
|
|
1477
|
+
maxRateLimitRetryWaitMs
|
|
1396
1478
|
} = {}) {
|
|
1397
1479
|
if (signal != null && (typeof signal.aborted !== 'boolean' || typeof signal.addEventListener !== 'function' || typeof signal.removeEventListener !== 'function')) throw new ValidationError('INVALID_RELAY_SIGNAL')
|
|
1398
1480
|
if (getRetryEvent != null && typeof getRetryEvent !== 'function') throw new ValidationError('INVALID_RETRY_EVENT_CALLBACK')
|
|
1399
1481
|
if (typeof useBulkPacing !== 'boolean') throw new ValidationError('INVALID_USE_BULK_PACING')
|
|
1400
1482
|
const retries = this.#retryCount(rateLimitRetries)
|
|
1483
|
+
const maxRetryWait = this.#maxRetryWait(maxRateLimitRetryWaitMs)
|
|
1401
1484
|
const urls = normalizedRelayUrls(relays)
|
|
1402
1485
|
if (!urls.length) {
|
|
1403
1486
|
const promise = Promise.resolve(publishSummary([], urls, {
|
|
@@ -1456,6 +1539,7 @@ export class RelayPool {
|
|
|
1456
1539
|
return await this.#publishEvent(relay, eventForAttempt, getAuthEvent, operationSignal, {
|
|
1457
1540
|
useBulkPacing,
|
|
1458
1541
|
rateLimitRetries: retries,
|
|
1542
|
+
maxRateLimitRetryWaitMs: maxRetryWait,
|
|
1459
1543
|
retrySignal: retryControllers[index].signal,
|
|
1460
1544
|
deadlineAt: deadline
|
|
1461
1545
|
})
|