一个为 Swift 分布式集群系统实现的事件溯源框架。
EventStore
协议。这可以是任何类、Actor 等。ClusterJournalPlugin
将存储包装成单例,因此也应添加单例插件(顺序很重要!)。let node = await ClusterSystem("simple-node") {
$0.plugins.install(plugin: ClusterSingletonPlugin())
$0.plugins.install(
plugin: ClusterJournalPlugin { _ in
SomeStore()
}
)
}
EventSourced
。提供 persistenceID
并定义 handleEvent(_:)
函数。distributed actor SomeActor: EventSourced {
// Some custom events for actor
enum Event {
case doSomething
}
// This is important to provide, events are stored per actor using persistenceID
distributed var persistenceID: PersistenceID { "some-actor" }
distributed func doSomething() async throws {
try await self.emit(event: .doSomething)
}
distributed func handleEvent(_ event: Event) {
switch event {
case .doSomething:
// update state
}
}
init(actorSystem: ClusterSystem) {
self.actorSystem = actorSystem
}
}