Darkoyd

Darkoyd

Hello everyone,

I am fairly new to Elixir and so far I’ve had a blast.
As part of an academic assignment I’ve been tasked with implementing a concurrent messaging application using Mutex and Locks.

Before anything, I’ll start by saying that I know this is highly discouraged in Elixir and I am very aware and understand why. Still, for learning’s sake, I must do it with Mutex and Locks in mind.

I am currently using the Mutex Mix module described here. As far as I understand, it’s basically a GenServer with error handling.

My first solution was to follow the documentation on the Mutex Mix module and did everything on top of another GenServer to harbour its utilities to handle state and lock every operation with the Mutex. It works as expected, yet upon asking my TA, they confirmed that was not allowed for this assignment.

Having those two things in mind, so far, I’ve defined a module such that

defmodule MutexChatroom do

def func(state) do
  receive do
    {:join, user} ->
      state = join(state, user)
      func(state)
    {:disconnect, user} ->
      state = disconnect(state, user)
      func(state)
    {:write_message, user, message} ->
      state = write_message(state, user, message)
      func(state)
    {:read_messages, user} ->
      state = read_messages(state, user)
      func(state)
    {:exit} ->
      IO.puts("SERVER: Goodbye!")
  end
end
...
end

the join/2, disconnect/2, write_message/3 and read_messages/2 functions all follow the following structure:

def func(state, x) do
    lock = Mutex.await(ChatLock, state)

        #Some logic checking the state and logging to console.

     Mutex.release(ChatLock, lock)
     state
  end

Inside the main module where I handle user input on the console I am calling on start the following:

    {:ok, pid} = Task.start_link(fn -> MutexChatroom.func(%{users: [], messages: []}) end)

    children = [
      {Mutex, name: ChatLock}
    ]

    {:ok, sup_pid} = Supervisor.start_link(children, strategy: :one_for_one)

and using the send/2 function using pid as a parameter to update the state in the module.

Upon running the code, once send/2 is called I get the following error:

** (EXIT from #PID<0.191.0>) shell process exited with reason: shutdown: failed to start child: Mutex
    ** (EXIT) already started: #PID<0.193.0>

So far I’ve had no luck replicating the behaviour of my previous implementation neither managed to run it completely.

Any help is welcome,
Thanks! :slight_smile:

Showing Posts 1 to 2

al2o3cr

al2o3cr

That error message is from Supervisor.start_link - are you sure execution even makes it to the send?

The message tells you something else has already started a process named ChatLock.

Darkoyd

Darkoyd OP

Hi,

You are right, I found out the error like 20 minutes after posting and was trying to frantically delete the post once I fixed it. I had the Supervisor.start_link line running recursively by accident.

Thanks anyway :slight_smile:

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews