mirror of
https://github.com/Ride-The-Lightning/RTL.git
synced 2026-08-13 12:33:07 +02:00
Bound CLN alias resolution on peers and route lookups (#1501)
RTL resolves peer aliases by calling listnodes once per peer. A prior fix
(1cec7b1) bounded this to 20 concurrent calls plus a cache for the channel
list, but the peers list and route lookup still used an unbounded Promise.all,
firing one request per peer at once. On nodes with many peers this overwhelms
clnrest and fails with 'Resource temporarily unavailable (os error 11)'
(EAGAIN), so aliases fall back to raw node IDs.
- peers.ts and network.ts getRoute now resolve aliases via
runWithConcurrencyLimit(tasks, 20, ...), matching the channel list.
- Harden runWithConcurrencyLimit to call done() immediately for an empty task
list; otherwise an empty peers/route set would never send a response.
- Give the alias cache a 6h TTL and a max size (evicting oldest) so aliases
refresh without an RTL restart and the cache can't grow unbounded.
This commit is contained in:
parent
75dba90fae
commit
fbd336a89b
7 changed files with 68 additions and 13 deletions
|
|
@ -4,6 +4,10 @@ import { Common } from '../../utils/common.js';
|
|||
let options = null;
|
||||
const logger = Logger;
|
||||
const common = Common;
|
||||
// Alias cache: peerId -> { alias, ts }. Bounded by a TTL so an updated node alias is picked
|
||||
// up without an RTL restart, and by a max size so it can't grow unbounded (evicts oldest).
|
||||
const ALIAS_CACHE_TTL = 6 * 60 * 60 * 1000; // 6 hours
|
||||
const ALIAS_CACHE_MAX = 5000;
|
||||
const aliasCache = new Map();
|
||||
export const getRoute = (req, res, next) => {
|
||||
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Network', msg: 'Getting Network Routes..' });
|
||||
|
|
@ -15,7 +19,10 @@ export const getRoute = (req, res, next) => {
|
|||
options.body = req.body;
|
||||
request.post(options).then((body) => {
|
||||
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Network', msg: 'Network Routes Received', data: body });
|
||||
return Promise.all(body.route?.map((rt) => getAlias(req.session.selectedNode, rt, 'id'))).then((values) => {
|
||||
// Resolve hop aliases with a bounded number of concurrent listnodes calls, matching the
|
||||
// peers/channels paths, so a long route can't storm clnrest (#1501).
|
||||
const getRouteAliasesTasks = (body.route || []).map((rt) => () => getAlias(req.session.selectedNode, rt, 'id'));
|
||||
common.runWithConcurrencyLimit(getRouteAliasesTasks, 20, () => {
|
||||
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Peers', msg: 'Network Routes with Alias Received', data: body });
|
||||
res.status(200).json(body || []);
|
||||
});
|
||||
|
|
@ -87,8 +94,9 @@ export const getAlias = (selNode, peer, id) => {
|
|||
peer.alias = '';
|
||||
return Promise.resolve(peer);
|
||||
}
|
||||
if (aliasCache.has(peerId)) {
|
||||
peer.alias = aliasCache.get(peerId);
|
||||
const cached = aliasCache.get(peerId);
|
||||
if (cached && (Date.now() - cached.ts) < ALIAS_CACHE_TTL) {
|
||||
peer.alias = cached.alias;
|
||||
return Promise.resolve(peer);
|
||||
}
|
||||
options.url = selNode.settings.lnServerUrl + '/v1/listnodes';
|
||||
|
|
@ -96,7 +104,13 @@ export const getAlias = (selNode, peer, id) => {
|
|||
return request.post(options).then((body) => {
|
||||
logger.log({ selectedNode: selNode, level: 'DEBUG', fileName: 'Network', msg: 'Peer Alias Finished', data: body });
|
||||
const alias = body.nodes?.[0]?.alias || peerId.substring(0, 20);
|
||||
aliasCache.set(peerId, alias);
|
||||
// Re-insert so a refreshed entry moves to the most-recent position, then evict the
|
||||
// oldest if we're over the cap (Map preserves insertion order).
|
||||
aliasCache.delete(peerId);
|
||||
aliasCache.set(peerId, { alias, ts: Date.now() });
|
||||
if (aliasCache.size > ALIAS_CACHE_MAX) {
|
||||
aliasCache.delete(aliasCache.keys().next().value);
|
||||
}
|
||||
peer.alias = alias;
|
||||
return peer;
|
||||
}).catch((errRes) => {
|
||||
|
|
|
|||
|
|
@ -15,7 +15,11 @@ export const getPeers = (req, res, next) => {
|
|||
request.post(options).then((body) => {
|
||||
logger.log({ selectedNode: req.session.selectedNode, level: 'DEBUG', fileName: 'Peers', msg: 'Peers List Received', data: body });
|
||||
const peers = !body.peers ? [] : body.peers;
|
||||
return Promise.all(peers?.map((peer) => getAlias(req.session.selectedNode, peer, 'id'))).then((values) => {
|
||||
// Resolve peer aliases with a bounded number of concurrent listnodes calls. An unbounded
|
||||
// Promise.all here fires one request per peer at once, which overwhelms clnrest on nodes
|
||||
// with many peers and fails with "Resource temporarily unavailable (os error 11)" (#1501).
|
||||
const getPeerAliasesTasks = peers.map((peer) => () => getAlias(req.session.selectedNode, peer, 'id'));
|
||||
common.runWithConcurrencyLimit(getPeerAliasesTasks, 20, () => {
|
||||
logger.log({ selectedNode: req.session.selectedNode, level: 'INFO', fileName: 'Peers', msg: 'Sorted Peers List Received', data: body.peers });
|
||||
res.status(200).json(body.peers || []);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -609,7 +609,12 @@ export class CommonService {
|
|||
return JSON.parse(dataStr);
|
||||
};
|
||||
this.runWithConcurrencyLimit = (tasks, limit, done) => {
|
||||
const results = new Array(tasks.length);
|
||||
const results = new Array(tasks?.length || 0);
|
||||
// No tasks: the start loop below never runs, so 'done' would never fire and the
|
||||
// response would hang. Resolve immediately for empty lists (e.g. a node with no peers).
|
||||
if (!tasks || tasks.length === 0) {
|
||||
return done(results);
|
||||
}
|
||||
let nextIndex = 0;
|
||||
let activeCount = 0;
|
||||
const runNext = () => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue