-
Notifications
You must be signed in to change notification settings - Fork 41
Support adding services while ServiceGroup
is running
#199
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
Merged
FranzBusch
merged 6 commits into
swift-server:main
from
sliemeobn:feature/add-services-dynamically
Mar 8, 2025
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b1084d7
basic implementation of a mechanism that adds services to a running g…
sliemeobn 7dce0c3
rename
sliemeobn 84ccf18
shovel added services into task group results
sliemeobn 2513338
renamed to addServiceUnlessShutdown and minor touch-ups
sliemeobn a837a30
improved comment about channel finish
sliemeobn 10ac76e
added missing license header
sliemeobn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import ServiceLifecycle | ||
|
||
actor MockService: Service, CustomStringConvertible { | ||
enum Event { | ||
case run | ||
case runPing | ||
case runCancelled | ||
case shutdownGracefully | ||
} | ||
|
||
let events: AsyncStream<Event> | ||
internal private(set) var hasRun: Bool = false | ||
|
||
private let eventsContinuation: AsyncStream<Event>.Continuation | ||
|
||
private var runContinuation: CheckedContinuation<Void, Error>? | ||
|
||
nonisolated let description: String | ||
|
||
private let pings: AsyncStream<Void> | ||
private nonisolated let pingContinuation: AsyncStream<Void>.Continuation | ||
|
||
init( | ||
description: String | ||
) { | ||
var eventsContinuation: AsyncStream<Event>.Continuation! | ||
self.events = AsyncStream<Event> { eventsContinuation = $0 } | ||
self.eventsContinuation = eventsContinuation! | ||
|
||
var pingContinuation: AsyncStream<Void>.Continuation! | ||
self.pings = AsyncStream<Void> { pingContinuation = $0 } | ||
self.pingContinuation = pingContinuation! | ||
|
||
self.description = description | ||
} | ||
|
||
func run() async throws { | ||
self.hasRun = true | ||
|
||
try await withTaskCancellationHandler { | ||
try await withGracefulShutdownHandler { | ||
try await withThrowingTaskGroup(of: Void.self) { group in | ||
group.addTask { | ||
self.eventsContinuation.yield(.run) | ||
for await _ in self.pings { | ||
self.eventsContinuation.yield(.runPing) | ||
} | ||
} | ||
|
||
try await withCheckedThrowingContinuation { | ||
self.runContinuation = $0 | ||
} | ||
|
||
group.cancelAll() | ||
} | ||
} onGracefulShutdown: { | ||
self.eventsContinuation.yield(.shutdownGracefully) | ||
} | ||
} onCancel: { | ||
self.eventsContinuation.yield(.runCancelled) | ||
} | ||
} | ||
|
||
func resumeRunContinuation(with result: Result<Void, Error>) { | ||
self.runContinuation?.resume(with: result) | ||
} | ||
|
||
nonisolated func sendPing() { | ||
self.pingContinuation.yield() | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apologies for the late review - it'd be great to call out that this can also be used for adding services before the group was started. Right now the comment leaves that ambiguous of what happens.