Tieske / uuid

A pure Lua uuid generator (modified from a Rackspace module)
http://tieske.github.io/uuid/
137 stars 50 forks source link

feat: seedonce function for seeding only once #12

Open ivahaev opened 2 years ago

ivahaev commented 2 years ago

I use uuid library for generating UUIDs, but each second we got the same UUIDs. Unfortunately I can't add uuid.seed() call into init_worker context, so I wrote uuid.seedonce() function which controls if it was called before and seeds only on the first call.

Tieske commented 2 years ago

@ivahaev if using openresty, may I suggest you use https://github.com/thibaultcha/lua-resty-jit-uuid instead?

ivahaev commented 2 years ago

With this function your library works like a charm in OpenResty :).

Tieske commented 2 years ago

timebased randomseeds are bad in general, this lib uses it because it is Lua only. With Openresty there are way better options, like the lib mentioned above they can use crypto grade seeds.

And even then, the state kept in the random generator is application global. So the application is responsible to manage that state. With Openresty it means seeding on init and again on init-worker. Calling this new seedonce function on every call before getting a random number is not efficient.

In Kong we even report attempts to re-seed the random generator, so we can track them down and fix the underlying problem (see https://github.com/Kong/kong/blob/master/kong/globalpatches.lua#L256-L260)

alexdowad commented 2 years ago

@ivahaev if using openresty, may I suggest you use https://github.com/thibaultcha/lua-resty-jit-uuid instead?

Hmm. Just looked at lua-resty-jit-uuid and it uses the exact same seed function as this library.