Something went wrong. Try again.
I'm going to build my own OpenClaw, with blackjack... and bun!
Something went wrong. Try again.
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839/** * remote/execute-concurrency.ts – In-memory concurrency guard for remote execute requests. */
/** * Tracks global and per-peer execute concurrency limits. * * This helper encapsulates shared mutable counters so remote execute handlers can * enforce admission checks and release slots consistently. */export class RemoteExecuteConcurrency { private readonly inflightByPeer = new Map<string, number>(); private inflightTotal = 0;
constructor( private readonly maxTotal: number, private readonly maxPerPeer: number ) {}
/** Return true when the peer cannot start another execute request right now. */ isAtLimit(peerInstanceId: string): boolean { return this.inflightTotal >= this.maxTotal || (this.inflightByPeer.get(peerInstanceId) || 0) >= this.maxPerPeer; }
/** Reserve one execute slot for the given peer. */ acquire(peerInstanceId: string): void { this.inflightTotal += 1; this.inflightByPeer.set(peerInstanceId, (this.inflightByPeer.get(peerInstanceId) || 0) + 1); }
/** Release one execute slot for the given peer. */ release(peerInstanceId: string): void { this.inflightTotal = Math.max(0, this.inflightTotal - 1); const count = (this.inflightByPeer.get(peerInstanceId) || 1) - 1; if (count <= 0) this.inflightByPeer.delete(peerInstanceId); else this.inflightByPeer.set(peerInstanceId, count); }}