MCPcopy Create free account
hub / github.com/ecomfe/tempad-dev / connectWithRetry

Function connectWithRetry

packages/mcp-server/src/cli.ts:97–121  ·  view source on GitHub ↗
(timeout: number)

Source from the content-addressed store, hash-verified

95}
96
97async function connectWithRetry(timeout: number): Promise<Socket> {
98 const startTime = Date.now()
99 let delay = CONNECT_RETRY_DELAY
100 while (Date.now() - startTime < timeout) {
101 try {
102 return await connectHub()
103 } catch (err: unknown) {
104 if (
105 err &&
106 typeof err === 'object' &&
107 'code' in err &&
108 (err.code === 'ENOENT' || err.code === 'ECONNREFUSED')
109 ) {
110 const remainingTime = timeout - (Date.now() - startTime)
111 const waitTime = Math.min(delay, remainingTime)
112 if (waitTime <= 0) break
113 await new Promise((r) => setTimeout(r, waitTime))
114 delay = Math.min(delay * 1.5, 1000)
115 } else {
116 throw err
117 }
118 }
119 }
120 throw new Error(`Failed to connect to Hub within ${timeout}ms.`)
121}
122
123function startHub(): ChildProcess {
124 log.info('Spawning new Hub process...')

Callers 2

waitForHubStartFunction · 0.85

Calls 1

connectHubFunction · 0.85

Tested by

no test coverage detected