Skip to content

Commit 5f54289

Browse files
authored
Add configuration for NIOAsyncChannel (#2464)
# Motivation While adopting the new `NIOAsyncChannel` type we saw an exploding number of parameters passed to the various connect/bind/configure methods. All these methods had in common that we had to pass configuration for the `NIOAsyncChannel`. # Modification This PR introduces a new type `NIOAsyncChannel.Configuration` which groups all four configuration parameters into a struct. # Result We can now write more concise methods that use a single configuration object.
1 parent 7e6dc1e commit 5f54289

File tree

6 files changed

+175
-322
lines changed

6 files changed

+175
-322
lines changed

Sources/NIOCore/AsyncChannel/AsyncChannel.swift

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,43 @@
3535
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
3636
@_spi(AsyncChannel)
3737
public final class NIOAsyncChannel<Inbound: Sendable, Outbound: Sendable>: Sendable {
38+
@_spi(AsyncChannel)
39+
public struct Configuration {
40+
/// The backpressure strategy of the ``NIOAsyncChannel/inboundStream``.
41+
public var backpressureStrategy: NIOAsyncSequenceProducerBackPressureStrategies.HighLowWatermark
42+
43+
/// If outbound half closure should be enabled. Outbound half closure is triggered once
44+
/// the ``NIOAsyncChannelWriter`` is either finished or deinitialized.
45+
public var isOutboundHalfClosureEnabled: Bool
46+
47+
/// The ``NIOAsyncChannel/inboundStream`` message's type.
48+
public var inboundType: Inbound.Type
49+
50+
/// The ``NIOAsyncChannel/outboundWriter`` message's type.
51+
public var outboundType: Outbound.Type
52+
53+
/// Initializes a new ``NIOAsyncChannel/Configuration``.
54+
///
55+
/// - Parameters:
56+
/// - backpressureStrategy: The backpressure strategy of the ``NIOAsyncChannel/inboundStream``. Defaults
57+
/// to a watermarked strategy (lowWatermark: 2, highWatermark: 10).
58+
/// - isOutboundHalfClosureEnabled: If outbound half closure should be enabled. Outbound half closure is triggered once
59+
/// the ``NIOAsyncChannelWriter`` is either finished or deinitialized. Defaults to `false`.
60+
/// - inboundType: The ``NIOAsyncChannel/inboundStream`` message's type.
61+
/// - outboundType: The ``NIOAsyncChannel/outboundWriter`` message's type.
62+
public init(
63+
backpressureStrategy: NIOAsyncSequenceProducerBackPressureStrategies.HighLowWatermark = .init(lowWatermark: 2, highWatermark: 10),
64+
isOutboundHalfClosureEnabled: Bool = false,
65+
inboundType: Inbound.Type = Inbound.self,
66+
outboundType: Outbound.Type = Outbound.self
67+
) {
68+
self.backpressureStrategy = backpressureStrategy
69+
self.isOutboundHalfClosureEnabled = isOutboundHalfClosureEnabled
70+
self.inboundType = inboundType
71+
self.outboundType = outboundType
72+
}
73+
}
74+
3875
/// The underlying channel being wrapped by this ``NIOAsyncChannel``.
3976
@_spi(AsyncChannel)
4077
public let channel: Channel
@@ -52,25 +89,18 @@ public final class NIOAsyncChannel<Inbound: Sendable, Outbound: Sendable>: Senda
5289
///
5390
/// - Parameters:
5491
/// - channel: The ``Channel`` to wrap.
55-
/// - backpressureStrategy: The backpressure strategy of the ``NIOAsyncChannel/inboundStream``.
56-
/// - isOutboundHalfClosureEnabled: If outbound half closure should be enabled. Outbound half closure is triggered once
57-
/// the ``NIOAsyncChannelWriter`` is either finished or deinitialized.
58-
/// - inboundType: The ``NIOAsyncChannel/inboundStream`` message's type.
59-
/// - outboundType: The ``NIOAsyncChannel/outboundWriter`` message's type.
92+
/// - configuration: The ``NIOAsyncChannel``s configuration.
6093
@inlinable
6194
@_spi(AsyncChannel)
6295
public init(
6396
synchronouslyWrapping channel: Channel,
64-
backpressureStrategy: NIOAsyncSequenceProducerBackPressureStrategies.HighLowWatermark? = nil,
65-
isOutboundHalfClosureEnabled: Bool = false,
66-
inboundType: Inbound.Type = Inbound.self,
67-
outboundType: Outbound.Type = Outbound.self
97+
configuration: Configuration = .init()
6898
) throws {
6999
channel.eventLoop.preconditionInEventLoop()
70100
self.channel = channel
71101
(self.inboundStream, self.outboundWriter) = try channel._syncAddAsyncHandlers(
72-
backpressureStrategy: backpressureStrategy,
73-
isOutboundHalfClosureEnabled: isOutboundHalfClosureEnabled
102+
backpressureStrategy: configuration.backpressureStrategy,
103+
isOutboundHalfClosureEnabled: configuration.isOutboundHalfClosureEnabled
74104
)
75105
}
76106

@@ -83,23 +113,18 @@ public final class NIOAsyncChannel<Inbound: Sendable, Outbound: Sendable>: Senda
83113
///
84114
/// - Parameters:
85115
/// - channel: The ``Channel`` to wrap.
86-
/// - backpressureStrategy: The backpressure strategy of the ``NIOAsyncChannel/inboundStream``.
87-
/// - isOutboundHalfClosureEnabled: If outbound half closure should be enabled. Outbound half closure is triggered once
88-
/// the ``NIOAsyncChannelWriter`` is either finished or deinitialized.
89-
/// - inboundType: The ``NIOAsyncChannel/inboundStream`` message's type.
116+
/// - configuration: The ``NIOAsyncChannel``s configuration.
90117
@inlinable
91118
@_spi(AsyncChannel)
92119
public init(
93120
synchronouslyWrapping channel: Channel,
94-
backpressureStrategy: NIOAsyncSequenceProducerBackPressureStrategies.HighLowWatermark? = nil,
95-
isOutboundHalfClosureEnabled: Bool = false,
96-
inboundType: Inbound.Type = Inbound.self
121+
configuration: Configuration
97122
) throws where Outbound == Never {
98123
channel.eventLoop.preconditionInEventLoop()
99124
self.channel = channel
100125
(self.inboundStream, self.outboundWriter) = try channel._syncAddAsyncHandlers(
101-
backpressureStrategy: backpressureStrategy,
102-
isOutboundHalfClosureEnabled: isOutboundHalfClosureEnabled
126+
backpressureStrategy: configuration.backpressureStrategy,
127+
isOutboundHalfClosureEnabled: configuration.isOutboundHalfClosureEnabled
103128
)
104129

105130
self.outboundWriter.finish()
@@ -125,7 +150,7 @@ public final class NIOAsyncChannel<Inbound: Sendable, Outbound: Sendable>: Senda
125150
backpressureStrategy: NIOAsyncSequenceProducerBackPressureStrategies.HighLowWatermark? = nil,
126151
isOutboundHalfClosureEnabled: Bool = false,
127152
channelReadTransformation: @Sendable @escaping (Channel) -> EventLoopFuture<ChannelReadResult>,
128-
postFireChannelReadTransformation: @Sendable @escaping (ChannelReadResult) -> EventLoopFuture<Inbound>
153+
postFireChannelReadTransformation: @Sendable @escaping (ChannelReadResult) -> EventLoopFuture<Inbound>
129154
) throws -> NIOAsyncChannel<Inbound, Outbound> where Outbound == Never {
130155
channel.eventLoop.preconditionInEventLoop()
131156
let (inboundStream, outboundWriter): (NIOAsyncChannelInboundStream<Inbound>, NIOAsyncChannelOutboundWriter<Outbound>) = try channel._syncAddAsyncHandlersWithTransformations(

Sources/NIOCore/AsyncChannel/AsyncChannelInboundStreamChannelHandler.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ internal final class NIOAsyncChannelInboundStreamChannelHandler<InboundIn: Senda
111111
static func makeHandlerWithTransformations(
112112
eventLoop: EventLoop,
113113
closeRatchet: CloseRatchet,
114-
channelReadTransformation:@Sendable @escaping (InboundIn) -> EventLoopFuture<ReadTransformationResult>,
114+
channelReadTransformation: @Sendable @escaping (InboundIn) -> EventLoopFuture<ReadTransformationResult>,
115115
postFireChannelReadTransformation: @Sendable @escaping (ReadTransformationResult) -> EventLoopFuture<ProducerElement>
116116
) -> NIOAsyncChannelInboundStreamChannelHandler where InboundIn == Channel {
117117
return .init(

0 commit comments

Comments
 (0)