vlad1010

vlad1010

Websocket "race condition"

Hi everyone, sorry for the dumb question. But I am a newbie in elixir came from golang.

I really love elixir and FP style, I try to implement some basic feature and had an issue with race conditions on channel connection. For example, in golang I resolved it with Mutex, but it’s not FP style.

Initial problem:
Several channel connections, waiting for the group of random amount for example 5. And then randomly shuffle people. In this case, I had some problems with “race condition”. Same people to different groups etc.

What is the right idiomatic way to solve this in Elixir?

Thank you for your help. And sorry again for a dumb question.

Marked As Solved

sb8244

sb8244

Author of Real-Time Phoenix

I struggled with the concept of mutexes a lot when I first started out (beam does have them, but not commonly used). I often asked my coworker “but what about race conditions? How does it solve for them?” He said something along the lines of “they didn’t solve them, they just don’t exist.”

That’s a bit tongue in cheek because of course they can still exist. However, GenServer makes it possible to solve many classes of problem that would be an issue and need mutexes in other languages. I’d say a large reason that mutexes are used in other languages just aren’t needed. The reason for this is that a GenServer can process one message at a time.

In this case, I’d probably have a GenServer that knows about all users connected (via some sort of registration mechanism such as a Registry), and then it shuffles and passes a message to each when 5 join.

This becomes harder when distributed because you need all 5 players to be on the same GenServer. This is easiest when they’re on the same Beam Node. There are techniques to solve this distributed, still GenServer without mutexes, but requires a bit more mechanism.

Also, welcome to the Elixir forum!

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
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
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
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
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New

Other popular topics Top

Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
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 Postgres...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
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
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
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

We're in Beta

About us Mission Statement