Crowdhailer
The Gen* behaviours in Elixir (and erlang) provide a pure interface.
One of the benefits of this is business logic is easy to test. No need to start processes just pass the arguments you want to test to handle_call for example and assert on one of the results.
I’m sure I read in a book that a pure interface was an important part of the setup, unfortunately I can’t remember which one so if anyone can point me to that again I’d be grateful.
Unfortunately with GenServer implemented as it is several things can’t be done in a pure fashion. For example sending a reply to a call in response to a later message.
It’s a fairly contrived example but I don’t believe this can be implemented in a pure manner.
defmodule PairUp do
use GenServer
def handle_call({:pair, pid}, from, :none) do
{:noreply, {:waiting, pid, from}}
end
def handle_call({:pair, pid2}, from2, {:waiting, pid1, from1}) do
GenServer.reply(from1, {:other, pid2})
{:reply, {:other, pid1}, :none}
end
end
I was thinking with some small changes to the GenServer design purity could be regained.
defmodule PairUp do
use AltServer
def handle_call({:pair, pid}, from, :none) do
{[], {:waiting, pid, from}}
end
def handle_call({:pair, pid2}, from2, {:waiting, pid1, from1}) do
messages = [
{from2, {:other, pid1}},
{from1, {:other, pid2}},
]
{messages, :none}
end
end
The key changes to this interface is that :send/:nosend are replaced by a list of {target, message} pairings, an empty list giving a same behaviour as no send.
I think the structure {[{target, message], state} could be treated as a writer monad. This might even be a helpful model to add type safety to message sending.
This post is really just me musing. my questions are?
- Is this a great idea or a horrible idea
- Does something similar exist already
Trending in Discussions
Other Trending Topics
Chat & Discussions>Discussions
Latest on Elixir Forum
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
- #blog-post
- #ai
- #phoenix_html
- #iex
- #graphql
- #elixirconf-us
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
michalmuskala
Being idealistic, I think this could be an improvement, but on the other hand, being realistic I know there’s just no chance to change GenServer (and probably for a good reason).
Fortunately, what you propose is already implemented in
gen_statemwhich accepts a list of “actions” in return from state functions:Crowdhailer
Completely get this. That said a library implementation could still prove it’s worth.
Thanks for reminding me of
gen_statem. It’s existence is a good point.peerreynders
This design rule was specified in connection to user interfaces but over the years I’ve found it to be a good design rule in general. OO often pushed for more generic solutions - frequently in the name of improved reusability - without necessarily accounting for the increase in complexity or decrease in transparency that sometimes results from trying to account for another 5%(? or less) of use cases.
So while your alternate solution may seem more “generic” most use cases only ever need one or no message - not many.
Your concern can be easily addressed by composing the logic in
handle_callwith pure functions which can be tested separately instead of testinghandle_calldirectly.If you are finding that you need to quite often dispatch multiple replies simply go with something like
Can you elaborate on what this means? In connection to functions “purity” is a well defined concept. Going from your post you seem to be largely concerned with message dispatch that isn’t handled via callback return values.
My typical solution is to implement the “business logic” in an entirely separate module and have the
GenServercallbacks simply invoke those module functions. SoPairUpwould be simplyGenServerinteraction logic (andGenServerrelated helper functions) while somePairUpLogicmodule would contain the actual “pure business functions”. So testing would focus onPairUpLogicwhilePairUpcallbacks would mostly just callPairUpLogicfunctions.Crowdhailer
PairUpLogicThis seams like an abstraction above what I want. In my opinion the module using GenServer is already the business logic module. With Genserver hiding details about sending monitoring etc.That is my main concern I want to have all message dispatch handled by callback return values
mjadczak
I would agree that in most cases, that is sufficient. If there were a very complex business process, however, which may require a lot of complex rules about sending multiple replies or deferring some replies, it could be beneficial to write a separate pure module with business logic, which would provide in a pure, declarative form “instructions” for the GenServer to carry out, facilitating better testing of that complex logic. However, most GenServer implementations will not need that complexity.
Crowdhailer
So I have written an implementation that allows an arbitrary number or messages to be sent in response to a received message. Those messages can also be sent to new processes and not just part of a GenServer reply.
The README has the long term plan explaining why try so hard to make all my message passing explicit.
mjadczak
I’m unconvinced by what you’re aiming to do with this library. You say in the docs that
which isn’t really accurate—you can’t ever send more than one reply, since a reply implies that someone is waiting for it—a
GenServer.callwill block until a reply is received. If another message is sent apart from the reply, it’s either just another asynchronous message to be handled in some way, or if it’s also structured like a GenServer reply, it will sit in a process mailbox forever sincecallattaches a unique reference to its message so that it can recognise the reply.This is also the reason that
GenServerexplicitly distinguishes between calling and casting—a call will block until a reply is received, and a cast cannot be replied to.So, when you say that your module makes it possible to send multiple ‘replies’, it doesn’t. It likely allows you to send multiple outbound messages based on one incoming one, but so does a normal GenServer. ‘Unifying’ all the handler functions into one will likely mean that any potential users would likely recreate things like synchronous casting and asynchronous calling themselves, likely in a much less robust way than the built-in GenServer functions.
Perhaps I’m misunderstanding your project, and if so, correct me, but I just don’t see what you’re trying to achieve. You do mention that you’d like to
and
but I don’t see how this project helps you towards this. Firstly, you can actually inspect all messages flowing through a system using built-in BEAM functions—just because the user does not explicitly send messages, does not mean they are not ‘explicit’ if your goal is system analysis. Secondly, surely you don’t expect the entire system to only ever run these
Comms.Actors? Even if you were to convince a user to only use them to build out functionality, many libraries will be using regular BEAM functionality to achieve their goals, and so even if you did manage to have some run-time model and insight into all the actors running, the messages between them and any potential deadlock or race-condition scenarios within them, you could not say the same for interactions between the user system and the libraries it will inevitably use.Crowdhailer
In my original
PairUpexample I send zero replies in response to the first call and two in response to the second call.I should have been more explicit. I want to reason about messages at compile time.
mjadczak
Ok yeah, I see what you’re doing. You’re moving the async calls to sending messages and replying to pending calls into an actual function return as opposed to doing it ad-hoc.
The problem still remains of how your code interacts with other code if you want to globally reason about messaging. I think only a small amount of user code ever uses more complex or advanced configuration of
GenServers to the point where trying to reason about deadlock or race conditions wholly within your own system would be fruitful, but best of luck with your project.To clarify as well, when I said that if I had to use multiple asynchronous replies etc. in my codebase, splitting up the code into pure business logic and a GenServer to interpret, I would have my business logic reply in some business-logic specific way and let the GenServer translate that into whatever complexity is needed, whereas here you seem to be pushing towards enforcing a GenServer-like-yet-not-standard convention on the user’s business logic itself. If you do manage to get some static reasoning about messages together I would be very interested in looking at that code, but I still remain unconvinced of how useful it would be for actual development.
OvermindDL1
Just as an aside, if you think of the BEAM Actors and Message Passing as just a normal Algebraic Effect (which it is), then it is functionally pure.