libp2r2p 0.12.21 → 0.12.22

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "libp2r2p",
3
- "version": "0.12.21",
3
+ "version": "0.12.22",
4
4
  "description": "Peer-to-relay-to-peer",
5
5
  "keywords": [
6
6
  "p2r2p",
@@ -122,6 +122,8 @@ const STORAGE_POLICY_BROADCAST_CHANNEL = 'libp2r2p:private-messenger:storage-pol
122
122
  const DEFAULT_OFFLINE_SKEW_SECONDS = 30
123
123
  const DEFAULT_RELOAD_GAP_DELAY_MS = 500
124
124
  const DEFAULT_SEEDER_PRESENCE_INTERVAL_MS = 10 * 60 * 1000
125
+ const SEEDER_PRESENCE_COALESCE_MS = 250
126
+ const SEEDER_PRESENCE_MAX_PENDING = 4096
125
127
  const DEFAULT_SEEDER_ONLINE_SECONDS = 20 * 60
126
128
  const DEFAULT_MAX_DYNAMIC_RECOVERY_SEEDERS = 8
127
129
  const DEFAULT_MESSAGE_QUEUE_MAX_BYTES = 16 * 1024 * 1024 // 16 MiB
@@ -515,6 +517,8 @@ export class PrivateMessenger {
515
517
  this.watchRevisionByChannel = new Map()
516
518
  this.presenceTimers = new Map()
517
519
  this.presenceJobs = new Map()
520
+ this.pendingSeederPresence = new Map()
521
+ this.seederPresenceFlushTimer = null
518
522
  this.seedQueueOperationTail = Promise.resolve()
519
523
  this.stopRelayListWatcher = null
520
524
  this.relayListWatcherPubkey = ''
@@ -952,6 +956,10 @@ export class PrivateMessenger {
952
956
  this.channelDebugRefs.delete(pubkey)
953
957
  }
954
958
  }
959
+ this.discardSeederPresence([
960
+ ...removedPubkeys,
961
+ ...changed.map(channel => channel.pubkey)
962
+ ])
955
963
 
956
964
  const storageSnapshot = await activatePrivateMessengerStorage({
957
965
  userPubkey: this.userPubkey,
@@ -1274,6 +1282,78 @@ export class PrivateMessenger {
1274
1282
  return true
1275
1283
  }
1276
1284
 
1285
+ seederPresenceKey (channelPubkey, senderPubkey) {
1286
+ return JSON.stringify([channelPubkey, senderPubkey])
1287
+ }
1288
+
1289
+ async receiveSeederPresence (channelPubkey, message) {
1290
+ if (this.closePromise || this.sendingPaused() || message.provenance === 'hearsay' ||
1291
+ !this.desiredChannels.has(channelPubkey)) return
1292
+ if (!this.#admitInboundPeer(channelPubkey, 'yell', message)) return
1293
+ const senderPubkey = message.senderPubkey || message.event?.pubkey
1294
+ if (!senderPubkey) return
1295
+ const channel = this.channels.get(channelPubkey)
1296
+ if (!channel) return
1297
+ if (channel.seeders?.length && !channel.seeders.includes(senderPubkey)) return
1298
+ const at = messageTime(message)
1299
+ const key = this.seederPresenceKey(channelPubkey, senderPubkey)
1300
+ const current = this.pendingSeederPresence.get(key)
1301
+ if (current && current.at >= at) return
1302
+ if (current) this.pendingSeederPresence.delete(key)
1303
+ if (!channel.seeders?.length) {
1304
+ const maxDynamic = Number.isSafeInteger(this.maxDynamicRecoverySeeders) &&
1305
+ this.maxDynamicRecoverySeeders > 0
1306
+ ? this.maxDynamicRecoverySeeders
1307
+ : 0
1308
+ if (!maxDynamic) return
1309
+ const channelEntries = [...this.pendingSeederPresence.entries()]
1310
+ .filter(([, pending]) => pending.channelPubkey === channelPubkey)
1311
+ if (channelEntries.length >= maxDynamic) {
1312
+ channelEntries.sort((left, right) => left[1].at - right[1].at)
1313
+ for (const [pendingKey] of channelEntries.slice(
1314
+ 0,
1315
+ channelEntries.length - maxDynamic + 1
1316
+ )) {
1317
+ this.pendingSeederPresence.delete(pendingKey)
1318
+ }
1319
+ }
1320
+ }
1321
+ while (this.pendingSeederPresence.size >= SEEDER_PRESENCE_MAX_PENDING) {
1322
+ this.pendingSeederPresence.delete(this.pendingSeederPresence.keys().next().value)
1323
+ }
1324
+ this.pendingSeederPresence.set(key, { channelPubkey, senderPubkey, at })
1325
+ if (this.seederPresenceFlushTimer) return
1326
+ this.seederPresenceFlushTimer = this._setTimeout(() => {
1327
+ this.seederPresenceFlushTimer = null
1328
+ this.flushSeederPresence()
1329
+ }, SEEDER_PRESENCE_COALESCE_MS)
1330
+ this.seederPresenceFlushTimer?.unref?.()
1331
+ }
1332
+
1333
+ flushSeederPresence () {
1334
+ const pending = [...this.pendingSeederPresence.values()]
1335
+ this.pendingSeederPresence.clear()
1336
+ for (const { channelPubkey, senderPubkey, at } of pending) {
1337
+ if (this.closePromise || this.sendingPaused() || !this.desiredChannels.has(channelPubkey)) continue
1338
+ const channel = this.channels.get(channelPubkey)
1339
+ if (!channel) continue
1340
+ const configuredSeeders = channel.seeders || []
1341
+ if (configuredSeeders.length && !configuredSeeders.includes(senderPubkey)) continue
1342
+ this.markSeederActive(channelPubkey, senderPubkey, { announced: true, at })
1343
+ }
1344
+ }
1345
+
1346
+ discardSeederPresence (channels) {
1347
+ const channelSet = channels ? new Set(channels) : null
1348
+ for (const [key, pending] of this.pendingSeederPresence) {
1349
+ if (!channelSet || channelSet.has(pending.channelPubkey)) this.pendingSeederPresence.delete(key)
1350
+ }
1351
+ if (!this.pendingSeederPresence.size && this.seederPresenceFlushTimer) {
1352
+ this._clearTimeout(this.seederPresenceFlushTimer)
1353
+ this.seederPresenceFlushTimer = null
1354
+ }
1355
+ }
1356
+
1277
1357
  contentKeyStatus (contentKeyPubkey) {
1278
1358
  if (!contentKeyPubkey) return 'none'
1279
1359
  return contentKeyPubkey === this.contentKeyPubkey ? 'known' : 'unknown'
@@ -1425,7 +1505,9 @@ export class PrivateMessenger {
1425
1505
  onAsk: message => this.receive(pubkey, () => this.handleAsk(pubkey, message), message, { inboundType: 'ask' }),
1426
1506
  onReply: message => this.receive(pubkey, () => this.handleReply(pubkey, message), message, { inboundType: 'reply' }),
1427
1507
  onTell: message => this.receive(pubkey, () => this.handleTell(pubkey, message), message, { inboundType: 'tell' }),
1428
- onYell: message => this.receive(pubkey, () => this.handleYell(pubkey, message), message, { inboundType: 'yell' }),
1508
+ onYell: message => messageCode(message) === SEEDER_PRESENCE_CODE
1509
+ ? this.receiveSeederPresence(pubkey, message)
1510
+ : this.receive(pubkey, () => this.handleYell(pubkey, message), message, { inboundType: 'yell' }),
1429
1511
  onNym: message => this.receive(pubkey, () => this.handleNym(pubkey, message), message, { inboundType: 'nym' }),
1430
1512
  onMessage: message => {
1431
1513
  const type = eventType(message.event)
@@ -1972,6 +2054,7 @@ export class PrivateMessenger {
1972
2054
 
1973
2055
  unwatch (channels) {
1974
2056
  const pubkeys = channels ? uniq(Array.isArray(channels) ? channels : [channels]) : [...this.desiredChannels]
2057
+ this.discardSeederPresence(pubkeys)
1975
2058
  for (const extension of this.extensions) extension.unwatch?.(pubkeys)
1976
2059
  for (const pubkey of pubkeys) {
1977
2060
  this.desiredChannels.delete(pubkey)
@@ -2099,6 +2182,7 @@ export class PrivateMessenger {
2099
2182
  applyPause (reason) {
2100
2183
  if (typeof reason !== 'string' || !reason.trim()) throw new ValidationError('PAUSE_REASON_REQUIRED')
2101
2184
  this.assertOpen()
2185
+ if (reason !== 'capacity') this.discardSeederPresence()
2102
2186
  this.pauseReasons.add(reason)
2103
2187
  this.pauseRevisions.set(reason, (this.pauseRevisions.get(reason) || 0) + 1)
2104
2188
  const pausedError = this.pausedError()
@@ -4184,6 +4268,7 @@ export class PrivateMessenger {
4184
4268
  await (this.seedStorage ? this.seedStorage.removeLocal({ channelPubkey: pubkey }) : this.seedQueue?.removeBy('byChannel', pubkey))
4185
4269
  }
4186
4270
  for (const pubkey of stalePubkeys) this.dropRecoveryState(pubkey)
4271
+ this.discardSeederPresence(stalePubkeys)
4187
4272
  this.state = state
4188
4273
  if (stalePubkeys.length) await this.removeChannelStates(stalePubkeys)
4189
4274
  })
@@ -4221,6 +4306,7 @@ export class PrivateMessenger {
4221
4306
  this.decryptionScheduler.close()
4222
4307
  this.stopDecryptionForeground?.()
4223
4308
  if (this.closePromise) return this.closePromise
4309
+ this.discardSeederPresence()
4224
4310
  const initSettledPromise = this.initSettledPromise
4225
4311
  this.sendRoutingLifetime.abort()
4226
4312
  this.publicationRelayHealth.clear()