@@ -100,6 +100,55 @@ public struct NIOAsyncChannel<Inbound: Sendable, Outbound: Sendable>: Sendable {
100
100
/// - channel: The ``Channel`` to wrap.
101
101
/// - configuration: The ``NIOAsyncChannel``s configuration.
102
102
@inlinable
103
+ public init (
104
+ wrappingChannelSynchronously channel: Channel ,
105
+ configuration: Configuration = . init( )
106
+ ) throws {
107
+ channel. eventLoop. preconditionInEventLoop ( )
108
+ self . channel = channel
109
+ ( self . _inbound, self . _outbound) = try channel. _syncAddAsyncHandlers (
110
+ backPressureStrategy: configuration. backPressureStrategy,
111
+ isOutboundHalfClosureEnabled: configuration. isOutboundHalfClosureEnabled,
112
+ closeOnDeinit: false
113
+ )
114
+ }
115
+
116
+ /// Initializes a new ``NIOAsyncChannel`` wrapping a ``Channel`` where the outbound type is `Never`.
117
+ ///
118
+ /// This initializer will finish the ``NIOAsyncChannel/outbound`` immediately.
119
+ ///
120
+ /// - Important: This **must** be called on the channel's event loop otherwise this init will crash. This is necessary because
121
+ /// we must install the handlers before any other event in the pipeline happens otherwise we might drop reads.
122
+ ///
123
+ /// - Parameters:
124
+ /// - channel: The ``Channel`` to wrap.
125
+ /// - configuration: The ``NIOAsyncChannel``s configuration.
126
+ @inlinable
127
+ public init (
128
+ wrappingChannelSynchronously channel: Channel ,
129
+ configuration: Configuration = . init( )
130
+ ) throws where Outbound == Never {
131
+ channel. eventLoop. preconditionInEventLoop ( )
132
+ self . channel = channel
133
+ ( self . _inbound, self . _outbound) = try channel. _syncAddAsyncHandlers (
134
+ backPressureStrategy: configuration. backPressureStrategy,
135
+ isOutboundHalfClosureEnabled: configuration. isOutboundHalfClosureEnabled,
136
+ closeOnDeinit: false
137
+ )
138
+
139
+ self . _outbound. finish ( )
140
+ }
141
+
142
+ /// Initializes a new ``NIOAsyncChannel`` wrapping a ``Channel``.
143
+ ///
144
+ /// - Important: This **must** be called on the channel's event loop otherwise this init will crash. This is necessary because
145
+ /// we must install the handlers before any other event in the pipeline happens otherwise we might drop reads.
146
+ ///
147
+ /// - Parameters:
148
+ /// - channel: The ``Channel`` to wrap.
149
+ /// - configuration: The ``NIOAsyncChannel``s configuration.
150
+ @available ( * , deprecated, renamed: " init(wrappingChannelSynchronously:configuration:) " , message: " This method has been deprecated since it defaults to deinit based resource teardown " )
151
+ @inlinable
103
152
public init (
104
153
synchronouslyWrapping channel: Channel ,
105
154
configuration: Configuration = . init( )
@@ -108,7 +157,8 @@ public struct NIOAsyncChannel<Inbound: Sendable, Outbound: Sendable>: Sendable {
108
157
self . channel = channel
109
158
( self . _inbound, self . _outbound) = try channel. _syncAddAsyncHandlers (
110
159
backPressureStrategy: configuration. backPressureStrategy,
111
- isOutboundHalfClosureEnabled: configuration. isOutboundHalfClosureEnabled
160
+ isOutboundHalfClosureEnabled: configuration. isOutboundHalfClosureEnabled,
161
+ closeOnDeinit: true
112
162
)
113
163
}
114
164
@@ -123,6 +173,7 @@ public struct NIOAsyncChannel<Inbound: Sendable, Outbound: Sendable>: Sendable {
123
173
/// - channel: The ``Channel`` to wrap.
124
174
/// - configuration: The ``NIOAsyncChannel``s configuration.
125
175
@inlinable
176
+ @available ( * , deprecated, renamed: " init(wrappingChannelSynchronously:configuration:) " , message: " This method has been deprecated since it defaults to deinit based resource teardown " )
126
177
public init (
127
178
synchronouslyWrapping channel: Channel ,
128
179
configuration: Configuration = . init( )
@@ -131,7 +182,8 @@ public struct NIOAsyncChannel<Inbound: Sendable, Outbound: Sendable>: Sendable {
131
182
self . channel = channel
132
183
( self . _inbound, self . _outbound) = try channel. _syncAddAsyncHandlers (
133
184
backPressureStrategy: configuration. backPressureStrategy,
134
- isOutboundHalfClosureEnabled: configuration. isOutboundHalfClosureEnabled
185
+ isOutboundHalfClosureEnabled: configuration. isOutboundHalfClosureEnabled,
186
+ closeOnDeinit: true
135
187
)
136
188
137
189
self . _outbound. finish ( )
@@ -149,12 +201,12 @@ public struct NIOAsyncChannel<Inbound: Sendable, Outbound: Sendable>: Sendable {
149
201
self . _outbound = outboundWriter
150
202
}
151
203
152
-
153
204
/// This method is only used from our server bootstrap to allow us to run the child channel initializer
154
205
/// at the right moment.
155
206
///
156
207
/// - Important: This is not considered stable API and should not be used.
157
208
@inlinable
209
+ @available ( * , deprecated, message: " This method has been deprecated since it defaults to deinit based resource teardown " )
158
210
public static func _wrapAsyncChannelWithTransformations(
159
211
synchronouslyWrapping channel: Channel ,
160
212
backPressureStrategy: NIOAsyncSequenceProducerBackPressureStrategies . HighLowWatermark ? = nil ,
@@ -165,6 +217,35 @@ public struct NIOAsyncChannel<Inbound: Sendable, Outbound: Sendable>: Sendable {
165
217
let ( inboundStream, outboundWriter) : ( NIOAsyncChannelInboundStream < Inbound > , NIOAsyncChannelOutboundWriter < Outbound > ) = try channel. _syncAddAsyncHandlersWithTransformations (
166
218
backPressureStrategy: backPressureStrategy,
167
219
isOutboundHalfClosureEnabled: isOutboundHalfClosureEnabled,
220
+ closeOnDeinit: true ,
221
+ channelReadTransformation: channelReadTransformation
222
+ )
223
+
224
+ outboundWriter. finish ( )
225
+
226
+ return . init(
227
+ channel: channel,
228
+ inboundStream: inboundStream,
229
+ outboundWriter: outboundWriter
230
+ )
231
+ }
232
+
233
+ /// This method is only used from our server bootstrap to allow us to run the child channel initializer
234
+ /// at the right moment.
235
+ ///
236
+ /// - Important: This is not considered stable API and should not be used.
237
+ @inlinable
238
+ public static func _wrapAsyncChannelWithTransformations(
239
+ wrappingChannelSynchronously channel: Channel ,
240
+ backPressureStrategy: NIOAsyncSequenceProducerBackPressureStrategies . HighLowWatermark ? = nil ,
241
+ isOutboundHalfClosureEnabled: Bool = false ,
242
+ channelReadTransformation: @Sendable @escaping ( Channel ) -> EventLoopFuture < Inbound >
243
+ ) throws -> NIOAsyncChannel < Inbound , Outbound > where Outbound == Never {
244
+ channel. eventLoop. preconditionInEventLoop ( )
245
+ let ( inboundStream, outboundWriter) : ( NIOAsyncChannelInboundStream < Inbound > , NIOAsyncChannelOutboundWriter < Outbound > ) = try channel. _syncAddAsyncHandlersWithTransformations (
246
+ backPressureStrategy: backPressureStrategy,
247
+ isOutboundHalfClosureEnabled: isOutboundHalfClosureEnabled,
248
+ closeOnDeinit: false ,
168
249
channelReadTransformation: channelReadTransformation
169
250
)
170
251
@@ -229,17 +310,20 @@ extension Channel {
229
310
@inlinable
230
311
func _syncAddAsyncHandlers< Inbound: Sendable , Outbound: Sendable > (
231
312
backPressureStrategy: NIOAsyncSequenceProducerBackPressureStrategies . HighLowWatermark ? ,
232
- isOutboundHalfClosureEnabled: Bool
313
+ isOutboundHalfClosureEnabled: Bool ,
314
+ closeOnDeinit: Bool
233
315
) throws -> ( NIOAsyncChannelInboundStream < Inbound > , NIOAsyncChannelOutboundWriter < Outbound > ) {
234
316
self . eventLoop. assertInEventLoop ( )
235
317
236
318
let inboundStream = try NIOAsyncChannelInboundStream< Inbound> . makeWrappingHandler(
237
319
channel: self ,
238
- backPressureStrategy: backPressureStrategy
320
+ backPressureStrategy: backPressureStrategy,
321
+ closeOnDeinit: closeOnDeinit
239
322
)
240
323
let writer = try NIOAsyncChannelOutboundWriter < Outbound > (
241
324
channel: self ,
242
- isOutboundHalfClosureEnabled: isOutboundHalfClosureEnabled
325
+ isOutboundHalfClosureEnabled: isOutboundHalfClosureEnabled,
326
+ closeOnDeinit: closeOnDeinit
243
327
)
244
328
return ( inboundStream, writer)
245
329
}
@@ -249,18 +333,21 @@ extension Channel {
249
333
func _syncAddAsyncHandlersWithTransformations< ChannelReadResult> (
250
334
backPressureStrategy: NIOAsyncSequenceProducerBackPressureStrategies . HighLowWatermark ? ,
251
335
isOutboundHalfClosureEnabled: Bool ,
336
+ closeOnDeinit: Bool ,
252
337
channelReadTransformation: @Sendable @escaping ( Channel ) -> EventLoopFuture < ChannelReadResult >
253
338
) throws -> ( NIOAsyncChannelInboundStream < ChannelReadResult > , NIOAsyncChannelOutboundWriter < Never > ) {
254
339
self . eventLoop. assertInEventLoop ( )
255
340
256
341
let inboundStream = try NIOAsyncChannelInboundStream< ChannelReadResult> . makeTransformationHandler(
257
342
channel: self ,
258
343
backPressureStrategy: backPressureStrategy,
344
+ closeOnDeinit: closeOnDeinit,
259
345
channelReadTransformation: channelReadTransformation
260
346
)
261
347
let writer = try NIOAsyncChannelOutboundWriter < Never > (
262
348
channel: self ,
263
- isOutboundHalfClosureEnabled: isOutboundHalfClosureEnabled
349
+ isOutboundHalfClosureEnabled: isOutboundHalfClosureEnabled,
350
+ closeOnDeinit: closeOnDeinit
264
351
)
265
352
return ( inboundStream, writer)
266
353
}
0 commit comments