sasa1977 / elixir-in-action

Code samples for Elixir in Action book
947 stars 199 forks source link

SimpleRegistry links to itself? #26

Closed ooddaa closed 2 years ago

ooddaa commented 2 years ago

Dear Saša,

I was going through the GenServer implementation of SimpleRegistry and am puzzled by this line in your example.

https://github.com/sasa1977/elixir-in-action/blob/4ab14522f28470f8d59cbc5405b981342cb6cac5/code_samples/ch10/process_registry/gen_server.ex#L9

This registers the same SimpleRegistry's process's pid for any given key.

I think the intention was to create a new linked Process, something like

{:ok, pid} = Agent.start_link(fn -> %{} end) 

and register it in the parent SimpleRegistry.

Am I missing anything?

Thanks, Oda

sasa1977 commented 2 years ago

Hey,

This won't register the registry process pid. Note that self() returns the pid of the current process. So if you have e.g. this code:

Agent.start_link(fn -> SimpleRegistry.register(:foo) end)

then under the hood, register will invoke self, which will return the pid of this agent process, so everything should work as expected.

Does that make sense?

ooddaa commented 2 years ago

Thank you for coming back to me!

I see, I was using SimpleRegistry slightly wrong, this makes sense now.

I interpreted the task as if I was to run everything through SimpleRegistry, ie the other way around.