-
Notifications
You must be signed in to change notification settings - Fork 3
Add support for swift-service-lifecycle #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 28 commits
4c6a241
d9b1859
4f29521
126ca49
799ae5f
d64751b
54aa03b
c07e575
2eb4ec8
4c4b00b
a6e41d2
94ea5d2
ffdc5ca
29958fe
53b0a01
aca69bd
ea5a2da
f6d076a
321af08
2d57b9a
17a0587
05a003e
91298cf
8aa2e2b
e553a8f
9d4d54a
2adab4a
8f76939
8834692
cf189f5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,13 +18,15 @@ public import Logging | |
| import NIOCertificateReloading | ||
| import NIOConcurrencyHelpers | ||
| import NIOCore | ||
| import NIOExtras | ||
| import NIOHTTP1 | ||
| import NIOHTTP2 | ||
| import NIOHTTPTypes | ||
| import NIOHTTPTypesHTTP1 | ||
| import NIOHTTPTypesHTTP2 | ||
| import NIOPosix | ||
| import NIOSSL | ||
| import ServiceLifecycle | ||
| import SwiftASN1 | ||
| import Synchronization | ||
| import X509 | ||
|
|
@@ -85,6 +87,8 @@ public struct NIOHTTPServer: HTTPServer { | |
| let logger: Logger | ||
| private let configuration: NIOHTTPServerConfiguration | ||
|
|
||
| let serverQuiescingHelper: ServerQuiescingHelper | ||
|
|
||
| var listeningAddressState: NIOLockedValueBox<State> | ||
|
|
||
| /// Task-local storage for connection-specific information accessible from request handlers. | ||
|
|
@@ -106,6 +110,8 @@ public struct NIOHTTPServer: HTTPServer { | |
| // TODO: If we allow users to pass in an event loop, use that instead of the singleton MTELG. | ||
| let eventLoopGroup: MultiThreadedEventLoopGroup = .singletonMultiThreadedEventLoopGroup | ||
| self.listeningAddressState = .init(.idle(eventLoopGroup.any().makePromise())) | ||
|
|
||
| self.serverQuiescingHelper = .init(group: eventLoopGroup) | ||
| } | ||
|
|
||
| /// Starts an HTTP server with the specified request handler. | ||
|
|
@@ -149,15 +155,16 @@ public struct NIOHTTPServer: HTTPServer { | |
| public func serve( | ||
| handler: some HTTPServerRequestHandler<RequestConcludingReader, ResponseConcludingWriter> | ||
| ) async throws { | ||
| defer { | ||
| switch self.listeningAddressState.withLockedValue({ $0.close() }) { | ||
| case .failPromise(let promise, let error): | ||
| promise.fail(error) | ||
| case .doNothing: | ||
| () | ||
| } | ||
| try await withTaskCancellationOrGracefulShutdownHandler { | ||
| try await self._serve(handler: handler) | ||
| } onCancelOrGracefulShutdown: { | ||
| self.close() | ||
|
||
| } | ||
| } | ||
|
|
||
| private func _serve( | ||
| handler: some HTTPServerRequestHandler<RequestConcludingReader, ResponseConcludingWriter> | ||
| ) async throws { | ||
| let asyncChannelConfiguration: NIOAsyncChannel<HTTPRequestPart, HTTPResponsePart>.Configuration | ||
| switch self.configuration.backpressureStrategy.backing { | ||
| case .watermark(let low, let high): | ||
|
|
@@ -176,10 +183,6 @@ public struct NIOHTTPServer: HTTPServer { | |
| ) | ||
|
|
||
| case .tls(let certificateChain, let privateKey): | ||
| let http2Config = NIOHTTP2Handler.Configuration( | ||
| httpServerHTTP2Configuration: self.configuration.http2 | ||
| ) | ||
|
|
||
| let certificateChain = try certificateChain.map { try NIOSSLCertificateSource($0) } | ||
| let privateKey = try NIOSSLPrivateKeySource(privateKey) | ||
|
|
||
|
|
@@ -194,14 +197,10 @@ public struct NIOHTTPServer: HTTPServer { | |
| tlsConfiguration: tlsConfiguration, | ||
| handler: handler, | ||
| asyncChannelConfiguration: asyncChannelConfiguration, | ||
| http2Configuration: http2Config | ||
| http2Configuration: self.configuration.http2 | ||
| ) | ||
|
|
||
| case .reloadingTLS(let certificateReloader): | ||
| let http2Config = NIOHTTP2Handler.Configuration( | ||
| httpServerHTTP2Configuration: configuration.http2 | ||
| ) | ||
|
|
||
| var tlsConfiguration: TLSConfiguration = try .makeServerConfiguration( | ||
| certificateReloader: certificateReloader | ||
| ) | ||
|
|
@@ -212,14 +211,10 @@ public struct NIOHTTPServer: HTTPServer { | |
| tlsConfiguration: tlsConfiguration, | ||
| handler: handler, | ||
| asyncChannelConfiguration: asyncChannelConfiguration, | ||
| http2Configuration: http2Config | ||
| http2Configuration: self.configuration.http2 | ||
| ) | ||
|
|
||
| case .mTLS(let certificateChain, let privateKey, let trustRoots, let verificationMode, let verificationCallback): | ||
| let http2Config = NIOHTTP2Handler.Configuration( | ||
| httpServerHTTP2Configuration: configuration.http2 | ||
| ) | ||
|
|
||
| let certificateChain = try certificateChain.map { try NIOSSLCertificateSource($0) } | ||
| let privateKey = try NIOSSLPrivateKeySource(privateKey) | ||
| let nioTrustRoots = try NIOSSLTrustRoots(treatingNilAsSystemTrustRoots: trustRoots) | ||
|
|
@@ -237,15 +232,11 @@ public struct NIOHTTPServer: HTTPServer { | |
| tlsConfiguration: tlsConfiguration, | ||
| handler: handler, | ||
| asyncChannelConfiguration: asyncChannelConfiguration, | ||
| http2Configuration: http2Config, | ||
| http2Configuration: self.configuration.http2, | ||
| verificationCallback: verificationCallback | ||
| ) | ||
|
|
||
| case .reloadingMTLS(let certificateReloader, let trustRoots, let verificationMode, let verificationCallback): | ||
| let http2Config = NIOHTTP2Handler.Configuration( | ||
| httpServerHTTP2Configuration: configuration.http2 | ||
| ) | ||
|
|
||
| let nioTrustRoots = try NIOSSLTrustRoots(treatingNilAsSystemTrustRoots: trustRoots) | ||
|
|
||
| var tlsConfiguration: TLSConfiguration = try .makeServerConfigurationWithMTLS( | ||
|
|
@@ -260,7 +251,7 @@ public struct NIOHTTPServer: HTTPServer { | |
| tlsConfiguration: tlsConfiguration, | ||
| handler: handler, | ||
| asyncChannelConfiguration: asyncChannelConfiguration, | ||
| http2Configuration: http2Config, | ||
| http2Configuration: self.configuration.http2, | ||
| verificationCallback: verificationCallback | ||
| ) | ||
| } | ||
|
|
@@ -345,6 +336,17 @@ public struct NIOHTTPServer: HTTPServer { | |
| throw error | ||
| } | ||
| } | ||
|
|
||
| func close() { | ||
| switch self.listeningAddressState.withLockedValue({ $0.close() }) { | ||
| case .failPromise(let promise, let error): | ||
| promise.fail(error) | ||
| case .doNothing: | ||
| () | ||
| } | ||
|
|
||
| self.serverQuiescingHelper.initiateShutdown(promise: nil) | ||
| } | ||
| } | ||
|
|
||
| @available(macOS 26.2, iOS 26.2, watchOS 26.2, tvOS 26.2, visionOS 26.2, *) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.