openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 3.05 kB
JavaScript
import{o as resolveGlobalSingleton}from"./global-singleton-DolL9xGt.mjs";import{t as runWithAsyncStore}from"./async-store-Bl4w05El.mjs";import{t as runInDetachedAsyncContext}from"./detached-async-context-DLLuWsxc.mjs";import{versions}from"node:process";import{promiseHooks}from"node:v8";import{AsyncLocalStorage}from"node:async_hooks";import{performance}from"node:perf_hooks";
//!src/infra/main-thread-stall.ts
const STALL_MS=250;const UNATTRIBUTED_STALL_MS=1e3;const HEARTBEAT_MS=20;const MAX_PENDING_STALLS=8;const taskState=resolveGlobalSingleton(Symbol.for(`openclaw.mainThreadTask`),()=>({context:new AsyncLocalStorage}));function getMainThreadTaskName(){return taskState.observer?.currentName()??taskState.context.getStore()}function runWithMainThreadTask(e,t,n){const r=taskState.observer;return r?r.run(e,t,n):t()}function createMainThreadStallMonitor(e=()=>performance.now()){taskState.observer?.stop();const t=[];let n;const r=/* @__PURE__ */ new Float64Array(5);const i=0,a=1,o=2,s=3,c=4;r[i]=e();let l=0;let u=0;let d=false;let f=`unattributed`;let p;let m=f;let h;let g;let stopContinuations;const observeLoop=()=>{const n=e();const a=r[i];r[i]=n;const o=n-a;if(o<=UNATTRIBUTED_STALL_MS||t.some(e=>e.startedAt<=a+HEARTBEAT_MS&&e.endedAt>=n-HEARTBEAT_MS))return;if(t.length<MAX_PENDING_STALLS)t.push({elapsedMs:o,task:`unattributed`,taskMs:o,startedAt:a,endedAt:n});else u++};const segment=e=>{const t=e-r[s];if(t>r[c]){m=f;h=p;r[c]=t}r[s]=e};const begin=(e,t)=>{d=true;r[o]=r[s]=t;f=m=e;p=h=void 0;r[c]=0};const finish=e=>{d=false;const n=e-r[o];r[a]=Math.max(r[a],n);if(n>=STALL_MS){if(t.length<MAX_PENDING_STALLS)t.push({elapsedMs:n,task:h?`${m}:${h}`:m,taskMs:r[c],startedAt:r[o],endedAt:e});else l++}};const _={before(t){const n=taskState.context.getStore();if(!d&&n!==void 0){g=t;begin(n,e())}},after(t){if(g===t){g=void 0;const t=e();segment(t);finish(t)}}};const v={currentName:()=>d?f:void 0,stop(){clearInterval(n);n=void 0;stopContinuations?.();stopContinuations=void 0;g=void 0;if(taskState.observer===v){delete taskState.observer;taskState.context.disable()}t.length=0;l=0;u=0;r[a]=0},run(t,n,r){const i=e();const a=!d;if(a)begin(`unattributed`,i);else segment(i);const o=f;const s=p;f=t;p=r;try{return r===void 0?runWithAsyncStore(taskState.context,t,n):n()}finally{const t=e();segment(t);f=o;p=s;if(a)finish(t)}}};taskState.observer=v;return{setEventLoopSampling(t){if(t===Boolean(n))return;clearInterval(n);r[i]=e();n=t?runInDetachedAsyncContext(()=>setInterval(observeLoop,HEARTBEAT_MS).unref()):void 0;if(t&&!versions.bun){const e=promiseHooks.createHook(_);stopContinuations=()=>e()}else{stopContinuations?.();stopContinuations=void 0;if(g!==void 0){g=void 0;d=false}}},drain(){if(n)observeLoop();const e=t.splice(0);const i=l;const o=u;l=0;u=0;const s=r[a];r[a]=0;return{stalls:e,dropped:i,unattributedDropped:o,delayMaxMs:s}},reset(){r[i]=e();t.length=0;l=0;u=0;r[a]=0;r[o]=r[s]=e();r[c]=0;m=f;h=p},stop:()=>v.stop()}}export{getMainThreadTaskName as n,runWithMainThreadTask as r,createMainThreadStallMonitor as t};