Skip to content

Commit f4c4702

Browse files
authored
add openTimeout options to NodeSocket.makeNet (#5590)
1 parent bc573e4 commit f4c4702

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

.changeset/cyan-cougars-send.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@effect/platform-node-shared": patch
3+
"@effect/cluster": patch
4+
"@effect/platform-node": patch
5+
"@effect/workflow": patch
6+
---
7+
8+
add openTimeout options to NodeSocket.makeNet

packages/platform-node-shared/src/NodeClusterSocketCommon.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ export const layerClientProtocol: Layer.Layer<
2525
const socket = yield* NodeSocket.makeNet({
2626
host: address.host,
2727
port: address.port,
28-
timeout: 5500
28+
timeout: 5500,
29+
openTimeout: 1000
2930
})
3031
return yield* RpcClient.makeProtocolSocket().pipe(
3132
Effect.provideService(Socket, socket),

packages/platform-node-shared/src/NodeSocket.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import * as Channel from "effect/Channel"
66
import type * as Chunk from "effect/Chunk"
77
import * as Context from "effect/Context"
88
import * as Deferred from "effect/Deferred"
9+
import type * as Duration from "effect/Duration"
910
import * as Effect from "effect/Effect"
1011
import * as FiberSet from "effect/FiberSet"
12+
import { identity } from "effect/Function"
1113
import * as Layer from "effect/Layer"
1214
import * as Scope from "effect/Scope"
1315
import * as Net from "node:net"
@@ -34,7 +36,9 @@ export const NetSocket: Context.Tag<NetSocket, Net.Socket> = Context.GenericTag(
3436
* @category constructors
3537
*/
3638
export const makeNet = (
37-
options: Net.NetConnectOpts
39+
options: Net.NetConnectOpts & {
40+
readonly openTimeout?: Duration.DurationInput | undefined
41+
}
3842
): Effect.Effect<Socket.Socket, Socket.SocketError> =>
3943
fromDuplex(
4044
Effect.scopeWith((scope) => {
@@ -65,15 +69,19 @@ export const makeNet = (
6569
})
6670
})
6771
)
68-
})
72+
}),
73+
options
6974
)
7075

7176
/**
7277
* @since 1.0.0
7378
* @category constructors
7479
*/
7580
export const fromDuplex = <RO>(
76-
open: Effect.Effect<Duplex, Socket.SocketError, RO>
81+
open: Effect.Effect<Duplex, Socket.SocketError, RO>,
82+
options?: {
83+
readonly openTimeout?: Duration.DurationInput | undefined
84+
}
7785
): Effect.Effect<Socket.Socket, never, Exclude<RO, Scope.Scope>> =>
7886
Effect.withFiberRuntime<Socket.Socket, never, Exclude<RO, Scope.Scope>>((fiber) => {
7987
let currentSocket: Duplex | undefined
@@ -96,7 +104,15 @@ export const fromDuplex = <RO>(
96104
})
97105
)
98106

99-
const conn = yield* Scope.extend(open, scope)
107+
const conn = yield* Scope.extend(open, scope).pipe(
108+
options?.openTimeout ?
109+
Effect.timeoutFail({
110+
duration: options.openTimeout,
111+
onTimeout: () =>
112+
new Socket.SocketGenericError({ reason: "Open", cause: new Error("Connection timed out") })
113+
}) :
114+
identity
115+
)
100116
conn.on("end", onEnd)
101117
conn.on("error", onError)
102118
conn.on("close", onClose)

0 commit comments

Comments
 (0)