Skip to content

Instantly share code, notes, and snippets.

@develCuy
Created February 28, 2016 20:17
Show Gist options
  • Save develCuy/87a52426f7d2dc2b1948 to your computer and use it in GitHub Desktop.
Save develCuy/87a52426f7d2dc2b1948 to your computer and use it in GitHub Desktop.
thread-safe UUID generator
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