dokuzbir

dokuzbir

Unique Identifier For Session

I have a slot booking app. What i want is lock a slot 5 minutes for a session/user. I added lock_until, locked_for attributes to slots. A user has id, but what is equivalent thing for a session. Also if there are another approaches can you please share?

Marked As Solved

NobbZ

NobbZ

The trick with such identifiers is, that you can use whatever you like and write it into the session…

I’m not experienced with phoenix, but have you considered using it roughly like this?

def create_token() do
  uuid = UUID.generate_v4() # you probably need to get a UUID package, therefore this is pseudo code
  Phoenix.Token.sign(YourApp.Endpoint, "your secret salt", uuid)
end

def validate_token(token) do
  Phoenix.Token.verify(YourApp.Endpoint, "your secret salt", token, max_age: 5 * 60)
end

You can store and retrieve that Token via Plug.Conn.{fetch,get,put}_session.

Also Liked

kokolegorille

kokolegorille

There is a slot locking mecanism in this video

around 25 minutes… If this is what You are looking for.

Basically You want to lock a slot for a booking application for 5 minutes? Probably because each booking reduces the capacity of your stock, and You don’t want to sell more than what You can provide.

He wants to lock a timeslot for a user to order something, so no other user could bypass this by typing faster. There is not even the need to store lock_until, just spawn a process with locked_for, and a timeout. As soon as the process expire, so does the lock.

Each resource has a capacity, when You want to book, You request an access, that will decrement capacity, in case You don’t fill the booking in 5 minutes, the token expires without being used, and capacity is increased.

Is that what You want to do?

mgwidmann

mgwidmann

I’m not sure what a slot booking app is, could you clarify what you’re trying to do a bit more? Are you trying to ensure a user in a Phoenix application gets logged out every 5 minutes?

Qqwy

Qqwy

TypeCheck Core Team

Instead of using an UUID, you could use make_ref() |> :erlang.term_to_binary() to create a string-representation of an almost surely unique reference (The keyspace is 2^82), whose behaviour is provided to you by the Erlang VM.

Last Post!

NobbZ

NobbZ

One could even sign the token with a :signed_at time.

That time could be written to the database and when looking for available solts, we look for all WHERE blocked_since < five_minutes_ago OR blocked_since IS NULL (well, actually I mean the SQL that comes closest in its semantic to my pseudo-SQL :wink: ).

So no useless lookups or writes to “free” a slot.

Where Next?

Popular in Questions Top

hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
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

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49134 226
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New

We're in Beta

About us Mission Statement