Skip to content

Commit 4d5e912

Browse files
committed
refactor and address comments
1 parent 6139fd9 commit 4d5e912

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

Sources/LCLWebSocket/Client/WebSocketClient.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public struct WebSocketClient: Sendable, LCLWebSocketListenable {
4545
private var _onError: (@Sendable (Error) -> Void)?
4646

4747
private let isShutdown: ManagedAtomic<Bool>
48+
private let isMultiThreadedEventloop: Bool
4849

4950
/// Initialize the `WebSocketClient` instance on the given `EventLoopGroup`
5051
///
@@ -53,6 +54,7 @@ public struct WebSocketClient: Sendable, LCLWebSocketListenable {
5354
init(on eventloopGroup: any EventLoopGroup) {
5455
self.eventloopGroup = eventloopGroup
5556
self.isShutdown = ManagedAtomic(false)
57+
self.isMultiThreadedEventloop = self.eventloopGroup is MultiThreadedEventLoopGroup
5658
}
5759

5860
public mutating func onOpen(_ callback: (@Sendable (WebSocket) -> Void)?) {
@@ -307,7 +309,7 @@ extension WebSocketClient {
307309
#endif
308310

309311
#if canImport(Network)
310-
if self.eventloopGroup is MultiThreadedEventLoopGroup {
312+
if self.isMultiThreadedEventloop {
311313
return makeClientBootstrap()
312314
} else {
313315
return makeNIOTSConnectionBootstrap()
@@ -322,10 +324,10 @@ extension WebSocketClient {
322324
resolvedAddress: SocketAddress,
323325
scheme: WebSocketScheme,
324326
host: String
325-
) -> ChannelInitializerCallback {
327+
) -> ChannelInitializer {
326328
@Sendable
327329
func makeChannelInitializer(_ channel: Channel) -> EventLoopFuture<Void> {
328-
if self.eventloopGroup is MultiThreadedEventLoopGroup {
330+
if self.isMultiThreadedEventloop {
329331
if configuration.socketReuseAddress,
330332
let syncOptions = channel.syncOptions
331333
{

Sources/LCLWebSocket/LCLWebSocket+ChannelInitializer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import Foundation
1414
import NIOCore
1515

16-
typealias ChannelInitializerCallback = @Sendable (Channel) -> EventLoopFuture<Void>
16+
typealias ChannelInitializer = @Sendable (Channel) -> EventLoopFuture<Void>
1717

1818
/// Bind the connection to the given `device` using the given `Channel`.
1919
///

Sources/LCLWebSocket/Server/WebSocketServer.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public struct WebSocketServer: Sendable, LCLWebSocketListenable {
4747
private var _onError: (@Sendable (Error) -> Void)?
4848

4949
private let isShutdown: ManagedAtomic<Bool>
50+
private let isMultiThreadedEventLoop: Bool
5051

5152
/// Initialize a `WebSocketServer` instance on the given `EventLoopGroup` using the provided `serverConfiguration`
5253
///
@@ -60,6 +61,7 @@ public struct WebSocketServer: Sendable, LCLWebSocketListenable {
6061
self.eventloopGroup = eventloopGroup
6162
self.serverUpgradeConfiguration = serverUpgradeConfiguration
6263
self.isShutdown = ManagedAtomic(false)
64+
self.isMultiThreadedEventLoop = self.eventloopGroup is MultiThreadedEventLoopGroup
6365
}
6466

6567
public mutating func onOpen(_ callback: (@Sendable (WebSocket) -> Void)?) {
@@ -119,7 +121,7 @@ public struct WebSocketServer: Sendable, LCLWebSocketListenable {
119121
self.makeBootstrapAndBind(with: configuration, resolvedAddress: address) { channel in
120122
logger.debug("child channel: \(channel)")
121123
do {
122-
try self.makeChildChannelInitializer(configuration: configuration, channel: channel)
124+
try self.initializeChildChannel(using: configuration, on: channel)
123125
} catch {
124126
return channel.eventLoop.makeFailedFuture(error)
125127
}
@@ -230,7 +232,7 @@ public struct WebSocketServer: Sendable, LCLWebSocketListenable {
230232

231233
extension WebSocketServer {
232234

233-
private func makeChildChannelInitializer(configuration: LCLWebSocket.Configuration, channel: Channel) throws {
235+
private func initializeChildChannel(using configuration: LCLWebSocket.Configuration, on channel: Channel) throws {
234236
if self.eventloopGroup is MultiThreadedEventLoopGroup {
235237
if configuration.socketReuseAddress,
236238
let syncOptions = channel.syncOptions
@@ -326,6 +328,7 @@ extension WebSocketServer {
326328
func makeNIOTSListenerBootstrap() -> EventLoopFuture<Channel> {
327329

328330
let tcpOptions = NWProtocolTCP.Options()
331+
tcpOptions.connectionTimeout = Int(configuration.connectionTimeout.seconds)
329332
tcpOptions.noDelay = configuration.socketTcpNoDelay
330333

331334
return NIOTSListenerBootstrap(group: self.eventloopGroup)
@@ -371,7 +374,7 @@ extension WebSocketServer {
371374
#endif
372375

373376
#if canImport(Network)
374-
if self.eventloopGroup is MultiThreadedEventLoopGroup {
377+
if self.isMultiThreadedEventLoop {
375378
return makeServerBootstrap()
376379
} else {
377380
return makeNIOTSListenerBootstrap()
@@ -403,7 +406,7 @@ extension WebSocketServer {
403406
self.makeBootstrapAndBind(with: configuration, resolvedAddress: address) { channel in
404407
logger.debug("child channel: \(channel)")
405408
do {
406-
try self.makeChildChannelInitializer(configuration: configuration, channel: channel)
409+
try self.initializeChildChannel(using: configuration, on: channel)
407410
} catch {
408411
return channel.eventLoop.makeFailedFuture(error)
409412
}
@@ -465,7 +468,6 @@ extension WebSocketServer {
465468
),
466469
WebSocketHandler(websocket: websocket),
467470
])
468-
print(channel.pipeline.debugDescription, channel)
469471
return channel.eventLoop.makeSucceededFuture(UpgradeResult.websocket)
470472
} catch {
471473
return channel.eventLoop.makeFailedFuture(error)

0 commit comments

Comments
 (0)