MCPcopy Create free account
hub / github.com/Effect-TS/effect / onDefect

Function onDefect

packages/cluster/src/internal/entityManager.ts:340–397  ·  view source on GitHub ↗
(cause: Cause.Cause<never>)

Source from the content-addressed store, hash-verified

338 )
339
340 function onDefect(cause: Cause.Cause<never>): Effect.Effect<void> {
341 if (!activeServers.has(address.entityId)) {
342 return endLatch.open
343 }
344 if (isRestartingDueToDefect) {
345 return Effect.void
346 }
347 // Interrupt non-persisted streams instead of replaying them.
348 defectRequestIds = new Set()
349 const interrupted: Array<Message.IncomingRequestLocal<any>> = []
350 for (const [id, request] of activeRequests) {
351 if (!Context.get(request.rpc.annotations, Persisted) && RpcSchema.isStreamSchema(request.rpc.successSchema)) {
352 activeRequests.delete(id)
353 processedRequestIds.add(request.message.envelope.requestId)
354 interrupted.push(request.message)
355 } else {
356 defectRequestIds.add(id)
357 }
358 }
359 if (activeRequests.size === 0) {
360 state.lastActiveCheck = clock.unsafeCurrentTimeMillis()
361 }
362 isRestartingDueToDefect = true
363 const effect = writeRef.unsafeRebuild()
364 return Effect.logError("Defect in entity, restarting", cause).pipe(
365 Effect.andThen(Effect.forkIn(
366 Effect.forEach(
367 interrupted,
368 (message) =>
369 Effect.exit(retryRespond(
370 4,
371 Effect.suspend(() =>
372 message.respond(
373 new Reply.WithExit({
374 requestId: message.envelope.requestId,
375 id: snowflakeGen.unsafeNext(),
376 exit: Exit.interrupt(FiberId.none)
377 })
378 )
379 )
380 )),
381 { concurrency: "unbounded", discard: true }
382 ),
383 managerScope
384 )),
385 Effect.andThen(Effect.ignore(retryDriver.next(void 0))),
386 Effect.flatMap(() => activeServers.has(address.entityId) ? effect : endLatch.open),
387 Effect.ensuring(Effect.sync(() => {
388 isRestartingDueToDefect = false
389 })),
390 Effect.annotateLogs({
391 module: "EntityManager",
392 address,
393 runner: options.runnerAddress
394 }),
395 Effect.catchAllCause(onDefect)
396 )
397 }

Callers 1

onFromServerFunction · 0.85

Calls 9

retryRespondFunction · 0.85
unsafeRebuildMethod · 0.80
syncMethod · 0.80
getMethod · 0.65
addMethod · 0.65
pipeMethod · 0.65
nextMethod · 0.65
interruptMethod · 0.45

Tested by

no test coverage detected