From 7b365aa4e7185d16c16c7fa561a0d251d5e6a366 Mon Sep 17 00:00:00 2001 From: Ferdinand99 Date: Tue, 17 Nov 2026 06:12:53 +0100 Subject: [PATCH 1/3] fix(bridge): one configurable link limit for the bridge and the mirror (BRIDGE_MAX_LINKS, default 100) The bridge capped a server at 10 links while the server mirror linked up to 50, so the page could show 35 of 10. Both now share one limit set by the operator with BRIDGE_MAX_LINKS (1 to 500, default 100). A mirror links up to it and reports the rest as not linked; the Bridge page explains how to raise it and never shows a count above the limit. The relay spaces each webhook by what its platform allows. --- .env.example | 5 + docs/mirror.md | 7 +- docs/modules/bridge.md | 18 ++- docs/self-hosting.md | 1 + src/config.js | 9 ++ src/lib/bridgeLimit.js | 28 +++++ src/mirror/bridgeLink.js | 28 ++++- src/modules/bridge.js | 63 +++++++---- src/modules/lib/bridgeQueue.js | 28 ++++- test/bridgeLimit.test.js | 162 +++++++++++++++++++++++++++ test/mirrorBridgeLink.test.js | 38 ++++++- web-v2/src/__tests__/Bridge.test.jsx | 24 ++++ web-v2/src/moduleForms/Bridge.jsx | 13 ++- 13 files changed, 386 insertions(+), 38 deletions(-) create mode 100644 src/lib/bridgeLimit.js create mode 100644 test/bridgeLimit.test.js diff --git a/.env.example b/.env.example index df582f29..6598a7ee 100644 --- a/.env.example +++ b/.env.example @@ -126,6 +126,11 @@ WEB_PORT=3000 # network (127.0.0.1, 10.x, 192.168.x, 169.254.x ...) are refused. Allow them on a trusted LAN: # ALLOW_PRIVATE_URLS=true +# ---- Discord <-> Fluxer bridge (optional) ---- +# The most bridged channels one server can have, links made by hand and by a server mirror together. +# A whole mirrored server is linked channel by channel, so the default is 100. 1 to 500. +# BRIDGE_MAX_LINKS=100 + # ---- Push notifications for the Sylo app (optional) ---- # Postal Pigeon, the Hedgerow push relay: with this set, the Sylo app gets notifications with no # text in them (Pigeon and Apple only see a random reference; the app asks this instance for the diff --git a/docs/mirror.md b/docs/mirror.md index 7387ebe9..064bec15 100644 --- a/docs/mirror.md +++ b/docs/mirror.md @@ -73,8 +73,11 @@ so a message written on one side shows up on the other. Voice channels, categori is in the report ("Linking channels through the bridge" and the notes), and the rest are linked. - **Safe to run again.** "Copy again" links only the pairs that have no link yet. A channel that an owner unlinked on the Bridge page stays unlinked, and a pair the owners bridged by hand is never taken over or - changed (it is reported as skipped). A server can have at most 50 bridged channels this way (10 when - linking by hand), and a copy links 25 channels at a time. A channel made on Discord later (continuous mirroring) + changed (it is reported as skipped). A server can have at most 100 bridged channels, links made by hand + included (the operator can change this with `BRIDGE_MAX_LINKS`, 1 to 500). A mirror of a bigger server links + up to the limit and lists the rest in the report as "not linked: the server's bridge limit (100) is reached"; + they are linked on a later "Copy again" once the limit is raised or links are removed. A copy links 25 + channels at a time. A channel made on Discord later (continuous mirroring) is linked too. - **The links follow the mirror.** While the mirror is paused (by an owner, by Sylo after a failure, or because a server changed owner) or stopped, its links are paused, with no notice in each channel, and they run again when diff --git a/docs/modules/bridge.md b/docs/modules/bridge.md index f49d3e65..e3b54f0c 100644 --- a/docs/modules/bridge.md +++ b/docs/modules/bridge.md @@ -37,7 +37,7 @@ A link is never made from one side alone. shared with #chat in Fluxer Home on Fluxer ...". A link joins a Discord channel to a Fluxer channel, never two on the same platform. A channel is in one link -at a time, and a server has at most 10 links and 5 waiting requests. The Bridge module must be on in both +at a time, and a server has at most 100 links (the operator can change this, see below) and 5 waiting requests. The Bridge module must be on in both servers. Wrong codes are rate limited, and a few wrong ones in a row lock the attempts for a while. **Either side can unlink at any time** (the owner). Both webhooks are deleted, the message map goes, and @@ -47,8 +47,8 @@ is told too. **Links made by the server mirror.** A [server mirror](../mirror.md) links its copied text channels through the bridge without codes, because both owners already agreed to the mirror. Those links show "Name (server mirror)" as who made them, follow the mirror (paused with it, running again with it; the Bridge page cannot resume one on -its own while the mirror is paused) and follow every rule on this page. A server can have 50 of them (10 when you -link by hand). Unlink works for them as for any other link, and an unlinked channel is not linked again. If the +its own while the mirror is paused) and follow every rule on this page. They count toward the same limit as links +made by hand. Unlink works for them as for any other link, and an unlinked channel is not linked again. If the mirror is removed, its links stay, paused by the server that removed it: only that server's owner can resume them, and either owner can unlink them. The mirror switches this module on only the first time it links, and a server that switches it off keeps it off. @@ -92,7 +92,17 @@ stopped itself (see below) can be resumed by either server once the cause is fix - **Rate limits.** Each webhook has its own queue: sends are spaced out, a rate limit from the platform is waited out, a short outage is retried with growing pauses, and a message that waited more than five minutes is dropped. A queue holds at most 100 messages; a flooded channel drops the newest and counts it. - One server's queue never slows another's. + One server's queue never slows another's. The spacing follows what each platform allows for one webhook: + Fluxer allows 60 sends and 30 edits or deletes a minute (Sylo waits 1 second between sends and 2 between + edits and deletes), Discord does not publish its figures and answers with a retry time, which Sylo waits out + (it spaces sends about half a second apart). +- **How many links.** A server can have **100** bridged channels by default, made by hand or by a server + mirror together. The people who run the Sylo instance can change the number with `BRIDGE_MAX_LINKS` (1 to + 500, see [self-hosting](../self-hosting.md)). The limit is about Sylo's own work, not the platforms' + webhook limits: each link uses one webhook in each channel (a channel can hold 15 webhooks on Discord and on + Fluxer, a Fluxer server 1000), and each webhook has its own queue. When a server is at the limit, new + requests are refused and a mirror reports the channels it could not link; the Bridge page says so and how to + raise it. - **A failing link stops.** No permission, a webhook Sylo cannot make again, a deleted channel, or five failed deliveries in a row stop the link. Sylo says so in both channels, the **Health** page shows it as a problem with the reason, and it is not retried until someone presses **Resume**. diff --git a/docs/self-hosting.md b/docs/self-hosting.md index 8e70e1b2..b0f0047c 100644 --- a/docs/self-hosting.md +++ b/docs/self-hosting.md @@ -73,6 +73,7 @@ At least one platform must be configured. | `APPLE_PRIVATE_KEY` | — | The key's `.p8` file: PEM text, PEM on one line with literal `\n`, or base64 | | `APPLE_VENDOR_NUMBER` | — | Optional vendor number for a planned daily downloads digest | | `TRUST_PROXY` | one private hop with `DASHBOARD_URL` | Which proxies may set `X-Forwarded-For`: `false`, a hop count, or addresses/subnets. See [Behind a reverse proxy](#behind-a-reverse-proxy) | +| `BRIDGE_MAX_LINKS` | `100` | The most bridged channels one server can have, links by hand and links made by a [server mirror](mirror.md) together (1 to 500; Sylo does not start with another value). Each link uses a webhook in each of its two channels | | `ALLOW_PRIVATE_URLS` | `false` | Let server admins use feed and image URLs on private, loopback or link-local addresses (your own network). Off by default: those URLs are fetched by the server | | `PUSH_RELAY_URL` | `https://app.sylobot.com` | Where this instance sends [Sylo app notifications](mobile-app.md#notifications); empty turns them off | | `APNS_KEY_ID` | — | Only for the instance that talks to Apple itself (the hosted Sylo): APNs auth key id | diff --git a/src/config.js b/src/config.js index da48d712..b22ac800 100644 --- a/src/config.js +++ b/src/config.js @@ -6,6 +6,7 @@ import { resolveSessionSecret } from './lib/sessionSecret.js'; import { normalisePrivateKey } from './lib/appStoreConnect.js'; import { checkPigeonUrl } from './lib/pigeonUrl.js'; import { commandPrefixError } from './lib/commandPrefix.js'; +import { parseBridgeMaxLinks } from './lib/bridgeLimit.js'; /** * Read a required string env var, or exit with a clear message. @@ -282,6 +283,13 @@ if (commandPrefixProblem) { process.exit(1); } +// The most bridged channels one server can have (the Bridge module and the server mirror share it). +const bridgeMaxLinks = parseBridgeMaxLinks(process.env.BRIDGE_MAX_LINKS); +if (bridgeMaxLinks.error) { + console.error(`[config] ${bridgeMaxLinks.error}`); + process.exit(1); +} + export const config = Object.freeze({ // Discord (null when this install does not run on Discord) discordToken, @@ -294,6 +302,7 @@ export const config = Object.freeze({ fluxerWebUrl, fluxerApiUrl, commandPrefix, + bridgeMaxLinks: bridgeMaxLinks.value, // Optional: register slash commands to these guild(s) for instant availability // during development (one id, or a comma/space-separated list). When empty, // commands are registered globally (can take up to ~1 hour to propagate). diff --git a/src/lib/bridgeLimit.js b/src/lib/bridgeLimit.js new file mode 100644 index 00000000..c912d9de --- /dev/null +++ b/src/lib/bridgeLimit.js @@ -0,0 +1,28 @@ +// The most bridged channels a server can have, set by the operator with BRIDGE_MAX_LINKS (src/config.js). +// +// The real limits are the platforms' webhook limits and the relay queue, not this number: Sylo makes one +// webhook per bridged channel (Discord and Fluxer both allow 15 per channel, Fluxer 1000 per server), and the +// relay spaces the sends of each webhook inside what the platform allows (src/modules/lib/bridgeQueue.js). +// A server mirror links every copied text channel, so the default has to be well above a typical server. + +export const BRIDGE_MAX_LINKS_DEFAULT = 100; +export const BRIDGE_MAX_LINKS_MIN = 1; +export const BRIDGE_MAX_LINKS_MAX = 500; + +/** + * Read BRIDGE_MAX_LINKS. Unset or blank gives the default. + * @param {string | undefined | null} raw + * @returns {{ value: number, error: null } | { value: null, error: string }} + */ +export function parseBridgeMaxLinks(raw) { + const text = raw == null ? '' : String(raw).trim(); + if (text === '') return { value: BRIDGE_MAX_LINKS_DEFAULT, error: null }; + const n = /^\d+$/.test(text) ? Number(text) : NaN; + if (!Number.isInteger(n) || n < BRIDGE_MAX_LINKS_MIN || n > BRIDGE_MAX_LINKS_MAX) { + return { + value: null, + error: `BRIDGE_MAX_LINKS must be a whole number from ${BRIDGE_MAX_LINKS_MIN} to ${BRIDGE_MAX_LINKS_MAX} (default ${BRIDGE_MAX_LINKS_DEFAULT}).`, + }; + } + return { value: n, error: null }; +} diff --git a/src/mirror/bridgeLink.js b/src/mirror/bridgeLink.js index 43cd8c07..ccdd7df3 100644 --- a/src/mirror/bridgeLink.js +++ b/src/mirror/bridgeLink.js @@ -39,6 +39,7 @@ import { isModuleEnabled } from '../db/modules.js'; import { BridgeError, createMirrorLink, + getMaxLinks, enableBridgeForMirror, holdMirrorLink, releaseMirrorLink, @@ -223,8 +224,10 @@ export async function linkMirroredChannels( let linked = 0; let attempted = 0; + let full = false; const tried = []; for (const item of todo) { + if (full) break; if (attempted >= limit) break; attempted += 1; tried.push(item.sourceId); @@ -240,6 +243,15 @@ export async function linkMirroredChannels( linked += 1; } catch (err) { if (!(err instanceof BridgeError)) throw err; + if (err.code === 'limit') { + // A server is at its bridge limit: this pair and every pair still waiting cannot be linked. They are + // reported (not "remaining": nothing more will be tried until the limit is raised or a link is removed). + full = true; + const rest = todo.filter((t) => !tried.includes(t.sourceId) || t === item); + for (const t of rest) skipped.push(limitSkip(t)); + log.info('mirror', `pair ${pair.id}: ${rest.length} channels not linked, the bridge limit is reached`); + continue; + } log.info( 'mirror', `pair ${pair.id}: ${item.name} not linked (${err.code || err.status}): ${err.message}` @@ -253,5 +265,19 @@ export async function linkMirroredChannels( } if (attempted < limit && attempted < todo.length) await sleep(PAUSE_BETWEEN_MS); } - return { linked, already, remaining: Math.max(0, todo.length - attempted), tried, skipped }; + return { + linked, + already, + remaining: full ? 0 : Math.max(0, todo.length - attempted), + tried, + skipped, + }; } + +/** A pair that is not linked because a server is at its bridge limit (BRIDGE_MAX_LINKS). */ +const limitSkip = (item) => ({ + sourceId: item.sourceId, + name: item.name, + code: 'limit', + message: `the server's bridge limit (${getMaxLinks()}) is reached`, +}); diff --git a/src/modules/bridge.js b/src/modules/bridge.js index 8f4f3215..5bdb4fe7 100644 --- a/src/modules/bridge.js +++ b/src/modules/bridge.js @@ -36,8 +36,9 @@ import { createWebhookTransport } from '../platform/webhooks.js'; import { isAgeRestricted as ageOf, jumpUrl } from './quoteBoard.js'; import { safeFetch } from '../lib/safeFetch.js'; import { log } from '../lib/log.js'; +import { config } from '../config.js'; import { sweepMap } from '../lib/sweepMap.js'; -import { BridgeQueueError, classifyError, createRelayQueue } from './lib/bridgeQueue.js'; +import { BridgeQueueError, classifyError, createRelayQueue, relayGapMs } from './lib/bridgeQueue.js'; import { FILE_BYTES_LIMIT, escapeMarkdown, @@ -60,11 +61,22 @@ import { // The module has no settings of its own (modules/limits.js lists none); these are its ceilings, sent to the // page with the overview. -/** The most links a server can have, and the most requests it can have waiting. */ -export const MAX_LINKS = 10; +/** + * The most links a server can have, by hand and through a server mirror alike (src/mirror/bridgeLink.js links + * every copied text channel). The operator sets it with BRIDGE_MAX_LINKS (default 100, 1 to 500, see + * src/lib/bridgeLimit.js); tests replace it through useBridgeParts. + */ +let maxLinks = config.bridgeMaxLinks; +export const getMaxLinks = () => maxLinks; +/** The most requests a server can have waiting. */ export const MAX_REQUESTS = 5; -/** The ceiling for a server that is mirrored (src/mirror/bridgeLink.js links every copied text channel). */ -export const MAX_MIRROR_LINKS = 50; +/** The refusal for a server that is at the ceiling (the operator raises it with BRIDGE_MAX_LINKS). */ +const limitError = (code = '') => + new BridgeError( + `This server has reached its limit of ${maxLinks} bridged channels. The operator of this Sylo instance can raise it with BRIDGE_MAX_LINKS.`, + 409, + code + ); /** How many failed deliveries in a row stop a link. */ export const FAIL_THRESHOLD = 5; @@ -85,7 +97,7 @@ export class BridgeError extends Error { // --- wiring that tests replace ----------------------------------------------------------------------- let transport = createWebhookTransport(); -let queue = createRelayQueue(); +let queue = createRelayQueue({ platformGaps: true }); let download = (url, maxBytes) => safeFetch(url, { maxBytes, timeoutMs: 15_000 }); // One budget for every file download of the process, across all servers: files are held in memory until @@ -106,9 +118,10 @@ function releaseDownload(size) { } /** Use other parts (tests): a transport, a queue, a downloader, a download budget. No arguments: the real ones. */ -export function useBridgeParts({ transport: t, queue: q, download: d, budget: b } = {}) { +export function useBridgeParts({ transport: t, queue: q, download: d, budget: b, maxLinks: m } = {}) { + maxLinks = m ?? config.bridgeMaxLinks; transport = t ?? createWebhookTransport(); - queue = q ?? createRelayQueue(); + queue = q ?? createRelayQueue({ platformGaps: true }); download = d ?? ((url, maxBytes) => safeFetch(url, { maxBytes, timeoutMs: 15_000 })); budget = { ...DEFAULT_BUDGET, ...b }; } @@ -138,6 +151,8 @@ const hookOf = (link, side) => const sideOf = (link, key) => (link.guildA === key ? 'a' : link.guildB === key ? 'b' : null); const platformOf = (key) => splitPlatformGuildId(key)?.platform ?? 'discord'; const laneKey = (link, side) => `${link.id}|${side}`; +/** The pause the platform that owns the webhook on `side` needs between two calls (see relayGapMs). */ +const gapOf = (link, side, op) => relayGapMs(platformOf(guildOf(link, side)), op); const TEXT_TYPES = new Set([ChannelType.GuildText, ChannelType.GuildAnnouncement]); const channelIn = (key, channelId) => guildByKey(key)?.channels?.cache?.get?.(channelId) ?? null; @@ -363,9 +378,7 @@ export async function requestLink({ guildKey, channelId, options, actor }) { if (await store.channelHasPendingCode(guildKey, channelId)) { throw new BridgeError('That channel already has a request waiting. Cancel it to make a new one.', 409); } - if ((await store.linkCount(guildKey)) >= MAX_LINKS) { - throw new BridgeError(`A server can have at most ${MAX_LINKS} bridged channels.`, 409); - } + if ((await store.linkCount(guildKey)) >= maxLinks) throw limitError('limit'); if ((await store.pendingCodeCount(guildKey)) >= MAX_REQUESTS) { throw new BridgeError(`At most ${MAX_REQUESTS} requests can wait at once. Cancel one first.`, 409); } @@ -408,9 +421,7 @@ export async function acceptLink({ guildKey, channelId, code, options, actor }) const mine = checkChannel(guildKey, channelId); if (await store.linkForChannel(guildKey, channelId)) throw new BridgeError('That channel is already bridged.', 409); - if ((await store.linkCount(guildKey)) >= MAX_LINKS) { - throw new BridgeError(`A server can have at most ${MAX_LINKS} bridged channels.`, 409); - } + if ((await store.linkCount(guildKey)) >= maxLinks) throw limitError('limit'); const request = await store.peekCode(code); if (!request) throw new BridgeError(INVALID_CODE, 404); @@ -440,6 +451,14 @@ export async function acceptLink({ guildKey, channelId, code, options, actor }) if (await store.linkForChannel(request.guildId, request.channelId)) { throw new BridgeError('The other channel was bridged in the meantime.', 409); } + // The server that asked may have filled its limit since it made the request. + if ((await store.linkCount(request.guildId)) >= maxLinks) { + throw new BridgeError( + "The server that made this request has reached its limit of bridged channels, so it cannot take another. Ask the operator of this Sylo instance to raise BRIDGE_MAX_LINKS, or unlink a channel there.", + 409, + 'limit' + ); + } checkAge(guildKey, mine.channel, request.guildId, theirs.channel); // A webhook in each channel, made before the code is claimed (the claim is the atomic step: if two servers @@ -733,9 +752,7 @@ export async function createMirrorLink({ source, target, members = {}, open = fa throw new BridgeError('One of the channels is already bridged.', 409, 'exists'); } for (const key of [source.guildKey, target.guildKey]) { - if ((await store.linkCount(key)) >= MAX_MIRROR_LINKS) { - throw new BridgeError(`A server can have at most ${MAX_MIRROR_LINKS} bridged channels.`, 409, 'limit'); - } + if ((await store.linkCount(key)) >= maxLinks) throw limitError('limit'); } checkAge(source.guildKey, a.channel, target.guildKey, b.channel); // A channel only some members can see is linked only to a channel that is as closed: nothing private is @@ -1371,7 +1388,7 @@ export async function handleMessage(message, _config, key) { }; track( queue - .enqueue(laneKey(link, target), job, { op: 'send' }) + .enqueue(laneKey(link, target), job, { op: 'send', gapMs: gapOf(link, target, 'send') }) .catch((err) => noteFailure(link, target, err)) .finally(() => built?.release?.()) ); @@ -1425,7 +1442,9 @@ export async function handleUpdate({ new: updated }, _config, key) { return map.counterpartId; }; track( - queue.enqueue(laneKey(link, target), job, { op: 'edit' }).catch((err) => noteFailure(link, target, err)) + queue + .enqueue(laneKey(link, target), job, { op: 'edit', gapMs: gapOf(link, target, 'edit') }) + .catch((err) => noteFailure(link, target, err)) ); } @@ -1456,7 +1475,7 @@ async function deleteCopies(link, side, ids) { }; track( queue - .enqueue(laneKey(link, target), job, { op: 'delete' }) + .enqueue(laneKey(link, target), job, { op: 'delete', gapMs: gapOf(link, target, 'delete') }) .catch((err) => log.debug('bridge', `a delete was not carried across (link ${link.id}): ${err?.message ?? err}`) ) @@ -1517,7 +1536,7 @@ export async function handleJoin(member, _config, key) { if (!current || current.status !== 'active') return null; return (await withHook(current, target, (hook) => sendCopy(dstKey, hook, payload))).value; }, - { op: 'send' } + { op: 'send', gapMs: gapOf(link, target, 'send') } ) .catch((err) => noteFailure(link, target, err)) ); @@ -1634,7 +1653,7 @@ export async function bridgeOverview(key) { platform: platformOf(key), bothPlatforms: Boolean(runtime.clients.discord && runtime.clients.fluxer), enabled: (await getGuildModule(key, 'bridge')).enabled, - limits: { links: MAX_LINKS, requests: MAX_REQUESTS }, + limits: { links: maxLinks, requests: MAX_REQUESTS }, links: await Promise.all(links.map(async (l) => linkView(l, key, await mirrorHold(l)))), requests: requests.map((r) => ({ id: r.id, diff --git a/src/modules/lib/bridgeQueue.js b/src/modules/lib/bridgeQueue.js index 3a8c76da..1e52f70f 100644 --- a/src/modules/lib/bridgeQueue.js +++ b/src/modules/lib/bridgeQueue.js @@ -72,6 +72,21 @@ export function classifyError(err, op = 'send') { return { kind: 'transient' }; } +/** + * How long to wait between two calls to one webhook, by the platform that owns it. Fluxer publishes its limits: + * 60 sends, 30 edits and 30 deletes per webhook per minute, so a send is spaced 1 s apart and an edit or a delete + * 2 s apart (a little more than the exact figure). Discord does not publish webhook figures and reports them in + * its answers (a send is commonly 5 per 2 s per webhook), so it is spaced 450 ms apart and a "retry after" from + * the platform is still waited out. A bridged channel has one webhook of Sylo's on each side (a platform allows + * 15 per channel, Fluxer 1000 per server), so a server's links never share a webhook, and so never a queue. + * @param {'discord' | 'fluxer'} platform + * @param {'send' | 'edit' | 'delete'} [op] + */ +export function relayGapMs(platform, op = 'send') { + if (platform === 'fluxer') return op === 'send' ? 1050 : 2050; + return 450; +} + const defaultSleep = (ms) => new Promise((resolve) => { setTimeout(resolve, ms).unref?.(); @@ -79,6 +94,7 @@ const defaultSleep = (ms) => /** * @param {{ minGapMs?: number, maxQueued?: number, maxAgeMs?: number, maxAttempts?: number, + * platformGaps?: boolean (space each call by the `gapMs` it was queued with, see relayGapMs, instead of `minGapMs`), * sleep?: (ms: number) => Promise, now?: () => number }} [opts] */ export function createRelayQueue({ @@ -86,12 +102,15 @@ export function createRelayQueue({ maxQueued = 100, maxAgeMs = 5 * 60_000, maxAttempts = 4, + platformGaps = false, sleep = defaultSleep, now = Date.now, } = {}) { /** @type {Map} */ const lanes = new Map(); + const gapOf = (task) => (platformGaps && task.gapMs != null ? task.gapMs : minGapMs); + async function drain(key, lane) { lane.running = true; try { @@ -117,7 +136,7 @@ export function createRelayQueue({ lane.tasks.shift(); task.reject(Object.assign(err instanceof Error ? err : new Error(String(err)), { failure })); } - if (lane.tasks.length) await sleep(minGapMs); + if (lane.tasks.length) await sleep(Math.max(gapOf(task), gapOf(lane.tasks[0]))); } } finally { lane.running = false; @@ -131,10 +150,11 @@ export function createRelayQueue({ * @template T * @param {string} key the target webhook (one lane per webhook) * @param {() => Promise} run - * @param {{ op?: 'send' | 'edit' | 'delete' }} [opts] + * @param {{ op?: 'send' | 'edit' | 'delete', gapMs?: number }} [opts] `gapMs`: the pause this call needs + * from its neighbours on the webhook (see relayGapMs); the queue's own `minGapMs` when not given * @returns {Promise} */ - enqueue(key, run, { op = 'send' } = {}) { + enqueue(key, run, { op = 'send', gapMs } = {}) { let lane = lanes.get(key); if (!lane) { lane = { tasks: [], running: false, dropped: 0 }; @@ -145,7 +165,7 @@ export function createRelayQueue({ return Promise.reject(new BridgeQueueError('overflow')); } const result = new Promise((resolve, reject) => { - lane.tasks.push({ run, op, keep: op === 'delete', at: now(), attempts: 0, resolve, reject }); + lane.tasks.push({ run, op, gapMs, keep: op === 'delete', at: now(), attempts: 0, resolve, reject }); }); if (!lane.running) void drain(key, lane); return result; diff --git a/test/bridgeLimit.test.js b/test/bridgeLimit.test.js new file mode 100644 index 00000000..c9e8fa1d --- /dev/null +++ b/test/bridgeLimit.test.js @@ -0,0 +1,162 @@ +// The ceiling of bridged channels per server (BRIDGE_MAX_LINKS): how it is read, that the Bridge page and the +// manual linking share it with the server mirror, and the platform spacing of the relay queue. +import './helpers/tmpDb.js'; +import test, { afterEach, beforeEach } from 'node:test'; +import assert from 'node:assert/strict'; +import { setGuildModule } from '../src/db/modules.js'; +import { purgeGuild } from '../src/db/purge.js'; +import { config } from '../src/config.js'; +import { + BRIDGE_MAX_LINKS_DEFAULT, + BRIDGE_MAX_LINKS_MAX, + BRIDGE_MAX_LINKS_MIN, + parseBridgeMaxLinks, +} from '../src/lib/bridgeLimit.js'; +import { + BridgeError, + acceptLink, + bridgeOverview, + dropBridgeCache, + getMaxLinks, + requestLink, + useBridgeParts, +} from '../src/modules/bridge.js'; +import { createRelayQueue, relayGapMs } from '../src/modules/lib/bridgeQueue.js'; +import { D, F, installPlatforms, removePlatforms, useFakes } from './helpers/fakeBridge.js'; + +let p; +let fakes; + +beforeEach(async () => { + p = installPlatforms(); + useFakes(); + dropBridgeCache(); + for (const key of [D, F]) await purgeGuild(key); + fakes = useFakes(); + await setGuildModule(D, 'bridge', { enabled: true }); + await setGuildModule(F, 'bridge', { enabled: true }); +}); +afterEach(() => { + useBridgeParts(); + removePlatforms(); +}); + +/** Use the fakes again with a ceiling of `n` links per server. */ +const capAt = (n) => useBridgeParts({ transport: fakes.transport, queue: fakes.queue, maxLinks: n }); + +/** One link made through the real request and accept calls. */ +async function link(name) { + const dCh = p.channel(p.dGuild, { name: `d-${name}` }); + const fCh = p.channel(p.fGuild, { name: `f-${name}` }); + const made = await requestLink({ guildKey: D, channelId: dCh.id, actor: 'Alice' }); + return acceptLink({ guildKey: F, channelId: fCh.id, code: made.code, actor: 'Bob' }); +} +const refuses = (promise, pattern) => + assert.rejects(promise, (err) => { + assert.ok(err instanceof BridgeError); + assert.equal(err.status, 409); + assert.equal(err.code, 'limit'); + assert.match(err.message, pattern); + return true; + }); + +test('BRIDGE_MAX_LINKS: unset or blank is 100, a whole number from 1 to 500 is taken as it is', () => { + assert.equal(BRIDGE_MAX_LINKS_DEFAULT, 100); + assert.deepEqual([BRIDGE_MAX_LINKS_MIN, BRIDGE_MAX_LINKS_MAX], [1, 500]); + for (const raw of [undefined, null, '', ' ']) { + assert.deepEqual(parseBridgeMaxLinks(raw), { value: 100, error: null }); + } + assert.equal(parseBridgeMaxLinks('1').value, 1); + assert.equal(parseBridgeMaxLinks('500').value, 500); + assert.equal(parseBridgeMaxLinks(' 250 ').value, 250); +}); + +test('BRIDGE_MAX_LINKS: anything outside 1 to 500, or not a whole number, is refused with a message', () => { + for (const raw of ['0', '501', '1000', '-5', '1.5', 'abc', '1e2', '10 links', '0x10']) { + const res = parseBridgeMaxLinks(raw); + assert.equal(res.value, null, raw); + assert.match(res.error, /BRIDGE_MAX_LINKS must be a whole number from 1 to 500/, raw); + } +}); + +test('the configured ceiling is the default of 100 when the environment sets nothing', () => { + assert.equal(config.bridgeMaxLinks, 100); + assert.equal(getMaxLinks(), 100); + capAt(3); + assert.equal(getMaxLinks(), 3, "tests can replace it"); + useBridgeParts(); + assert.equal(getMaxLinks(), 100, 'and it goes back'); +}); + +test('a request is refused at the ceiling, and the refusal says how to raise it', async () => { + capAt(2); + await link('one'); + await link('two'); + const dCh = p.channel(p.dGuild, { name: 'd-three' }); + await refuses( + requestLink({ guildKey: D, channelId: dCh.id, actor: 'Alice' }), + /limit of 2 bridged channels.*BRIDGE_MAX_LINKS/s + ); +}); + +test('accepting is refused when the server that asked has filled its ceiling since', async () => { + capAt(2); + const dCh = p.channel(p.dGuild, { name: 'd-late' }); + const fCh = p.channel(p.fGuild, { name: 'f-late' }); + const made = await requestLink({ guildKey: D, channelId: dCh.id, actor: 'Alice' }); + // The Discord server fills its two links with another Fluxer server, so the Fluxer server that accepts is not full. + const other = p.extra('95'); + const otherF = `fluxer:${other.fGuild.id}`; + await setGuildModule(otherF, 'bridge', { enabled: true }); + for (const name of ['one', 'two']) { + const req = await requestLink({ guildKey: D, channelId: p.channel(p.dGuild, { name }).id, actor: 'A' }); + await acceptLink({ + guildKey: otherF, + channelId: p.channel(other.fGuild, { name }).id, + code: req.code, + actor: 'B', + }); + } + await refuses( + acceptLink({ guildKey: F, channelId: fCh.id, code: made.code, actor: 'Bob' }), + /server that made this request has reached its limit.*BRIDGE_MAX_LINKS/s + ); +}); + +test('the page gets the ceiling, and never a count above it for a server within it', async () => { + capAt(3); + await link('one'); + await link('two'); + const view = await bridgeOverview(D); + assert.equal(view.limits.links, 3); + assert.equal(view.links.length, 2); + await link('three'); + const full = await bridgeOverview(D); + assert.deepEqual([full.links.length, full.limits.links], [3, 3]); +}); + +test('the relay spaces a webhook by what its platform allows', () => { + // Fluxer: 60 sends, 30 edits and 30 deletes per webhook per minute + assert.ok(relayGapMs('fluxer', 'send') >= 1000); + assert.ok(relayGapMs('fluxer', 'edit') >= 2000); + assert.ok(relayGapMs('fluxer', 'delete') >= 2000); + // Discord: about 5 sends per 2 seconds per webhook + assert.ok(relayGapMs('discord', 'send') >= 400); + assert.equal(relayGapMs('discord'), relayGapMs('discord', 'send')); +}); + +test('a queue made for the platforms waits the gap each call was queued with, and a plain one its own', async () => { + const run = async (opts, gaps) => { + const sleeps = []; + const queue = createRelayQueue({ + minGapMs: 7, + sleep: async (ms) => sleeps.push(ms), + ...opts, + }); + await Promise.all(gaps.map((gapMs) => queue.enqueue('hook', async () => 1, { gapMs }))); + return sleeps; + }; + assert.deepEqual(await run({ platformGaps: true }, [1050, 1050, 2050]), [1050, 2050]); + assert.deepEqual(await run({}, [1050, 1050, 2050]), [7, 7], 'the gap of the queue when it is not asked'); + assert.deepEqual(await run({ platformGaps: true }, [undefined, undefined]), [7]); +}); diff --git a/test/mirrorBridgeLink.test.js b/test/mirrorBridgeLink.test.js index caa45c1b..d12a48e1 100644 --- a/test/mirrorBridgeLink.test.js +++ b/test/mirrorBridgeLink.test.js @@ -19,7 +19,6 @@ import { } from '../src/mirror/bridgeLink.js'; import { BridgeError, - MAX_MIRROR_LINKS, bridgeIdle, bridgeProblems, dropBridgeCache, @@ -367,7 +366,8 @@ test('a batch links at most the limit, and the next run carries on without tryin test('the ceiling of links per server is respected and reported', async () => { const pair = await livePair(); const one = await mirrored(pair, 'chat'); - for (let i = 0; i < MAX_MIRROR_LINKS; i += 1) { + useBridgeParts({ transport: fakes.transport, queue: fakes.queue, maxLinks: 4 }); + for (let i = 0; i < 4; i += 1) { await bridgeDb.createLink({ guildA: D, channelA: `filler-${i}`, @@ -380,9 +380,39 @@ test('the ceiling of links per server is respected and reported', async () => { const res = await run(pair); assert.equal(res.linked, 0); assert.deepEqual( - res.skipped.map((s) => [s.sourceId, s.code]), - [[one.d.id, 'limit']] + res.skipped.map((s) => [s.sourceId, s.code, s.message]), + [[one.d.id, 'limit', "the server's bridge limit (4) is reached"]] + ); +}); + +test('a mirror of more channels than the ceiling links up to it, reports the rest, and stops trying', async () => { + useBridgeParts({ transport: fakes.transport, queue: fakes.queue, maxLinks: 3 }); + const pair = await livePair(); + const chans = []; + for (let i = 0; i < 5; i += 1) chans.push(await mirrored(pair, `c${i}`)); + // a hand-made link counts toward the same ceiling + await bridgeDb.createLink({ + guildA: D, + channelA: 'by-hand', + guildB: F, + channelB: 'by-hand-f', + hookA: { id: 'ha', token: 'a' }, + hookB: { id: 'hb', token: 'b' }, + }); + const res = await run(pair); + assert.deepEqual([res.linked, res.remaining], [2, 0], 'two fit next to the one made by hand'); + assert.deepEqual( + res.skipped.map((s) => [s.sourceId, s.code, s.message]), + chans.slice(2).map((c) => [c.d.id, 'limit', "the server's bridge limit (3) is reached"]) ); + assert.equal(await bridgeDb.linkCount(D), 3, 'never above the ceiling'); + assert.equal(await bridgeDb.linkCount(F), 3); + // running it again links nothing more and reports the same pairs, until the limit is raised + const again = await run(pair); + assert.deepEqual([again.linked, again.already, again.remaining, again.skipped.length], [0, 2, 0, 3]); + useBridgeParts({ transport: fakes.transport, queue: fakes.queue, maxLinks: 100 }); + const raised = await run(pair); + assert.deepEqual([raised.linked, raised.skipped.length], [3, 0]); }); test('a webhook that cannot be made skips the pair and leaves nothing half made', async () => { diff --git a/web-v2/src/__tests__/Bridge.test.jsx b/web-v2/src/__tests__/Bridge.test.jsx index f9f1e907..4a40ef89 100644 --- a/web-v2/src/__tests__/Bridge.test.jsx +++ b/web-v2/src/__tests__/Bridge.test.jsx @@ -70,6 +70,30 @@ describe('Bridge page', () => { expect(api.getBridge).toHaveBeenCalledWith(GUILD); }); + it('shows the count against the limit, and explains how to raise it once the limit is reached', async () => { + const many = (n) => Array.from({ length: n }, (_, i) => link({ id: i + 1 })); + api.getBridge.mockResolvedValue(overview({ limits: { links: 3, requests: 5 }, links: many(2) })); + const first = renderAtGuild(, GUILD); + expect(await screen.findByRole('heading', { name: 'Bridged channels (2 of 3)' })).toBeTruthy(); + expect(screen.queryByTestId('bridge-limit-note')).toBeNull(); + first.unmount(); + + api.getBridge.mockResolvedValue(overview({ limits: { links: 3, requests: 5 }, links: many(3) })); + const second = renderAtGuild(, GUILD); + expect(await screen.findByRole('heading', { name: 'Bridged channels (3 of 3)' })).toBeTruthy(); + const note = screen.getByTestId('bridge-limit-note'); + expect(note.textContent).toMatch(/reached the limit of 3/); + expect(note.textContent).toMatch(/BRIDGE_MAX_LINKS/); + second.unmount(); + + // an operator who lowered the limit below what exists: never "X of Y" with X above Y + api.getBridge.mockResolvedValue(overview({ limits: { links: 3, requests: 5 }, links: many(5) })); + renderAtGuild(, GUILD); + expect(await screen.findByRole('heading', { name: 'Bridged channels (5)' })).toBeTruthy(); + expect(screen.queryByText(/5 of 3/)).toBeNull(); + expect(screen.getByTestId('bridge-limit-note').textContent).toMatch(/more bridged channels than the limit of 3/); + }); + it('names the other platform, and explains that nothing pings and bots are off', async () => { renderAtGuild(, GUILD); await screen.findByRole('heading', { name: 'Bridge' }); diff --git a/web-v2/src/moduleForms/Bridge.jsx b/web-v2/src/moduleForms/Bridge.jsx index 6e87bc89..233673ae 100644 --- a/web-v2/src/moduleForms/Bridge.jsx +++ b/web-v2/src/moduleForms/Bridge.jsx @@ -216,8 +216,19 @@ export default function Bridge() {

