perf(mesh): bounded swarm flush on joinMesh with background continue

Race flush against PEARCORD_GUILD_SWARM_FLUSH_MS and emit mesh-flush-timeout
when continuing without blocking guild open.

Co-authored-by: Cursor <[email protected]>
This commit is contained in:
Raven Scott
2026-05-23 04:15:25 -04:00
co-authored by Cursor
parent 929ea4d194
commit 02e5e43cc9
2 changed files with 21 additions and 3 deletions
+1 -1
View File
@@ -27,7 +27,7 @@ Create and load guilds in local storage, join peers on `pearcord:guild:{guildId}
| `PearcordGuild#create({ name, iconHash? })` | New guild + owner member + `general` text + `Voice Lobby`; `nsfwGateEnabled` defaults true (v0.8.174) |
| `PearcordGuild#joinByInvite(invite, userId)` | Materialize guild/channels from invite; add member; bump invite uses |
| `PearcordGuild#setActiveGuild(guild)` | Set `this.guild` for list/create operations |
| `PearcordGuild#joinMesh()` / `#leaveMesh()` | Hyperswarm on `topicToBuffer(guild.topic)` |
| `PearcordGuild#joinMesh(opts?)` / `#leaveMesh()` | Hyperswarm on `topicToBuffer(guild.topic)`; bounded `flush()` via `PEARCORD_GUILD_SWARM_FLUSH_MS` (default 2.5s) |
| `PearcordGuild#listChannels()` / `#listMembers()` | DB queries with thread meta attached |
| `PearcordGuild#createChannel`, `#createCategory`, `#createThread`, `#createForumThread` | Channel tree |
| `PearcordGuild#reorderChannelsInParent` | Reassign `position` for siblings under one category/root |
+20 -2
View File
@@ -860,7 +860,7 @@ class PearcordGuild extends EventEmitter {
broadcastGossip(this, RPC.TYPING_START, payload)
}
async joinMesh () {
async joinMesh (opts = {}) {
if (!this.guild) throw new Error('no active guild')
const topic = topicToBuffer(this.guild.topic)
this.swarm.removeAllListeners('connection')
@@ -902,7 +902,25 @@ class PearcordGuild extends EventEmitter {
})
})
await this.swarm.join(topic, { server: true, client: true })
await this.swarm.flush()
const flushMs = Number(
opts.flushMs ??
process.env.PEARCORD_GUILD_SWARM_FLUSH_MS ??
process.env.PEARCORD_SWARM_FLUSH_MS ??
2500
)
if (this.swarm.flush) {
let flushed = false
await Promise.race([
this.swarm.flush().then(() => {
flushed = true
}),
new Promise((resolve) => setTimeout(resolve, flushMs))
])
if (!flushed) {
this.emit('mesh-flush-timeout', { flushMs, guildId: this.guild.id })
void this.swarm.flush().catch(() => {})
}
}
return this.guild
}