To generate a random filename I have the following microfunction:
function generateRandomFileName(){
return `tmp_${Date.now()}`;
}
This function runs in nodejs, and it is executed according to other functions as normal... the thing is that in this nodejs server, a few bytes of information are sent by sockets, and then they are written according to a random name , the function is in charge of this generateRandomFileName
, we must bear in mind that once the files are sampled (certain important information is collected from them), they are eliminated.
According to this I got to thinking... And what happens if, due to the exuberant chance, two people make this request at the same time and this request arrives at the socket server exactly at the same time and as a result the names of the file to write are exactly the same?, the result would be that the function that is responsible for collecting the information once written would fail or get confused... since there are two people writing a super light file at the same time...
How can I avoid this possible but destructive coincidence?
There is no way to test this easily, because it's too hard to sync everything for it to happen, so I think I'll have to do it a bit in my head, any idea what I can do to avoid this?, I haven't tried nothing because I don't know what I should do.
I have also thought the following:
If I check that the file doesn't exist first, as there may be a delay in creating the file, the second, or even third user using fileSystem's existsSync function may return them false
i.e. according to them the file doesn't exist yet, for which they create, this would make an automatic override to the file of the other users, the result is that none of them in the end will work the data extraction, something like the following:
const fs = require("fs");
/*
Puede haber un retraso creando el archivo, por lo que puede ser sobreescrito
sin querer aunque 'se compruebe que no exista'.
*/
if(fs.existsSync(outputFile)){
}
Any idea what I can do that will definitely eradicate this problem?
To avoid this, one or more methods can be followed, first of them is to modify the name that you generate for said file so that it is not a time stamp but a UUID, for this you can use the following code
Since it generates the name with random data adding that the more digits there are, the probability of repetition tends to be null (never zero) it is a good option if your data is not critical.
For critical data, what you can generate is a Singleton that generates the names and based on the time and a random uuid, depending on the time you can check if the name of the file already exists, the format would be temp__ and you would save said name in a temporary object.
Doing some research, there are two options, the first was to use:
Which returns the milliseconds elapsed since a certain date, and the second is to use the nanoseconds provided by
process.hrtime
:The second option is quite accurate, but there is still an almost zero probability that something unexpected will happen, so remember the following,
socket.io
your instance on the server has a propertyid
that is unique for each instance,socket
so you can avoid 100% race conditions mixing these two concepts, that's how I came to the following conclusion of what I could do to solve this problem:With this the file name will always be different.