Asd

Asd

Process.resolve/1 which resolves any process reference to pid

Process.resolve/1

What it does

Takes any possible name of the process and returns a pid of the process or port (or nil if none found). Similar to Process.whereis, but this one can also handle via tuples (like {:via, Registry, {MyApp.Registry, "agent"}}) and global names (like {:global, term}).

So the spec will be @spec resolve(GenServer.name()) :: pid() | nil

This function will raise if the via-tuple callback fails, :global fails or if the node from {node, name} is unavailable.

In other words

  • pid is returned if there is a pid associated with the name
  • nil is returned when there’s no process associated with the name
  • error is raised if the pid can’t be resolved for some reason

It goes without saying that all these statements are true only at the moment of the Process.resolve/1 call

But wait, what about GenServer.whereis/1?

Process.resolve will be extremely similar to GenServer.whereis but with a single difference, so that it returns pid even when called with {name, node} tuple. Plus Process.resolve will be a less confusing name, more on this later.

Example

# With register
iex> self()
#PID<0.100.0>
iex> Process.register(self(), :iex)
iex> Process.whereis(:iex)
#PID<0.100.0>
iex> Process.resolve(:iex)
#PID<0.100.0>

# With via-tuples
iex> {:ok, _} = Registry.start_link(keys: :unique, name: MyApp.Registry)
iex> name = {:via, Registry, {MyApp.Registry, "agent"}}
iex> Agent.start_link(fn -> 0 end, name: name)
{:ok, #PID<0.123.0>}
iex> Process.resolve(name)
#PID<0.123.0>

Why

  1. This would give a clear indication that this function must be used to resolve any process name to pid. Right now, there are some libraries which were written without via-tuples in mind, but which support atom names and {:global, term} tuples and pids as arguments.
  2. Because it is handy. In many projects I worked in, I wrote this boilerplate and I can see this boilerplate of different quality implemented in libraries.
  3. It is not associated with GenServers like GenServer.whereis. The problem with GenServer.whereis is that the name can resolve to any process or even port, not a genserver, therefore it is confusing

Why resolve?

It indicates that there is some resolution (like some process, sequence of actions) happens and this function may not return and may fail, because via-tuples are basically callbacks and resolution of {node, name} tuple is an rpc call.

Implementation

It is fairly easy to implement and I can do this if this proposal gets a green light


Your comments are welcome! :smiley:

Where Next?

Popular in Proposals: Ideas Top

altdsoy
Hi there :wave:, I hope everyone is doing well… Tl;Dr: Is there a use case to have Heroicons from an actual hex.pm package instead of G...
New
Astolfo
When generating a release with phx.gen.release --docker a debian version is used by default to avoid “DNS issues” on Alpine. It seems li...
New
andreamancuso
Hey folks, This might sound niche, but I think it’s worth bringing up - especially given Phoenix’s reputation for being lightweight, por...
New
Matsa59
Lets start by a story, I work with a team on a pretty big CMS with lots of modules. We split the cms using umbrella apps. So we have comp...
New
aglassman
Problem The cancel_async function is easily overlooked. Since the results of “outdated” tasks are ignored, it’s easy for developers to a...
New
woylie
We are seeing a lot of warning logs like this: navigate event to "https://someurl" failed because you are redirecting across live_sessio...
New
cevado
I was reading the EEP-79, and thinking about the poor record support in Elixir(i’ve tried to discuss about that in the forum before). I s...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36689 110
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42576 114
New

We're in Beta

About us Mission Statement