Skip to content

Commit

Permalink
deactivation
Browse files Browse the repository at this point in the history
  • Loading branch information
anthdm committed Dec 15, 2023
1 parent 14aa37f commit 9d2ce71
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
29 changes: 26 additions & 3 deletions cluster/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions examples/cluster/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ 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)
c3.Start()

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)

Expand Down

0 comments on commit 9d2ce71

Please sign in to comment.