Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions actor/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,18 @@ func (c *Context) Parent() *PID {
return nil
}

// Child will return the PID of the child (if any) by the given name/id.
// PID will be nil if it could not find it.
// Child will return the PID of the child (if any) by the given id.
// The id can be a relative path (e.g., "child/1") or a full path (e.g., "parent/1/child/1").
// Returns nil if the child could not be found.
func (c *Context) Child(id string) *PID {
pid, _ := c.children.Get(id)
// First try exact match (backward compatibility)
if pid, ok := c.children.Get(id); ok {
return pid
}

// Then try with parent prefix
fullKey := c.pid.ID + pidSeparator + id
pid, _ := c.children.Get(fullKey)
return pid
}

Expand Down
5 changes: 5 additions & 0 deletions actor/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ func TestSpawnChildPID(t *testing.T) {
case Started:
pid := c.SpawnChildFunc(childfn, "child", WithID("1"))
assert.True(t, expectedPID.Equals(pid))

pid = c.Child("child/1")
assert.NotNil(t, pid)
assert.True(t, expectedPID.Equals(pid))

wg.Done()
case Stopped:
}
Expand Down
35 changes: 35 additions & 0 deletions cluster/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,41 @@ func TestCannotDuplicateActor(t *testing.T) {
c2.Stop()
}

func TestClusterChildLookup(t *testing.T) {
var (
c1Addr = getRandomLocalhostAddr()
c1 = makeCluster(t, c1Addr, "A", "eu-west")
wg = sync.WaitGroup{}
)

wg.Add(1)
c1.engine.SpawnFunc(func(c *actor.Context) {
switch c.Message().(type) {
case actor.Started:
childPID := c.SpawnChildFunc(func(_ *actor.Context) {}, "worker", actor.WithID("1"))

// Test relative path lookup (new behavior)
found := c.Child("worker/1")
assert.NotNil(t, found)
assert.True(t, childPID.Equals(found))

// Test full path lookup (backward compatible)
fullPath := c.PID().ID + "/worker/1"
foundFull := c.Child(fullPath)
assert.NotNil(t, foundFull)
assert.True(t, childPID.Equals(foundFull))

assert.Equal(t, c1Addr, childPID.Address)

wg.Done()
}
}, "parent", actor.WithID("1"))

c1.Start()
wg.Wait()
c1.Stop()
}

func makeCluster(t *testing.T, addr, id, region string) *Cluster {
config := NewConfig().
WithID(id).
Expand Down