dband
GenServer.handle_call/3 Where is the origin of the from arg?
Hello, I am currently working trough the Coding Gnome for Elixir 2. There is an excercise where I am expected to save active clients in a list and remove them when they get disconnected.
There is 1 GenServer that is created by a DynamicSupervisor. The Genserver contains a register_client function, which stores the PID of the from argument into an Agent. When the client disconnects the PID is removed from the Agent in handle_info.
So far that seems to work, but I dont really understand why. I can´t trace where the contents of the fromarg originate. Also I can´t find the PID in the erlang observer. My understanding is that the field is supposed to identify the calling client, but I am stuck here.
Can anyone shed some light?
Marked As Solved
cmo
When you call GenServer.call you’re sending a message of a specific format and the GenServer behaviour handles receiving these messages for you. One part of that format includes the caller’s pid, retrieved with self().
The pid is created around here:
https://github.com/erlang/otp/blob/462bce4222def663359f2704b3f12fc655fbefb2/lib/stdlib/src/gen.erl#L256
And is handled around here:
https://github.com/erlang/otp/blob/462bce4222def663359f2704b3f12fc655fbefb2/lib/stdlib/src/gen_server.erl#L1177
An easy way to dig into the source is to click the </> on hexdocs. If you see an erlang function, e.g. :gen.call, then you’ll need to switch to the erlang source on GitHub.
Also Liked
dband
Got it, thanks. Also additionally, this answer on PID format might help others as well.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









