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 Mint.HTTP — Mint v1.9.0. 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.

First Post!

jwarlander

jwarlander

Well, no.. It doesn’t discard the message. It gets passed to the handle_normal_message/1 function, as an example.. This is where you’d implement any regular message handling of non-Mint messages.

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. Elixir Messaging - How To Optimise Performance which is a good overview.

Last Post!

blisscs

blisscs

Thank you @jwarlander

For updating about the drawbacks of using selective receive block also.

But as per document go I think selective receive block should be default on them. I have create an issue on the repository to see how maintainer think of it.

Thanks
Dev.

Where Next?

Popular in Chat/Questions Top

maqbool
what books/Resources do you recommend to learn about distributed system(theory)?
New
pillaiindu
I am a VSCode and Sublime user and I know VIM, though I don’t use VIM directly but whenever I code inside Sublime or VSCode, I use Vim em...
New
Scoty
Hey, I am currently reading Programming Elixir and I am doing one of the exercises where you should write a solution to the “I’m thinkin...
New
Nopp
Hey guys and girls, i am completely “new” to programming, recently played a bit with Python, Ruby and PureBasic, but i want to try somet...
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
phykos
In Ruby, which is very similar to Elixir I do this: def test yield end test do puts("sup there") end Here, the yield keyword will be...
New
armanm
I know zero downtime deployment can mean different things depending on your application and what your users can tolerate so expectations ...
New

Other popular topics Top

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
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
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...
274 42716 114
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement