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

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
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
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
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
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement