Skip to content

Commit c2e0aa4

Browse files
authored
Add Sendable conformances to WebsocketKit (vapor#131)
1 parent 7b6c5bc commit c2e0aa4

File tree

12 files changed

+205
-172
lines changed

12 files changed

+205
-172
lines changed

.github/workflows/test.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ concurrency:
55
on:
66
pull_request: { types: [opened, reopened, synchronize, ready_for_review] }
77
push: { branches: [ main ] }
8-
98
jobs:
10-
119
vapor-integration:
1210
if: ${{ !(github.event.pull_request.draft || false) }}
1311
runs-on: ubuntu-latest

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
DerivedData
66
.swiftpm
77
Package.resolved
8+
.devcontainer/

Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ let package = Package(
1616
.package(url: "https://github.com/apple/swift-nio.git", from: "2.53.0"),
1717
.package(url: "https://github.com/apple/swift-nio-extras.git", from: "1.16.0"),
1818
.package(url: "https://github.com/apple/swift-nio-ssl.git", from: "2.24.0"),
19-
.package(url: "https://github.com/apple/swift-nio-transport-services.git", from: "1.11.4"),
20-
.package(url: "https://github.com/apple/swift-atomics.git", from: "1.0.2"),
19+
.package(url: "https://github.com/apple/swift-nio-transport-services.git", from: "1.16.0"),
20+
.package(url: "https://github.com/apple/swift-atomics.git", from: "1.1.0"),
2121
],
2222
targets: [
2323
.target(name: "WebSocketKit", dependencies: [

Sources/WebSocketKit/Concurrency/WebSocket+Concurrency.swift

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,34 +40,42 @@ extension WebSocket {
4040
try await close(code: code).get()
4141
}
4242

43-
public func onText(_ callback: @escaping (WebSocket, String) async -> ()) {
44-
onText { socket, text in
45-
Task {
46-
await callback(socket, text)
43+
public func onText(_ callback: @Sendable @escaping (WebSocket, String) async -> ()) {
44+
self.eventLoop.execute {
45+
self.onText { socket, text in
46+
Task {
47+
await callback(socket, text)
48+
}
4749
}
4850
}
4951
}
5052

51-
public func onBinary(_ callback: @escaping (WebSocket, ByteBuffer) async -> ()) {
52-
onBinary { socket, binary in
53-
Task {
54-
await callback(socket, binary)
53+
public func onBinary(_ callback: @Sendable @escaping (WebSocket, ByteBuffer) async -> ()) {
54+
self.eventLoop.execute {
55+
self.onBinary { socket, binary in
56+
Task {
57+
await callback(socket, binary)
58+
}
5559
}
5660
}
5761
}
5862

59-
public func onPong(_ callback: @escaping (WebSocket) async -> ()) {
60-
onPong { socket in
61-
Task {
62-
await callback(socket)
63+
public func onPong(_ callback: @Sendable @escaping (WebSocket) async -> ()) {
64+
self.eventLoop.execute {
65+
self.onPong { socket in
66+
Task {
67+
await callback(socket)
68+
}
6369
}
6470
}
6571
}
6672

67-
public func onPing(_ callback: @escaping (WebSocket) async -> ()) {
68-
onPing { socket in
69-
Task {
70-
await callback(socket)
73+
public func onPing(_ callback: @Sendable @escaping (WebSocket) async -> ()) {
74+
self.eventLoop.execute {
75+
self.onPing { socket in
76+
Task {
77+
await callback(socket)
78+
}
7179
}
7280
}
7381
}
@@ -77,7 +85,7 @@ extension WebSocket {
7785
headers: HTTPHeaders = [:],
7886
configuration: WebSocketClient.Configuration = .init(),
7987
on eventLoopGroup: EventLoopGroup,
80-
onUpgrade: @escaping (WebSocket) async -> ()
88+
onUpgrade: @Sendable @escaping (WebSocket) async -> ()
8189
) async throws {
8290
return try await self.connect(
8391
to: url,
@@ -97,7 +105,7 @@ extension WebSocket {
97105
headers: HTTPHeaders = [:],
98106
configuration: WebSocketClient.Configuration = .init(),
99107
on eventLoopGroup: EventLoopGroup,
100-
onUpgrade: @escaping (WebSocket) async -> ()
108+
onUpgrade: @Sendable @escaping (WebSocket) async -> ()
101109
) async throws {
102110
return try await self.connect(
103111
to: url,
@@ -121,7 +129,7 @@ extension WebSocket {
121129
headers: HTTPHeaders = [:],
122130
configuration: WebSocketClient.Configuration = .init(),
123131
on eventLoopGroup: EventLoopGroup,
124-
onUpgrade: @escaping (WebSocket) async -> ()
132+
onUpgrade: @Sendable @escaping (WebSocket) async -> ()
125133
) async throws {
126134
return try await self.connect(
127135
scheme: scheme,

Sources/WebSocketKit/Exports.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
@_documentation(visibility: internal) @_exported import protocol NIOCore.EventLoopGroup
77
@_documentation(visibility: internal) @_exported import struct NIOCore.EventLoopPromise
88
@_documentation(visibility: internal) @_exported import class NIOCore.EventLoopFuture
9-
109
@_documentation(visibility: internal) @_exported import struct NIOHTTP1.HTTPHeaders
11-
1210
@_documentation(visibility: internal) @_exported import struct Foundation.URL
1311

1412
#else
@@ -19,9 +17,7 @@
1917
@_exported import protocol NIOCore.EventLoopGroup
2018
@_exported import struct NIOCore.EventLoopPromise
2119
@_exported import class NIOCore.EventLoopFuture
22-
2320
@_exported import struct NIOHTTP1.HTTPHeaders
24-
2521
@_exported import struct Foundation.URL
2622

2723
#endif

Sources/WebSocketKit/WebSocket+Connect.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ extension WebSocket {
1717
headers: HTTPHeaders = [:],
1818
configuration: WebSocketClient.Configuration = .init(),
1919
on eventLoopGroup: EventLoopGroup,
20-
onUpgrade: @escaping (WebSocket) -> ()
20+
onUpgrade: @Sendable @escaping (WebSocket) -> ()
2121
) -> EventLoopFuture<Void> {
2222
guard let url = URL(string: url) else {
2323
return eventLoopGroup.any().makeFailedFuture(WebSocketClient.Error.invalidURL)
@@ -45,7 +45,7 @@ extension WebSocket {
4545
headers: HTTPHeaders = [:],
4646
configuration: WebSocketClient.Configuration = .init(),
4747
on eventLoopGroup: EventLoopGroup,
48-
onUpgrade: @escaping (WebSocket) -> ()
48+
onUpgrade: @Sendable @escaping (WebSocket) -> ()
4949
) -> EventLoopFuture<Void> {
5050
let scheme = url.scheme ?? "ws"
5151
return self.connect(
@@ -83,7 +83,7 @@ extension WebSocket {
8383
headers: HTTPHeaders = [:],
8484
configuration: WebSocketClient.Configuration = .init(),
8585
on eventLoopGroup: EventLoopGroup,
86-
onUpgrade: @escaping (WebSocket) -> ()
86+
onUpgrade: @Sendable @escaping (WebSocket) -> ()
8787
) -> EventLoopFuture<Void> {
8888
return WebSocketClient(
8989
eventLoopGroupProvider: .shared(eventLoopGroup),
@@ -129,7 +129,7 @@ extension WebSocket {
129129
proxyConnectDeadline: NIODeadline = NIODeadline.distantFuture,
130130
configuration: WebSocketClient.Configuration = .init(),
131131
on eventLoopGroup: EventLoopGroup,
132-
onUpgrade: @escaping (WebSocket) -> ()
132+
onUpgrade: @Sendable @escaping (WebSocket) -> ()
133133
) -> EventLoopFuture<Void> {
134134
return WebSocketClient(
135135
eventLoopGroupProvider: .shared(eventLoopGroup),
@@ -171,7 +171,7 @@ extension WebSocket {
171171
proxyConnectDeadline: NIODeadline = NIODeadline.distantFuture,
172172
configuration: WebSocketClient.Configuration = .init(),
173173
on eventLoopGroup: EventLoopGroup,
174-
onUpgrade: @escaping (WebSocket) -> ()
174+
onUpgrade: @Sendable @escaping (WebSocket) -> ()
175175
) -> EventLoopFuture<Void> {
176176
guard let url = URL(string: url) else {
177177
return eventLoopGroup.any().makeFailedFuture(WebSocketClient.Error.invalidURL)

0 commit comments

Comments
 (0)