-
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 2 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 |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift HTTP Server open source project | ||
| // | ||
| // Copyright (c) 2026 Apple Inc. and the Swift HTTP Server project authors | ||
| // Licensed under Apache License v2.0 | ||
| // | ||
| // See LICENSE.txt for license information | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| /// A protocol for HTTP servers that support graceful shutdown. | ||
| public protocol GracefulShutdownService { | ||
| /// Initiates graceful shutdown of the HTTP server. | ||
| func beginGracefulShutdown() | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift HTTP Server open source project | ||
| // | ||
| // Copyright (c) 2026 Apple Inc. and the Swift HTTP Server project authors | ||
| // Licensed under Apache License v2.0 | ||
| // | ||
| // See LICENSE.txt for license information | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #if ServiceLifecycle | ||
| public import AsyncStreaming | ||
| public import HTTPTypes | ||
|
|
||
| @available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, visionOS 26.0, *) | ||
| extension HTTPService | ||
| where | ||
| Handler == HTTPServerClosureRequestHandler< | ||
| Server.RequestReader, | ||
| Server.RequestReader.Underlying, | ||
| Server.ResponseWriter, | ||
| Server.ResponseWriter.Underlying | ||
| > | ||
| { | ||
| /// - Parameters: | ||
| /// - server: The underlying HTTPServer instance. | ||
| /// - serverHandler: The request handler closure. | ||
| /// - gracefulShutdownHandler: A closure to execute upon graceful shutdown. | ||
| public init( | ||
| server: Server, | ||
| serverHandler: | ||
| nonisolated(nonsending) @Sendable @escaping ( | ||
| _ request: HTTPRequest, | ||
| _ requestContext: HTTPRequestContext, | ||
| _ requestBodyAndTrailers: consuming sending Server.RequestReader, | ||
| _ responseSender: consuming sending HTTPResponseSender<Server.ResponseWriter> | ||
| ) async throws -> Void, | ||
| gracefulShutdownHandler: @Sendable @escaping () -> Void = {} | ||
| ) { | ||
| self.server = server | ||
| self.serverHandler = HTTPServerClosureRequestHandler(handler: serverHandler) | ||
| self.gracefulShutdownHandler = gracefulShutdownHandler | ||
| } | ||
| } | ||
| #endif // ServiceLifecycle |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift HTTP Server open source project | ||
| // | ||
| // Copyright (c) 2026 Apple Inc. and the Swift HTTP Server project authors | ||
| // Licensed under Apache License v2.0 | ||
| // | ||
| // See LICENSE.txt for license information | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #if ServiceLifecycle | ||
| public import ServiceLifecycle | ||
|
|
||
| /// A wrapper over HTTPServer that integrates with ServiceLifecycle. | ||
| @available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, visionOS 26.0, *) | ||
| public struct HTTPService< | ||
| Server: HTTPServer & GracefulShutdownService, | ||
| Handler: HTTPServerRequestHandler | ||
| >: Service | ||
| where | ||
| Server.RequestReader == Handler.RequestReader, | ||
| Server.ResponseWriter == Handler.ResponseWriter | ||
| { | ||
| let server: Server | ||
| let serverHandler: Handler | ||
| let gracefulShutdownHandler: @Sendable () -> Void | ||
|
|
||
| /// - Parameters: | ||
| /// - server: The underlying HTTPServer instance. | ||
| /// - serverHandler: The request handler that `server` will use. | ||
| /// - onGracefulShutdown: A closure to execute upon graceful shutdown. | ||
| /// | ||
| /// - Note: The `onGracefulShutdown` closure will be called *after* initiating graceful shutdown on `server`. | ||
| public init( | ||
| server: Server, | ||
| serverHandler: Handler, | ||
| onGracefulShutdown gracefulShutdownHandler: @Sendable @escaping () -> Void = {} | ||
|
||
| ) { | ||
| self.server = server | ||
| self.serverHandler = serverHandler | ||
| self.gracefulShutdownHandler = gracefulShutdownHandler | ||
| } | ||
|
|
||
| /// Runs the HTTP server and handles graceful shutdown when signaled. | ||
| public func run() async throws { | ||
| try await withGracefulShutdownHandler( | ||
| operation: { | ||
| try await self.server.serve(handler: self.serverHandler) | ||
| }, | ||
| onGracefulShutdown: { | ||
| self.server.beginGracefulShutdown() | ||
| // Call the user-provided graceful shutdown handler | ||
| self.gracefulShutdownHandler() | ||
| } | ||
| ) | ||
| } | ||
| } | ||
| #endif // ServiceLifecycle | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift HTTP Server open source project | ||
| // | ||
| // Copyright (c) 2026 Apple Inc. and the Swift HTTP Server project authors | ||
| // Licensed under Apache License v2.0 | ||
| // | ||
| // See LICENSE.txt for license information | ||
| // See CONTRIBUTORS.txt for the list of Swift HTTP Server project authors | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #if ServiceLifecycle | ||
| public import HTTPServer | ||
| import HTTPTypes | ||
| import Logging | ||
| import NIOExtras | ||
|
|
||
| @available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, visionOS 26.0, *) | ||
| extension NIOHTTPServer: GracefulShutdownService { | ||
| /// Initiates graceful shutdown of the HTTP server. | ||
| public func beginGracefulShutdown() { | ||
| self.close() | ||
| self.serverQuiescingHelper.initiateShutdown(promise: nil) | ||
| } | ||
| } | ||
| #endif // ServiceLifecycle |
Uh oh!
There was an error while loading. Please reload this page.