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

martosaur
TL;DR Logger.Translator acts as a global filter and swallows structure of some OTP reports, which some logger handlers could benefit from...
New
maxpohlmann
In our application, we have many structs that contain lists of floats and, especially in test, we often use pattern matching on these obj...
New
Asd
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). Si...
New
dimitarvp
To @jonatanklosko and @the-mikedavis: I see that there is a Rust crate at crates.io: Rust Package Registry but it is pointing at https:/...
New
tristan
This is a cross post from the Erlang Forums. ETS table `select_take` - Proposals: Ideas - Erlang Programming Language Forum - Erlang Foru...
New
ffloyd
The Problem Currently, if I define a struct in the following way: defmodule MyStruct do # Both x and y will have the FIXED values unti...
New
Oliver
One common problem we face in constructing lists is that there is (AFAIK) no support for conditionally inserting members into list declar...
New
cevado
IEx is a very powerfull shell and it would be awesome to have all this power integrated inside a code editor. Clojure enables something l...
New
cheerfulstoic
I feel like Elixir is getting big enough and old enough that I’m starting to experience problems with conflicting dependencies. An examp...
New
sezaru
When writing my code, I always find __MODULE__ very useful to use as alias of that module “inner dependencies”, ex: alias __MODULE__.{Im...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

We're in Beta

About us Mission Statement