Skip to content

Commit 11b4b52

Browse files
authored
Close timeout (#93)
* Add closeTimeout to websocket configuration * Add testCloseTimeout * Use swift-websocket 1.2.0
1 parent 8cb5af3 commit 11b4b52

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ let package = Package(
1616
],
1717
dependencies: [
1818
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.5.0"),
19-
.package(url: "https://github.com/hummingbird-project/swift-websocket.git", from: "1.0.0"),
19+
.package(url: "https://github.com/hummingbird-project/swift-websocket.git", from: "1.2.0"),
2020
.package(url: "https://github.com/apple/swift-nio-extras.git", from: "1.22.0"),
2121
.package(url: "https://github.com/apple/swift-nio-ssl.git", from: "2.5.0"),
2222
],

Sources/HummingbirdWebSocket/WebSocketChannel.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public struct HTTP1WebSocketUpgradeChannel: ServerChildChannel, HTTPChannelHandl
8383
configuration: .init(
8484
extensions: extensions,
8585
autoPing: configuration.autoPing,
86+
closeTimeout: configuration.closeTimeout,
8687
validateUTF8: configuration.validateUTF8
8788
),
8889
asyncChannel: asyncChannel,

Sources/HummingbirdWebSocket/WebSocketServerConfiguration.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public struct WebSocketServerConfiguration: Sendable {
2222
public var extensions: [any WebSocketExtensionBuilder]
2323
/// Auto ping
2424
public var autoPing: AutoPingSetup
25+
/// How long server should wait for close frame from client before timing out
26+
public var closeTimeout: Duration
2527
/// Should we check text messages are valid UTF8
2628
public var validateUTF8: Bool
2729

@@ -35,11 +37,13 @@ public struct WebSocketServerConfiguration: Sendable {
3537
maxFrameSize: Int = (1 << 14),
3638
extensions: [WebSocketExtensionFactory] = [],
3739
autoPing: AutoPingSetup = .enabled(timePeriod: .seconds(30)),
40+
closeTimeout: Duration = .seconds(15),
3841
validateUTF8: Bool = false
3942
) {
4043
self.maxFrameSize = maxFrameSize
4144
self.extensions = extensions.map { $0.build() }
4245
self.autoPing = autoPing
46+
self.closeTimeout = closeTimeout
4347
self.validateUTF8 = validateUTF8
4448
}
4549
}

Tests/HummingbirdWebSocketTests/WebSocketTests.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,25 @@ final class HummingbirdWebSocketTests: XCTestCase {
573573
}
574574
}
575575

576+
func testCloseTimeout() async throws {
577+
let app = Application(
578+
router: Router(),
579+
server: .http1WebSocketUpgrade { _, _, _ in
580+
.upgrade([:]) { _, _, _ in
581+
try await cancelWhenGracefulShutdown {
582+
try await Task.sleep(for: .seconds(15))
583+
XCTFail("Should not reach here")
584+
}
585+
}
586+
}
587+
)
588+
try await app.test(.live) { client in
589+
_ = try await client.ws("/", configuration: .init(closeTimeout: .seconds(1))) { _, outbound, _ in
590+
try await outbound.write(.text("Hello"))
591+
}
592+
}
593+
}
594+
576595
func testUnrecognisedOpcode() async throws {
577596
let app = Application(
578597
router: Router(),

0 commit comments

Comments
 (0)