Open History to see the last commit
1/**
2 * Node TCP socket transport for Effect Cluster runner-to-runner RPC.
3 *
4 * This module provides the shared Node layers used by socket-based cluster
5 * transports. `layerClientProtocol` opens TCP sockets to peer runner addresses
6 * and wraps them in the current RPC serialization protocol. `layerSocketServer`
7 * exposes the socket server that receives incoming runner RPC traffic.
8 *
9 * @since 4.0.0
10 */
11import * as Effect from "effect/Effect"
12import * as Layer from "effect/Layer"
13import * as Option from "effect/Option"
14import * as Runners from "effect/unstable/cluster/Runners"
15import * as ShardingConfig from "effect/unstable/cluster/ShardingConfig"
16import * as RpcClient from "effect/unstable/rpc/RpcClient"
17import * as RpcSerialization from "effect/unstable/rpc/RpcSerialization"
18import { Socket } from "effect/unstable/socket/Socket"
19import type * as SocketServer from "effect/unstable/socket/SocketServer"
20import * as NodeSocket from "./NodeSocket.ts"
21import * as NodeSocketServer from "./NodeSocketServer.ts"
22
23/**
24 * Provides the cluster `RpcClientProtocol` by opening TCP sockets to runner
25 * addresses and using the current RPC serialization service.
26 *
27 * @category layers
28 * @since 4.0.0
29 */
30export const layerClientProtocol: Layer.Layer<
31 Runners.RpcClientProtocol,
32 never,
33 RpcSerialization.RpcSerialization
34> = Layer.effect(Runners.RpcClientProtocol)(
35 Effect.gen(function*() {
36 const serialization = yield* RpcSerialization.RpcSerialization
37 return Effect.fnUntraced(function*(address) {
38 const socket = yield* NodeSocket.makeNet({
39 openTimeout: 1000,
40 timeout: 5500,
41 host: address.host,
42 port: address.port
43 })
44 return yield* RpcClient.makeProtocolSocket().pipe(
45 Effect.provideService(Socket, socket),
46 Effect.provideService(RpcSerialization.RpcSerialization, serialization)
47 )
48 }, Effect.orDie)
49 })
50)
51
52/**
53 * Provides the socket server used by cluster runners, listening on
54 * `ShardingConfig.runnerListenAddress` or `runnerAddress`.
55 *
56 * @category layers
57 * @since 4.0.0
58 */
59export const layerSocketServer: Layer.Layer<
60 SocketServer.SocketServer,
61 SocketServer.SocketServerError,
62 ShardingConfig.ShardingConfig
63> = Effect.gen(function*() {
64 const config = yield* ShardingConfig.ShardingConfig
65 const listenAddress = Option.orElse(config.runnerListenAddress, () => config.runnerAddress)
66 if (Option.isNone(listenAddress)) {
67 return yield* Effect.die("layerSocketServer: ShardingConfig.runnerListenAddress is None")
68 }
69 return NodeSocketServer.layer(listenAddress.value)
70}).pipe(Layer.unwrap)