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:
@@ -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#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#joinByInvite(invite, userId)` | Materialize guild/channels from invite; add member; bump invite uses |
|
||||||
| `PearcordGuild#setActiveGuild(guild)` | Set `this.guild` for list/create operations |
|
| `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#listChannels()` / `#listMembers()` | DB queries with thread meta attached |
|
||||||
| `PearcordGuild#createChannel`, `#createCategory`, `#createThread`, `#createForumThread` | Channel tree |
|
| `PearcordGuild#createChannel`, `#createCategory`, `#createThread`, `#createForumThread` | Channel tree |
|
||||||
| `PearcordGuild#reorderChannelsInParent` | Reassign `position` for siblings under one category/root |
|
| `PearcordGuild#reorderChannelsInParent` | Reassign `position` for siblings under one category/root |
|
||||||
|
|||||||
@@ -860,7 +860,7 @@ class PearcordGuild extends EventEmitter {
|
|||||||
broadcastGossip(this, RPC.TYPING_START, payload)
|
broadcastGossip(this, RPC.TYPING_START, payload)
|
||||||
}
|
}
|
||||||
|
|
||||||
async joinMesh () {
|
async joinMesh (opts = {}) {
|
||||||
if (!this.guild) throw new Error('no active guild')
|
if (!this.guild) throw new Error('no active guild')
|
||||||
const topic = topicToBuffer(this.guild.topic)
|
const topic = topicToBuffer(this.guild.topic)
|
||||||
this.swarm.removeAllListeners('connection')
|
this.swarm.removeAllListeners('connection')
|
||||||
@@ -902,7 +902,25 @@ class PearcordGuild extends EventEmitter {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
await this.swarm.join(topic, { server: true, client: true })
|
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
|
return this.guild
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user