Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add quick Registry example to GenServer docs #14368

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

nathanl
Copy link
Contributor

@nathanl nathanl commented Mar 25, 2025

No description provided.

@nathanl nathanl force-pushed the genserver_registry_example branch from 91438ac to fa3e9e8 Compare March 25, 2025 13:27
Comment on lines +279 to +280
Registry.lookup(:stacks, "stack 1")
#=> [{#PID<0.150.0>, nil}]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nathanl Registry.lookup/2 is a dangerous pitfall (ask me how I know...) because it won't work if you attach metadata to the :via tuple. Maybe we use GenServer.whereis/1 in this example?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@whatyouhide I'm trying to add an example of the advice in the previous paragraph regarding Registry. GenServer.whereis/1 is for global atom names only, right?

I wasn't aware that Registry could store metadata, but it looks like Registry.lookup/2 can fetch it.

metadata = "taters"
{:ok, _} = Registry.register(:stacks, 9, metadata)
Registry.lookup(:stacks, 9)
[{#PID<0.138.0>, "taters"}] # value == "metadata"?
 Registry.lookup(:stacks, :bob) # registered with a :via tuple
[{#PID<0.137.0>, nil}]

I see in the v1.19.0-dev docs that {:via will be able to include metadata soon - is that when lookup/2 becomes a problem? What am I missing?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that's when lookup/2 becomes a problem. I would just register via :via here but use GenServer.whereis instead of Registry.lookup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants