Skip to content

Commit 695cde5

Browse files
committed
Overhaul service lifecycle for Structured Concurrency
# Motivation Since the release of Swift Concurrency the server ecosystem has adopted `async`/`await` in a lot of APIs making the user experience amazing. Another thing that Swift Concurrency introduced is Structured Concurrency which allows to create a task tree that with parent to child relations. Structured Concurrency has many benefits such as automatic cancellation and task local propagation. The current state of this library predates the introduction of Swift Concurrency and is not inline anymore with how applications are structured that want to leverage Concurrency. # Modification This PR overhauls the implementation of service lifecycle completely. The reason for this is that the current implementation is very focused on how NIO based applications work before the introduction of Concurrency. Furthermore, Structured Concurrency is actually replacing part of the current functionality by providing new scoping mechanisms. The overhauled implementation provides two primitive types. Anew `Service` protocol and a `ServiceRunner`. The former is providing a clear API which services have to conform to so that they can be run by the `ServiceRunner`. An example usage of the types looks like this ```swift actor FooService: Service { func run() async throws {} func shutdownGracefully() async throws {} } actor BarService: Service { func run() async throws {} func shutdownGracefully() async throws {} } let fooService = FooService() let barService = BarService() let runner = ServiceRunner( services: [fooService, barService], configuration: .init(), logger: logger ) try await runner.run() ``` # Result We now have a service lifecycle library that integrates nicely with Structured Concurrency. Its value add is that it solves to complex setup of the task group with the signal handling. Furthermore, it provides a currency type `Service` that can be passed to inject services.
1 parent fbdb075 commit 695cde5

38 files changed

+1665
-4750
lines changed

.spi.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
version: 1
22
builder:
33
configs:
4-
- documentation_targets: [Lifecycle, LifecycleNIOCompat]
4+
- documentation_targets: [ServiceLifecycle, UnixSignals]

.swiftformat

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
# file options
22

3-
--swiftversion 5.0
3+
--swiftversion 5.5
44
--exclude .build
55

66
# format options
77

88
--self insert
99
--patternlet inline
10+
--ranges nospace
1011
--stripunusedargs unnamed-only
1112
--ifdef no-indent
13+
--extensionacl on-declarations
14+
--disable typeSugar # https://github.com/nicklockwood/SwiftFormat/issues/636
15+
--disable andOperator
16+
--disable wrapMultilineStatementBraces
17+
--disable enumNamespaces
18+
--disable redundantExtensionACL
19+
--disable redundantReturn
20+
--disable preferKeyPath
21+
--disable sortedSwitchCases
1222

1323
# rules

Package.swift

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,60 @@
1-
// swift-tools-version:5.6
1+
// swift-tools-version:5.7
22

33
import PackageDescription
44

55
let package = Package(
66
name: "swift-service-lifecycle",
7+
platforms: [
8+
.macOS(.v10_15),
9+
.iOS(.v13),
10+
.watchOS(.v6),
11+
.tvOS(.v13),
12+
],
713
products: [
8-
.library(name: "Lifecycle", targets: ["Lifecycle"]),
9-
.library(name: "LifecycleNIOCompat", targets: ["LifecycleNIOCompat"]),
14+
.library(
15+
name: "ServiceLifecycle",
16+
targets: ["ServiceLifecycle"]
17+
),
18+
.library(
19+
name: "UnixSignals",
20+
targets: ["UnixSignals"]
21+
),
1022
],
1123
dependencies: [
12-
.package(url: "https://github.com/apple/swift-atomics.git", from: "1.0.0"),
13-
.package(url: "https://github.com/apple/swift-log.git", from: "1.0.0"),
14-
.package(url: "https://github.com/apple/swift-metrics.git", "1.0.0" ..< "3.0.0"),
15-
.package(url: "https://github.com/swift-server/swift-backtrace.git", from: "1.1.1"),
16-
.package(url: "https://github.com/apple/swift-nio.git", from: "2.0.0"), // used in tests
17-
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
24+
.package(
25+
url: "https://github.com/apple/swift-log.git",
26+
from: "1.5.2"
27+
),
28+
.package(
29+
url: "https://github.com/apple/swift-docc-plugin",
30+
from: "1.0.0"
31+
),
1832
],
1933
targets: [
20-
.target(name: "Lifecycle", dependencies: [
21-
.product(name: "Atomics", package: "swift-atomics"),
22-
.product(name: "Logging", package: "swift-log"),
23-
.product(name: "Metrics", package: "swift-metrics"),
24-
.product(name: "Backtrace", package: "swift-backtrace"),
25-
]),
26-
27-
.target(name: "LifecycleNIOCompat", dependencies: [
28-
"Lifecycle",
29-
.product(name: "NIO", package: "swift-nio"),
30-
]),
31-
32-
.testTarget(name: "LifecycleTests", dependencies: ["Lifecycle", "LifecycleNIOCompat"]),
34+
.target(
35+
name: "ServiceLifecycle",
36+
dependencies: [
37+
.product(
38+
name: "Logging",
39+
package: "swift-log"
40+
),
41+
.target(name: "UnixSignals"),
42+
]
43+
),
44+
.target(
45+
name: "UnixSignals"
46+
),
47+
.testTarget(
48+
name: "ServiceLifecycleTests",
49+
dependencies: [
50+
.target(name: "ServiceLifecycle"),
51+
]
52+
),
53+
.testTarget(
54+
name: "UnixSignalsTests",
55+
dependencies: [
56+
.target(name: "UnixSignals"),
57+
]
58+
),
3359
]
3460
)

[email protected]

Lines changed: 0 additions & 37 deletions
This file was deleted.

[email protected]

Lines changed: 0 additions & 37 deletions
This file was deleted.

[email protected]

Lines changed: 0 additions & 37 deletions
This file was deleted.

[email protected]

Lines changed: 0 additions & 37 deletions
This file was deleted.

[email protected]

Lines changed: 0 additions & 37 deletions
This file was deleted.

[email protected]

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)