From 9d2ce71ff76ad43244aaca7e6ecc835558b0366b Mon Sep 17 00:00:00 2001 From: anthdm Date: Fri, 15 Dec 2023 09:38:30 +0100 Subject: [PATCH] deactivation --- cluster/agent.go | 29 ++++++++++++++++++++++++++--- examples/cluster/main.go | 4 ++-- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/cluster/agent.go b/cluster/agent.go index ee9026a..d8f8ee1 100644 --- a/cluster/agent.go +++ b/cluster/agent.go @@ -67,8 +67,9 @@ func (a *Agent) Receive(c *actor.Context) { cid := a.activate(msg.kind, msg.id) c.Respond(cid) case deactivate: - // TODO: - a.bcast(&Deactivation{}) + a.bcast(&Deactivation{CID: msg.cid}) + case *Deactivation: + a.handleDeactivation(msg) case *ActivationRequest: resp := a.handleActivationRequest(msg) c.Respond(resp) @@ -91,6 +92,28 @@ func (a *Agent) handleActorTopology(msg *ActorTopology) { } } +func (a *Agent) handleDeactivation(msg *Deactivation) { + // Remove from our activeKinds + kinds, ok := a.activeKinds.kinds[msg.CID.Kind] + if !ok { + // we dont have this kind somehow? + return + } + // This seems kinda a hacky way to do it. But it works. + theKind := ActiveKind{} + kinds.Each(func(k ActiveKind) bool { + if k.cid.Equals(msg.CID) { + theKind = k + return false + } + return true + }) + if theKind.isLocal { + a.cluster.engine.Poison(msg.CID.PID) + } + kinds.Remove(theKind) +} + // A new kind is activated on this cluster. func (a *Agent) handleActivation(msg *Activation) { a.addActiveKind(msg.CID) @@ -207,7 +230,7 @@ func (a *Agent) bcast(msg any) { func (a *Agent) addActiveKind(cid *CID) { akind := ActiveKind{ cid: cid, - isLocal: false, + isLocal: cid.PID.Address == a.cluster.engine.Address(), } if !a.activeKinds.Has(akind) { a.activeKinds.Add(akind) diff --git a/examples/cluster/main.go b/examples/cluster/main.go index 8d8154a..b4a49cf 100644 --- a/examples/cluster/main.go +++ b/examples/cluster/main.go @@ -67,7 +67,7 @@ func main() { time.Sleep(time.Second) - pid := c2.Activate("player", "supermario") + cid := c2.Activate("player", "supermario") time.Sleep(time.Second) c3 := makeCluster("localhost:3003", "C", member) @@ -75,7 +75,7 @@ func main() { time.Sleep(time.Second * 2) c3.Spawn(NewInventory, "foobar", "inv") - c3.Engine().Send(pid, pid) + c3.Engine().Send(cid.PID, cid.PID) time.Sleep(time.Second)