@@ -71,15 +71,23 @@ public struct AsyncSocket: Sendable {
7171 try socket. setFlags ( . nonBlocking)
7272 }
7373
74- public static func connected< A: SocketAddress > ( to address: A ) async throws -> Self {
75- try await connected ( to: address, pool: ClientPoolLoader . shared. getPool ( ) )
76- }
77-
78- public static func connected< A: SocketAddress > ( to address: A , pool: some AsyncSocketPool ) async throws -> Self {
79- let socket = try Socket ( domain: Int32 ( address. makeStorage ( ) . ss_family) , type: Socket . stream)
80- let asyncSocket = try AsyncSocket ( socket: socket, pool: pool)
81- try await asyncSocket. connect ( to: address)
82- return asyncSocket
74+ public static func connected( to address: some SocketAddress , timeout: TimeInterval = 5 ) async throws -> Self {
75+ try await connected (
76+ to: address,
77+ pool: ClientPoolLoader . shared. getPool ( ) ,
78+ timeout: timeout
79+ )
80+ }
81+
82+ public static func connected( to address: some SocketAddress ,
83+ pool: some AsyncSocketPool ,
84+ timeout: TimeInterval = 5 ) async throws -> Self {
85+ try await withThrowingTimeout ( seconds: timeout) {
86+ let socket = try Socket ( domain: Int32 ( address. makeStorage ( ) . ss_family) , type: Socket . stream)
87+ let asyncSocket = try AsyncSocket ( socket: socket, pool: pool)
88+ try await asyncSocket. connect ( to: address)
89+ return asyncSocket
90+ }
8391 }
8492
8593 @Sendable
@@ -91,7 +99,7 @@ public struct AsyncSocket: Sendable {
9199 }
92100 }
93101
94- public func connect< A : SocketAddress > ( to address: A ) async throws {
102+ public func connect( to address: some SocketAddress ) async throws {
95103 return try await pool. loopUntilReady ( for: [ . write] , on: socket) {
96104 try socket. connect ( to: address)
97105 }
0 commit comments