:proc_lib vs GenServer?

Earlier today, Code Sync posted the recording of @mhanberg’s Code BEAM America 2022 talk: “OTP Process Abstractions with :proc_lib

I enjoyed the talk! After watching it, I went over to the OTP docs to read more about :proc_lib.

This module is used to start processes adhering to the OTP Design Principles. Specifically, the functions in this module are used by the OTP standard behaviors (for example, gen_server and gen_statem) when starting new processes. The functions can also be used to start special processes, user-defined processes that comply to the OTP design principles. For an example, see section sys and proc_lib in OTP Design Principles.

I wondered if any large Elixir projects make direct use of :proc_lib, so I went looking:

So I was left with one question: Mitchell said there are two common ways of creating a “process abstraction”: wrapping a GenServer and using :proc_lib. When would one reach for :proc_lib over a vanilla GenServer (or even just a plain process)? What are the pros/cons of each?

2 Likes

For now, will drop this GitHub Search query: Sign in to GitHub · GitHub.

I think the gist of the answer is that you would use it when you need very fine tuned behavior that differs from a GenServer, or when you want custom debugging/tracing capabilities.

José or some Erlang folks can probably be more specific.

1 Like

My apologies for a direct ping but… @rvirding? Is this an interesting topic for you to comment on?

Yeah I realized my query should have been something like (/:proc_lib\b/ OR /:sys\b/). So, a slightly modified query gives me some good ideas. :smile:

I understand a bit more after reading the “User-Defined Behaviours” section of the docs, though I’m still curious when others prefer to use :proc_lib for their processes!

Also there are many results in GitHub with proc_lib: (colon in the end) :wink:

My way of viewing this is that :proc_lib is a tool which is used for building OTP compliant servers. It is used for building the standard OTP behaviours like :gen_server/GenServer but you can of course use it for building your own OTP compliant processes, called in the OTP docs for special processes.

Why would you want to build your own processes and not just use the standard provided ones? Well maybe you need something which is completely OTP compliant so it fits in and can be run by supervisors but just doesn’t really fit to be built using the the standard behaviours, e.g. maybe you want much better control over message handling than you can get with a GenServer.

So building your own special processes is actually not that difficult. There are 5 rules to follow and the first one is to start it using :proc_lib. You then need to detect and handle system messages in the right way but most of that work is done for you with the :sys module which you call in the right places. Finally you need to terminate the process at the right time in the right way. Quite simple really.

All this is an hour or so in our (Erlang Solutions) Erlang OTP course. :wink: :smile:

BTW the :sys module contains a lot of useful functions for managing OTP behaviours.

14 Likes

The kafka_protocol library (used by brod) uses proc_lib. kafka_protocol/src/kpro_connection.erl at master · kafka4beam/kafka_protocol · GitHub.