slawlor / ractor

Rust actor framework
MIT License
1.52k stars 72 forks source link

Query the `type_id` of an actor at runtime #276

Closed stuartcarnie closed 3 weeks ago

stuartcarnie commented 4 weeks ago

Is your feature request related to a problem? Please describe.

At runtime, when a supervisor event occurs in handle_supervisor_evt, we would like to perform specific handling depending on the actor type.

Describe the solution you'd like

One solution we be to allow the type_id of an ActorCell to be queried at runtime, similarly to how it is performed by the runtime to verify the type, introduced in #12.

Describe alternatives you've considered

Currently, we store the ID of the actor at creation and then use the ActorCell::get_id to find the match.

slawlor commented 3 weeks ago

I'm not opposed to doing this, but TypeIds are somewhat unstable, especially over networks and between releases, which is why it's a closed functionality within the lib. I have no problem if you want to expose a retrieval call, but imho you'd be better off with the supervisor keeping a collection of ActorIds in it's state, and matching on those. This is what's commonly done, and is guaranteed to be stable across process/network/release boundaries etc.

philjb commented 3 weeks ago

imho you'd be better off with the supervisor keeping a collection of ActorIds in it's state, and matching on those

That's what we're doing now - since the parents generally spawn the children, we can continue to keep track of the supervised actors grouped by type.

@stuartcarnie and I were looking for a short circuit to switch on type instead of searching through collections to determine the type. We're not currently sending actor information over network - we're all intra-process.

stuartcarnie commented 3 weeks ago

Ahh right, clustered actors.

@philjb – clustered actors are quite handy for process introspection. Previously, using Proto.Actor (in Go), I used the clustering feature to create a CLI to connect to the process and query actors directly.

slawlor commented 3 weeks ago

This should be pretty straightforward to add. I don't really think it's that big of a risk, as long as it's clear with a comment. Do you want to put up a PR? I'm happy to review it.

slawlor commented 3 weeks ago

This needs more test coverage before i'll merge it, but the idea is in here. https://github.com/slawlor/ractor/pull/277

Does that work for what you want?

stuartcarnie commented 3 weeks ago

@slawlor those look like nice improvements and the docs are clear regarding the caveats.

stuartcarnie commented 3 weeks ago

Awesome, thanks!

slawlor commented 3 weeks ago

Thanks for the suggestions! It's released in 0.12.4, let me know if it works for your case!