blisscs

blisscs

Comment on Mint's receive block

As an example for receiving messages return from Mint.HTTP.request/4, in the Mint manual from the official document at https://hexdocs.pm/mint/Mint.HTTP.html#content. it is written -

receive do
  message ->
    case Mint.HTTP.stream(conn, message) do
      :unknown -> handle_normal_message(message)
      {:ok, conn, responses} -> handle_responses(conn, responses)
    end
end

From this sample code, you can see that the receive block pulls off whatever the first message that is in the process’s message queue, and then it checks again the Mint.HTTP.stream/2 to see that the first message is of the right message type that is sent back to this process by issue the HTTP action. If it is not of the wanted type, it discards that message and then it continues to find the message from the remaining messages in the Process Message block queue.

As Erlang processes can receive messages from any other processes, I think that we should use a more specific pattern to match the exact pattern that could be returned from using Mint library, just to preserve the non-Mint’s messages in the front of the process memory block queue.

Some editions to the original message above as pointed out by @jwarlander in the comment below -

the messages that got fetched out of the queue didn’t get discarded, they are been sent to handle_normal_message(message) to handle those messages.

So to rephrase, What I want to point out here is that these messages that could not be handled by this mint’s receive block, should not be fetched out at the time of this receive block is called. And we can use another strict pattern to match what we want instead of matching to anything in front of the queue.

Most Liked

jwarlander

jwarlander

Normally I think you would / should know what messages to expect, eg. those that relate to the purpose of your process. In this specific case, you could most often easily pattern-match any other messages you expect first, then pick it off and check if it’s a Mint message as the final case.

The problem with selective receive is that it can have a performance penalty if your mailbox has a lot of messages in it; also, you usually want a catch-all case somewhere in your main process loop to avoid message overflow.

For more in-depth info, see eg. https://www.erlang-solutions.com/blog/receiving-messages-in-elixir-or-a-few-things-you-need-to-know-in-order-to-avoid-performance-issues.html which is a good overview.

Where Next?

Popular in Chat/Questions Top

William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
maz
I’m getting this error: ## ** (Ecto.Query.CompileError) Tuples can only be used in comparisons with literal tuples of the same size fro...
New
Emily
Preface: I’m not sure if thise is the right place, because this is not direclty Elixir related… but I’ve always got some of the best advi...
New
InkFlo
Hi everyone, This year I’m graduated from Bachelor Degree (in computer science) from France (not really a bachelor, the exact term is “L...
New
ggwc82
Looking to get started with FP and Elixir coming mainly from an OOP Rails and PHP background. My first question is, whats the best cours...
New
Chawki
hi,i’m new to programming world i had learned front-end( javascript,react.js) and i wanna learn a back-end programming language i thought...
New
Besto
I've been trying to start learning Elixir for a couple of weeks while I develop a tiny project I have on node.js, but every time I try to...
New
Fl4m3Ph03n1x
Background After following the communitiy suggestion, I bought the Elixir in Action 2nd Edition book and I am about to finish it now. I ...
New
venomnert
Background I have been a backend elixir developer for about 3 years now. I have been mainly working on simple CRUD applications. Context...
New
shansiddiqui94
Greetings Elixir Developers, My name is Daniel, and I am taking my first step to learn all about the Elixir language. Currently I have a...
New

Other popular topics Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
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
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
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
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
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement