sodapopcan
Tmp files - what’s wrong with using "phx#{System.unique_integer()"?
In LiveViewTest’s implementation of open_browser, it creates a temp file. To name that, it implements a function that generates a bunch of random bytes which is used to make a filename that looks like: "phx-#{random_id}". The function looks like this:
def random_id do
"phx-"
|> Kernel.<>(random_encoded_bytes())
|> String.replace(["/", "+"], "-")
end
and random_encoded_bytes looks like this:
defp random_encoded_bytes do
binary = <<
System.system_time(:nanosecond)::64,
:erlang.phash2({node(), self()})::16,
:erlang.unique_integer()::16
>>
Base.url_encode64(binary)
end
My question is not about the implementation of these functions but rather: what’s wrong with simply using "phx#{System.unique_integer()"? I’ve thought on it a bunch but can’t come up with an answer.
Thanks!
mods: I wasn’t exactly sure where to put this. I mention LiveView but the question isn’t really about Phoenix but it’s also not even really about Elixir specifically! Apologies if it needs moving.
Marked As Solved
dimitarvp
Absolutely nothing. ![]()
Though to remove that last 0.01% chance of collision I’d do it only slightly differently, like so:
def random_id do
"phx-"
|> Kernel.<>(random_encoded_bytes())
|> String.replace(["/", "+"], "-")
end
defp random_encoded_bytes do
8
|> :crypto.strong_rand_bytes()
|> Base.url_encode64()
end
Or you can use only System.unique_integer but with the :monotonic modifier as well.
Also Liked
smathy
Those of us playing along at home were all waiting for the penny to drop, thanks for the laughs ![]()
al2o3cr
Not sure why it would be useful, but the more-complicated approach has some additional properties:
- leading with
system_timemeans that a temp file that sorts later in lexicographic order was created later - using
phash2means that it’s possible to spot temp files that were made by the same process (though 8-bytes into a base64 value is going to be hard to spot visually)
sodapopcan
![]()
Ya, I forgot to add more of my feelings about this situation
![]()
Hey, two pieces of code you didn’t write yourself are hard to distinguish! That’s a good excuse… right… ![]()
Last Post!
smathy
Oh man, LOL. Of course, I’m laughing because it’s such a familiar experience. Just like the classic “Who the *%&$ wrote this crap… git blame… oh, 6-months ago me
”
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #hex
- #security