- Bridged channels ({data.links.length} of {data.limits.links}) + {data.links.length <= data.limits.links + ? `Bridged channels (${data.links.length} of ${data.limits.links})` + : `Bridged channels (${data.links.length})`}

+ {data.links.length >= data.limits.links ? ( +

+ {data.links.length > data.limits.links + ? `This server has more bridged channels than the limit of ${data.limits.links}. No new link can be made until some are unlinked or the limit is raised. ` + : `This server has reached the limit of ${data.limits.links} bridged channels, so no new link can be made, and a server mirror stops linking channels. `} + The limit is set by whoever runs this Sylo instance (the BRIDGE_MAX_LINKS setting; see + the self-hosting guide); if that is not you, ask them. +

+ ) : null} {data.links.length === 0 ? ( No channel is bridged yet. Ask for a link below, or use a code from the other server. From c24d0dbf250b53637999c2c0484b75509bc8964f Mon Sep 17 00:00:00 2001 From: Ferdinand99 Date: Tue, 17 Nov 2026 06:14:02 +0100 Subject: [PATCH 2/3] style: prettier --- src/mirror/bridgeLink.js | 5 ++++- src/modules/bridge.js | 2 +- test/bridgeLimit.test.js | 2 +- web-v2/src/__tests__/Bridge.test.jsx | 4 +++- web-v2/src/moduleForms/Bridge.jsx | 4 ++-- 5 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/mirror/bridgeLink.js b/src/mirror/bridgeLink.js index ccdd7df3..5ac10003 100644 --- a/src/mirror/bridgeLink.js +++ b/src/mirror/bridgeLink.js @@ -249,7 +249,10 @@ export async function linkMirroredChannels( full = true; const rest = todo.filter((t) => !tried.includes(t.sourceId) || t === item); for (const t of rest) skipped.push(limitSkip(t)); - log.info('mirror', `pair ${pair.id}: ${rest.length} channels not linked, the bridge limit is reached`); + log.info( + 'mirror', + `pair ${pair.id}: ${rest.length} channels not linked, the bridge limit is reached` + ); continue; } log.info( diff --git a/src/modules/bridge.js b/src/modules/bridge.js index 5bdb4fe7..37ffe0e7 100644 --- a/src/modules/bridge.js +++ b/src/modules/bridge.js @@ -454,7 +454,7 @@ export async function acceptLink({ guildKey, channelId, code, options, actor }) // The server that asked may have filled its limit since it made the request. if ((await store.linkCount(request.guildId)) >= maxLinks) { throw new BridgeError( - "The server that made this request has reached its limit of bridged channels, so it cannot take another. Ask the operator of this Sylo instance to raise BRIDGE_MAX_LINKS, or unlink a channel there.", + 'The server that made this request has reached its limit of bridged channels, so it cannot take another. Ask the operator of this Sylo instance to raise BRIDGE_MAX_LINKS, or unlink a channel there.', 409, 'limit' ); diff --git a/test/bridgeLimit.test.js b/test/bridgeLimit.test.js index c9e8fa1d..e9a36d33 100644 --- a/test/bridgeLimit.test.js +++ b/test/bridgeLimit.test.js @@ -83,7 +83,7 @@ test('the configured ceiling is the default of 100 when the environment sets not assert.equal(config.bridgeMaxLinks, 100); assert.equal(getMaxLinks(), 100); capAt(3); - assert.equal(getMaxLinks(), 3, "tests can replace it"); + assert.equal(getMaxLinks(), 3, 'tests can replace it'); useBridgeParts(); assert.equal(getMaxLinks(), 100, 'and it goes back'); }); diff --git a/web-v2/src/__tests__/Bridge.test.jsx b/web-v2/src/__tests__/Bridge.test.jsx index 4a40ef89..a1b28ffe 100644 --- a/web-v2/src/__tests__/Bridge.test.jsx +++ b/web-v2/src/__tests__/Bridge.test.jsx @@ -91,7 +91,9 @@ describe('Bridge page', () => { renderAtGuild(, GUILD); expect(await screen.findByRole('heading', { name: 'Bridged channels (5)' })).toBeTruthy(); expect(screen.queryByText(/5 of 3/)).toBeNull(); - expect(screen.getByTestId('bridge-limit-note').textContent).toMatch(/more bridged channels than the limit of 3/); + expect(screen.getByTestId('bridge-limit-note').textContent).toMatch( + /more bridged channels than the limit of 3/ + ); }); it('names the other platform, and explains that nothing pings and bots are off', async () => { diff --git a/web-v2/src/moduleForms/Bridge.jsx b/web-v2/src/moduleForms/Bridge.jsx index 233673ae..ee97309c 100644 --- a/web-v2/src/moduleForms/Bridge.jsx +++ b/web-v2/src/moduleForms/Bridge.jsx @@ -225,8 +225,8 @@ export default function Bridge() { {data.links.length > data.limits.links ? `This server has more bridged channels than the limit of ${data.limits.links}. No new link can be made until some are unlinked or the limit is raised. ` : `This server has reached the limit of ${data.limits.links} bridged channels, so no new link can be made, and a server mirror stops linking channels. `} - The limit is set by whoever runs this Sylo instance (the BRIDGE_MAX_LINKS setting; see - the self-hosting guide); if that is not you, ask them. + The limit is set by whoever runs this Sylo instance (the BRIDGE_MAX_LINKS setting; + see the self-hosting guide); if that is not you, ask them.

) : null} {data.links.length === 0 ? ( From 5594e7ce9ffcfc59949d698cbfb614aa154b1793 Mon Sep 17 00:00:00 2001 From: Ferdinand99 Date: Tue, 17 Nov 2026 06:25:42 +0100 Subject: [PATCH 3/3] test(bridge): the page limit follows the configured ceiling --- test/v2Bridge.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/v2Bridge.test.js b/test/v2Bridge.test.js index adc0ff66..6b95cc1a 100644 --- a/test/v2Bridge.test.js +++ b/test/v2Bridge.test.js @@ -170,7 +170,7 @@ test('the overview lists channels, shows what is set up, and says when the modul assert.equal(data.platform, 'discord'); assert.equal(data.bothPlatforms, true); assert.equal(data.enabled, true); - assert.deepEqual(data.limits, { links: 10, requests: 5 }); + assert.deepEqual(data.limits, { links: 100, requests: 5 }); assert.deepEqual(data.links, []); assert.deepEqual( data.channels.map((c) => [c.name, c.ageRestricted, c.taken]),