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

Function makeProtocolSocket

packages/effect/src/rpc/RpcClient.ts:1303–1495  ·  view source on GitHub ↗
(options?: {
  readonly pingInterval?: Duration.Input | undefined
  readonly pingTimeout?: Duration.Input | undefined
  readonly retryTransientErrors?: boolean | undefined
  readonly retryPolicy?: Schedule.Schedule<any, Socket.SocketError> | undefined
  /**
   * Runs for each retried `SocketOpenError` when `retryTransientErrors` is enabled.
   * A ping timeout fails in-flight calls and is reported through `onPingTimeout`
   * instead. The returned `Effect<void>` cannot fail with a typed error or
   * require services; defects are logged and ignored so retries can continue.
   */
  readonly onTransientError?: ((error: RpcClientError) => Effect.Effect<void>) | undefined
  /**
   * Runs when an open connection is dropped because no server frame arrived
   * within `pingTimeout`, before `ConnectionHooks.onDisconnect` and before
   * in-flight calls fail with a `SocketReadError`. Defects are logged and ignored.
   *
   * @since 4.0.3
   */
  readonly onPingTimeout?: Effect.Effect<void> | undefined
})

Source from the content-addressed store, hash-verified

1301 * @since 4.0.0
1302 */
1303export const makeProtocolSocket = (options?: {
1304 readonly pingInterval?: Duration.Input | undefined
1305 readonly pingTimeout?: Duration.Input | undefined
1306 readonly retryTransientErrors?: boolean | undefined
1307 readonly retryPolicy?: Schedule.Schedule<any, Socket.SocketError> | undefined
1308 /**
1309 * Runs for each retried `SocketOpenError` when `retryTransientErrors` is enabled.
1310 * A ping timeout fails in-flight calls and is reported through `onPingTimeout`
1311 * instead. The returned `Effect<void>` cannot fail with a typed error or
1312 * require services; defects are logged and ignored so retries can continue.
1313 */
1314 readonly onTransientError?: ((error: RpcClientError) => Effect.Effect<void>) | undefined
1315 /**
1316 * Runs when an open connection is dropped because no server frame arrived
1317 * within `pingTimeout`, before `ConnectionHooks.onDisconnect` and before
1318 * in-flight calls fail with a `SocketReadError`. Defects are logged and ignored.
1319 *
1320 * @since 4.0.3
1321 */
1322 readonly onPingTimeout?: Effect.Effect<void> | undefined
1323}): Effect.Effect<
1324 Protocol["Service"],
1325 never,
1326 Scope.Scope | RpcSerialization.RpcSerialization | Socket.Socket
1327> =>
1328 Protocol.make(Effect.fnUntraced(function*(writeResponse, clientIds) {
1329 const socket = yield* Socket.Socket
1330 const serialization = yield* RpcSerialization.RpcSerialization
1331 const hooks = yield* Effect.serviceOption(ConnectionHooks)
1332 const requestClientMap = new Map<string | number, number>()
1333
1334 const writer = yield* socket.writer
1335
1336 let parser = serialization.makeUnsafe()
1337
1338 const supportsControlMessages = serialization.supportsControlMessages !== false
1339 // Encode each ping with the current connection's parser.
1340 const pinger = supportsControlMessages
1341 ? yield* makePinger(Effect.suspend(() => writer.write(parser.encode(constPing)!)), options)
1342 : { timeout: Effect.never, reset: constVoid, onFrame: constVoid }
1343 const onPingTimeout = Effect.ignoreCause(options?.onPingTimeout ?? Effect.void, {
1344 log: true,
1345 message: "RpcClient onPingTimeout hook failed"
1346 })
1347 let currentError: RpcClientError | undefined
1348
1349 const broadcast = (response: FromServerEncoded) => {
1350 const deliver = Effect.forEach(Array.from(clientIds), (clientId) => writeResponse(clientId, response))
1351 if (response._tag !== "Defect" && response._tag !== "ClientProtocolError") return deliver
1352 // A terminal response ends every request; clear routing before callers can start new ones
1353 requestClientMap.clear()
1354 return Effect.uninterruptible(deliver)
1355 }
1356 const broadcastError = (error: RpcClientError) => {
1357 currentError = error
1358 return broadcast({
1359 _tag: "ClientProtocolError",
1360 error

Callers 1

layerProtocolSocketFunction · 0.85

Calls 8

broadcastErrorFunction · 0.85
encodeMethod · 0.80
processDataFunction · 0.70
makeMethod · 0.65
writeMethod · 0.65
pipeMethod · 0.65
resetMethod · 0.65
failMethod · 0.65

Tested by

no test coverage detected