Skip to content

Commit afa1016

Browse files
committed
Expose the child param configurator as well on the server
1 parent 655daa8 commit afa1016

File tree

4 files changed

+66
-16
lines changed

4 files changed

+66
-16
lines changed

Sources/GRPC/ClientConnection.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ extension ClientConnection {
474474
public var debugChannelInitializer: (@Sendable (Channel) -> EventLoopFuture<Void>)?
475475

476476
#if canImport(Network)
477-
/// A closure allowing to customise the `NWParameters` used when establising a connection using NIOTransportServices.
477+
/// A closure allowing to customise the `NWParameters` used when establishing a connection using `NIOTransportServices`.
478478
@available(macOS 10.14, iOS 12.0, watchOS 6.0, tvOS 12.0, *)
479479
public var nwParametersConfigurator: (@Sendable (NWParameters) -> Void)? {
480480
get {

Sources/GRPC/ConnectionPool/GRPCChannelPool.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ extension GRPCChannelPool.Configuration {
322322
return configuration
323323
}
324324

325-
/// A closure allowing to customise the `NWParameters` used when establising a connection using NIOTransportServices.
325+
/// A closure allowing to customise the `NWParameters` used when establishing a connection using `NIOTransportServices`.
326326
@available(macOS 10.14, iOS 12.0, watchOS 6.0, tvOS 12.0, *)
327327
public var nwParametersConfigurator: (@Sendable (NWParameters) -> Void)? {
328328
get {

Sources/GRPC/Server.swift

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,18 @@ public final class Server: @unchecked Sendable {
129129
}
130130

131131
if #available(macOS 10.14, iOS 12.0, watchOS 6.0, tvOS 12.0, *),
132-
let configurator = configuration.nwParametersConfigurator,
132+
let configurator = configuration.listenerNWParametersConfigurator,
133133
let transportServicesBootstrap = bootstrap as? NIOTSListenerBootstrap
134134
{
135135
_ = transportServicesBootstrap.configureNWParameters(configurator)
136136
}
137+
138+
if #available(macOS 10.14, iOS 12.0, watchOS 6.0, tvOS 12.0, *),
139+
let configurator = configuration.childChannelNWParametersConfigurator,
140+
let transportServicesBootstrap = bootstrap as? NIOTSListenerBootstrap
141+
{
142+
_ = transportServicesBootstrap.configureChildNWParameters(configurator)
143+
}
137144
#endif // canImport(Network)
138145

139146
return
@@ -392,18 +399,31 @@ extension Server {
392399
internal var serviceProvidersByName: [Substring: CallHandlerProvider]
393400

394401
#if canImport(Network)
395-
/// A closure allowing to customise the `NWParameters` used when establising a connection using NIOTransportServices.
402+
/// A closure allowing to customise the listener's `NWParameters` used when establishing a connection using `NIOTransportServices`.
403+
@available(macOS 10.14, iOS 12.0, watchOS 6.0, tvOS 12.0, *)
404+
public var listenerNWParametersConfigurator: (@Sendable (NWParameters) -> Void)? {
405+
get {
406+
self._listenerNWParametersConfigurator as! (@Sendable (NWParameters) -> Void)?
407+
}
408+
set {
409+
self._listenerNWParametersConfigurator = newValue
410+
}
411+
}
412+
413+
private var _listenerNWParametersConfigurator: (any Sendable)?
414+
415+
/// A closure allowing to customise the child channels' `NWParameters` used when establishing connections using `NIOTransportServices`.
396416
@available(macOS 10.14, iOS 12.0, watchOS 6.0, tvOS 12.0, *)
397-
public var nwParametersConfigurator: (@Sendable (NWParameters) -> Void)? {
417+
public var childChannelNWParametersConfigurator: (@Sendable (NWParameters) -> Void)? {
398418
get {
399-
self._nwParametersConfigurator as! (@Sendable (NWParameters) -> Void)?
419+
self._childChannelNWParametersConfigurator as! (@Sendable (NWParameters) -> Void)?
400420
}
401421
set {
402-
self._nwParametersConfigurator = newValue
422+
self._childChannelNWParametersConfigurator = newValue
403423
}
404424
}
405425

406-
private var _nwParametersConfigurator: (any Sendable)?
426+
private var _childChannelNWParametersConfigurator: (any Sendable)?
407427
#endif
408428

409429
/// CORS configuration for gRPC-Web support.

Tests/GRPCTests/ServerTests.swift

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,59 @@ import GRPC
1818
import NIOConcurrencyHelpers
1919
import NIOTransportServices
2020
import XCTest
21+
import EchoModel
22+
import EchoImplementation
2123

2224
#if canImport(Network)
2325
import Network
2426
#endif
2527

2628
class ServerTests: GRPCTestCase {
2729
#if canImport(Network)
28-
func testParametersConfigurator() throws {
29-
let counter = NIOLockedValueBox(0)
30-
let serverEventLoopGroup = NIOTSEventLoopGroup()
30+
func testParametersConfigurators() throws {
31+
let listenerCounter = NIOLockedValueBox(0)
32+
let childChannelsCounter = NIOLockedValueBox(0)
33+
let group = NIOTSEventLoopGroup()
34+
defer {
35+
try? group.syncShutdownGracefully()
36+
}
37+
3138
var serverConfiguration = Server.Configuration.default(
3239
target: .hostAndPort("localhost", 0),
33-
eventLoopGroup: serverEventLoopGroup,
40+
eventLoopGroup: group,
3441
serviceProviders: []
3542
)
36-
serverConfiguration.nwParametersConfigurator = { _ in
37-
counter.withLockedValue { $0 += 1 }
43+
serverConfiguration.listenerNWParametersConfigurator = { _ in
44+
listenerCounter.withLockedValue { $0 += 1 }
45+
}
46+
serverConfiguration.childChannelNWParametersConfigurator = { _ in
47+
childChannelsCounter.withLockedValue { $0 += 1 }
3848
}
3949

4050
let server = try Server.start(configuration: serverConfiguration).wait()
41-
XCTAssertEqual(1, counter.withLockedValue({ $0 }))
51+
defer {
52+
try? server.close().wait()
53+
}
54+
55+
// The listener channel should be up and running after starting the server
56+
XCTAssertEqual(1, listenerCounter.withLockedValue({ $0 }))
57+
// However we don't have any child channels set up as there are no active connections
58+
XCTAssertEqual(0, childChannelsCounter.withLockedValue({ $0 }))
59+
60+
// Start a client and execute a request so that a connection is established.
61+
let channel = try GRPCChannelPool.with(
62+
target: .hostAndPort("localhost", server.channel.localAddress!.port!),
63+
transportSecurity: .plaintext,
64+
eventLoopGroup: group
65+
)
66+
defer {
67+
try? channel.close().wait()
68+
}
69+
let echo = Echo_EchoNIOClient(channel: channel)
70+
_ = try echo.get(.with { $0.text = "" }).status.wait()
4271

43-
try? server.close().wait()
72+
// Now the configurator should have run.
73+
XCTAssertEqual(1, childChannelsCounter.withLockedValue({ $0 }))
4474
}
4575
#endif
4676
}

0 commit comments

Comments
 (0)