openresty / lua-nginx-module

Embed the Power of Lua into NGINX HTTP servers
https://openresty.org/
11.3k stars 2.03k forks source link

Improvement suggestion: locking shdict keys #212

Open Thomas12 opened 11 years ago

Thomas12 commented 11 years ago

As you might know: Session management without a locking mechanism can lead to data-corruption as parallel requests will cause race-conditions. Please add this feature:

res=shdict:lock_key(key); res=shdict:unlock_key(key);

And for performance-reasons a combined command for the most obvious tasks:

data, res=shdict:lock_key_and_get(key); res=shdict:set_and_unlock_key(key, data);

and do an auto-unlock for all keys still being locked for that request when the Lua-script ends.

Maybe one could also allow a security-auto-unlock after 30 seconds. You just put a new int into your shdict node-structure and when it is 0 it is unlocked. When it contains a timestamp > 0 it is locked and when now-timestamp>30 then ignore this previous lock when trying to lock it. Locktime-limit can be set as second parameter in lock_key(key, lock_timeout) and lock_key_and_get(key, lock_timeout).

agentzh commented 11 years ago

Hello!

On Mon, Mar 4, 2013 at 10:02 AM, Thomas12 notifications@github.com wrote:

As you might know: Session management without a locking mechanism can lead to data-corruption as parallel requests will cause race-conditions. Please add this feature:

res=shdict:lock_key(key); res=shdict:unlock_key(key);

You can implement a custom lock by using the "add", "delete" methods and ngx.sleep yourself, as discussed here:

http://groups.google.com/group/openresty-en/browse_thread/thread/4c91de9fc25dd2d7

And that's a common practice in the Memcached community. I may add a transaction API to shared dict as in Redis in the future, but I don't want to expose a locking/unlocking API because it's not a good practice for a key-value store IMHO.

Best regards, -agentzh