Files
pearcord-platform/mesh-guild-backoff.js
T
Raven ScottandCursor 96f5893aad Complete Phase 409 platform perf, integrity, and mesh hardening.
Add adaptive poll helpers, discovery frame dedupe, poll DB read budget,
view integrity (draft prune, pin fallback, cross-ref audit, prefs checksum),
mesh quiet mode, topic merge counters, and discovery local-only guard.

Co-authored-by: Cursor <[email protected]>
2026-06-02 16:41:37 -04:00

34 lines
943 B
JavaScript

'use strict'
const crypto = require('hypercore-crypto')
const b4a = require('b4a')
/**
* Per-guild jitter for mesh retries (Phase 409 P409-14).
*/
function guildMeshJitterMs (guildId, attempt = 0, baseMs = 1000) {
const gid = String(guildId || '')
const seed = crypto.hash(b4a.from(`pearcord:mesh-jitter:${gid}`)).readUInt32BE(0)
const spread = Math.min(4000, Math.max(200, baseMs * 0.35))
const jitter = (seed % spread) + ((attempt * 137) % 200)
return Math.floor(baseMs + jitter)
}
/**
* Quiet mode: reduce sync churn when mesh has zero peers (P409-15).
*/
function shouldUseMeshQuietMode ({ peerCount = 0, meshBounded = false } = {}) {
if (meshBounded) return false
return Number(peerCount) <= 0
}
function meshQuietModePollMultiplier () {
return Math.max(2, Number(process.env.PEARCORD_MESH_QUIET_POLL_MULT) || 3)
}
module.exports = {
guildMeshJitterMs,
shouldUseMeshQuietMode,
meshQuietModePollMultiplier
}