Created
February 28, 2016 20:17
-
-
Save DevElCuy/87a52426f7d2dc2b1948 to your computer and use it in GitHub Desktop.
thread-safe UUID generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local uuid = require 'uuid' -- From https://github.com/Tieske/uuid | |
local socket = require 'socket' | |
-- Get a unique seed | |
local seed, fh, err | |
for i = 1, 256 do | |
seed = socket:gettime()*10000 | |
local filepath = ('/tmp/%s.uuid'):format(seed) | |
fh, err = io.open(filepath) | |
if fh then | |
print(('SEED: %s taken. Keep trying...'):format(seed)) | |
else | |
fh, err = io.open(filepath, 'a+') | |
if fh then | |
fh:write '.' | |
fh:close() | |
fh, err = io.open(filepath) | |
if fh then | |
local stamp = fh:read '*all' | |
if stamp == '.' then | |
break | |
else | |
print(('SEED: %s taken. Keep trying...'):format(seed)) | |
end | |
else | |
print(('FATAL: %s. Keep trying...'):format(err)) | |
end | |
else | |
print(('FATAL: %s. Keep trying...'):format(err)) | |
end | |
end | |
-- reset seed | |
seed = nil | |
end | |
-- Validate seed | |
if seed == nil then | |
error 'FATAL: Unable to get a unique seed' | |
end | |
-- Seeds the generator | |
uuid.randomseed(seed) | |
print(("SEED: %s | UUID: %s"):format(seed, uuid())) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment