JeancarloBarrios

JeancarloBarrios

How to handle sideffects on GenServer or in General

I’m just wondering if there is a specific pattern I have to follow to handle side effects of any type in a GenServer or any OTP Behavior like API calls, database calls, etc. Or even if I should keep the functions pure or not?

Marked As Solved

ityonemo

ityonemo

I find this talk to be enlightening:

https://www.destroyallsoftware.com/talks/boundaries

However, there is no easy answer, and I think this is ultimately one factor that separates a seasoned senior developer from the rest.

I have a few rules that I personally use:. I group API functions together usually near the top. Calls are short, and directly below I have *_impl equivalents which occur inside the gen_server. Internal tasks run by the server loop are named do_*. I put almost nothing in the handle_* functions, and label them as “router” in a comment, analogously to a phoenix or rails router. Usually the handlers I hide away, and I make the impls they call consistent so you never have to look at that section.

Also Liked

ityonemo

ityonemo

here is an example:

bootstrapping boilerplate grouped at the top,

API section with comment fence in the middle, each function grouped with its implementation
Some indirect API functions that are not "do_"s because they are called externally when the system sends a message.

(no do_'s in this particular module), that’s because I forgot to label “start_server” as “do_start_server”.

router on the bottom. Ideally, you should rarely if ever have to examine the router code once written.

Also, if I have a server (genserver or state machine) that has very complex state requirements (aka, not a single scalar or simple list) I prefer to have the internal kept datastructure a struct, and usually I declare typing info on it too.

Last rule is, if you don’t really need state, don’t use it. I only use genservers and gen_statems/state_server when I absolutely have to model the state of something IRL, or some sort of stateful communication protocol. For everything else there’s Tasks, (or it’s linear, functional, stateless code running in “someone else’s” process, like a liveview for example).

Where Next?

Popular in Questions Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
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
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
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...
273 38985 115
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement