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

Function makeProtocolWorker

packages/effect/src/rpc/RpcClient.ts:1593–1770  ·  view source on GitHub ↗
(
  options: {
    readonly size: number
    readonly concurrency?: number | undefined
    readonly targetUtilization?: number | undefined
  } | {
    readonly minSize: number
    readonly maxSize: number
    readonly concurrency?: number | undefined
    readonly targetUtilization?: number | undefined
    readonly timeToLive: Duration.Input
  }
)

Source from the content-addressed store, hash-verified

1591 * @since 4.0.0
1592 */
1593export const makeProtocolWorker = (
1594 options: {
1595 readonly size: number
1596 readonly concurrency?: number | undefined
1597 readonly targetUtilization?: number | undefined
1598 } | {
1599 readonly minSize: number
1600 readonly maxSize: number
1601 readonly concurrency?: number | undefined
1602 readonly targetUtilization?: number | undefined
1603 readonly timeToLive: Duration.Input
1604 }
1605): Effect.Effect<
1606 Protocol["Service"],
1607 WorkerError,
1608 Scope.Scope | Worker.WorkerPlatform | Worker.Spawner
1609> =>
1610 Protocol.make(Effect.fnUntraced(function*(writeResponse, clientIds) {
1611 const worker = yield* Worker.WorkerPlatform
1612 const scope = yield* Effect.scope
1613 let workerId = 0
1614 const initialMessage = yield* Effect.serviceOption(RpcWorker.InitialMessage)
1615 const hooks = yield* Effect.serviceOption(ConnectionHooks)
1616
1617 const entries = new Map<string | number, {
1618 readonly clientId: number
1619 readonly worker: Worker.Worker<FromServerEncoded, FromClientEncoded | RpcWorker.InitialMessage.Encoded>
1620 readonly latch: Latch.Latch
1621 }>()
1622
1623 const broadcast = (response: FromServerEncoded) =>
1624 Effect.forEach(clientIds, (clientId) => writeResponse(clientId, response))
1625
1626 const acquire = Effect.gen(function*() {
1627 const id = workerId++
1628 const backing = yield* worker.spawn<FromServerEncoded, FromClientEncoded | RpcWorker.InitialMessage.Encoded>(id)
1629
1630 yield* backing.run((response) => {
1631 if (response._tag === "Exit") {
1632 const entry = entries.get(response.requestId)
1633 if (entry) {
1634 entries.delete(response.requestId)
1635 entry.latch.openUnsafe()
1636 return writeResponse(entry.clientId, response)
1637 }
1638 } else if (response._tag === "Defect") {
1639 for (const [requestId, entry] of entries) {
1640 entries.delete(requestId)
1641 entry.latch.openUnsafe()
1642 }
1643 return broadcast(response)
1644 } else if ("requestId" in response) {
1645 const entry = entries.get(response.requestId)
1646 if (entry) {
1647 return writeResponse(entry.clientId, response)
1648 }
1649 }
1650 return broadcast(response)

Callers

nothing calls this directly

Calls 15

writeResponseFunction · 0.85
spawnMethod · 0.80
addFinalizerMethod · 0.80
broadcastFunction · 0.70
makeMethod · 0.65
pipeMethod · 0.65
runMethod · 0.65
getMethod · 0.65
deleteMethod · 0.65
openUnsafeMethod · 0.65
sendMethod · 0.65
pushMethod · 0.65

Tested by

no test coverage detected