Skip to content

Commit e28f532

Browse files
authored
Make plugin API async (#940)
* Make plugin API async Pave way for #824 * Fix formatting * Make stop blocking and non-throwing
1 parent a3db793 commit e28f532

File tree

4 files changed

+13
-19
lines changed

4 files changed

+13
-19
lines changed

Sources/ActorSingletonPlugin/ActorSingletonPlugin.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,14 @@ extension ActorSingletonPlugin: _Plugin {
7777
Self.pluginKey
7878
}
7979

80-
public func start(_ system: ClusterSystem) -> Result<Void, Error> {
81-
.success(())
82-
}
80+
public func start(_ system: ClusterSystem) async throws {}
8381

84-
// TODO: Future; TODO2: no need for this at all now since we have async await
85-
public func stop(_ system: ClusterSystem) -> Result<Void, Error> {
82+
public func stop(_ system: ClusterSystem) {
8683
self.singletonsLock.withLock {
8784
for (_, singleton) in self.singletons {
8885
singleton.stop(system)
8986
}
9087
}
91-
return .success(())
9288
}
9389
}
9490

Sources/DistributedActors/ClusterSystem.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ public class ClusterSystem: DistributedActorSystem, @unchecked Sendable {
380380
lazyNodeDeathWatcher?.wakeUp()
381381

382382
/// Starts plugins after the system is fully initialized
383-
self.settings.plugins.startAll(self)
383+
await self.settings.plugins.startAll(self)
384384

385385
self.log.info("ClusterSystem [\(self.name)] initialized, listening on: \(self.settings.uniqueBindNode)")
386386
if settings.enabled {

Sources/DistributedActors/Plugins/ActorSystem+Plugins.swift

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@
1717

1818
public protocol _AnyPlugin {
1919
/// Starts the plugin.
20-
// TODO: move to async function
21-
func start(_ system: ClusterSystem) -> Result<Void, Error>
20+
func start(_ system: ClusterSystem) async throws
2221

2322
/// Stops the plugin.
24-
// TODO: move to async function
25-
func stop(_ system: ClusterSystem) -> Result<Void, Error>
23+
func stop(_ system: ClusterSystem)
2624
}
2725

2826
/// A plugin provides specific features and capabilities (e.g., singleton) to a `ClusterSystem`.
@@ -50,11 +48,11 @@ internal struct BoxedPlugin: _AnyPlugin {
5048
return unwrapped
5149
}
5250

53-
func start(_ system: ClusterSystem) -> Result<Void, Error> {
54-
self.underlying.start(system)
51+
func start(_ system: ClusterSystem) async throws {
52+
try await self.underlying.start(system)
5553
}
5654

57-
func stop(_ system: ClusterSystem) -> Result<Void, Error> {
55+
func stop(_ system: ClusterSystem) {
5856
self.underlying.stop(system)
5957
}
6058
}

Sources/DistributedActors/Plugins/ActorSystemSettings+Plugins.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@ public struct PluginsSettings {
4141
}
4242

4343
/// Starts all plugins in the same order as they were added.
44-
internal func startAll(_ system: ClusterSystem) {
44+
internal func startAll(_ system: ClusterSystem) async {
4545
for plugin in self.plugins {
46-
if case .failure(let error) = plugin.start(system) {
46+
do {
47+
try await plugin.start(system)
48+
} catch {
4749
fatalError("Failed to start plugin \(plugin.key)! Error: \(error)")
4850
}
4951
}
@@ -54,9 +56,7 @@ public struct PluginsSettings {
5456
internal func stopAll(_ system: ClusterSystem) {
5557
// Shut down in reversed order so plugins with the fewest dependencies are stopped first!
5658
for plugin in self.plugins.reversed() {
57-
if case .failure(let error) = plugin.stop(system) {
58-
fatalError("Failed to stop plugin \(plugin.key)! Error: \(error)")
59-
}
59+
plugin.stop(system)
6060
}
6161
}
6262

0 commit comments

Comments
 (0)