Skip to content

Use atomic set #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ on:
- master
- release/*
- test-please/*
- use-atomic-set
pull_request:
paths-ignore:
# ignore top-level markdown files (CHANGELOG.md, README.md, etc.)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ storage_config = {
}
```

Redis >= 2.6.0 is required as this storage requires [PEXPIRE](https://redis.io/commands/pexpire).
Redis >= 2.6.12 is required as this storage requires [SET EX](https://redis.io/commands/set).
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder who uses such antique versions anyway (current is 7.0, 2.6 is not listed in release notes but seems to be from 2015).

Or are there two different versioning schemes?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha, https://gist.github.com/lqez/3944436 11 years ago. Maybe it's just 🦖 old.


### vault

Expand Down
29 changes: 13 additions & 16 deletions lib/resty/acme/storage/redis.lua
Original file line number Diff line number Diff line change
Expand Up @@ -95,35 +95,32 @@ local function remove_namespace(namespace, keys)
end
end

-- TODO: use EX/NX flag if we can determine redis version (>=2.6.12)
function _M:add(k, v, ttl)
k = self.namespace .. k
local ok, err = op(self, 'setnx', k, v)
local ok, err
if ttl then
ok, err = op(self, 'set', k, v, "nx", "px", math.floor(ttl * 1000))
else
ok, err = op(self, 'set', k, v, "nx")
end
if err then
return err
elseif ok == 0 then
elseif ok == ngx.null then
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happened here? I can't find ngx.null in the docs

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used nil at first, but this didn't work. I've noticed that other functions in this module use ngx.null instead.

Copy link
Author

@kozak kozak Jul 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

openresty/lua-resty-redis#90

Lua's nil value in an array-like Lua table can make "holes" in the array, which is troublesome for many things, like with the # operator.
When being returned directly as a single value, we need to distinguish a Lua nil value that indicates a low-level error (like network communication failures or timeout) and a valid redis-land null value.
For unit testing, you can swap your lua or luajit command with the resty command (which is provided by OpenResty under <openresty-prefix>/bin/. See

https://github.com/openresty/resty-cli

for the documentation.

return "exists"
end
if ttl then
local _, err = op(self, 'pexpire', k, math.floor(ttl * 1000))
if err then
return err
end
end
end

function _M:set(k, v, ttl)
k = self.namespace .. k
local _, err = op(self, 'set', k, v)
local err, _
if ttl then
_, err = op(self, 'set', k, v, "px", math.floor(ttl * 1000))
else
_, err = op(self, 'set', k, v)
end
if err then
return err
end
if ttl then
local _, err = op(self, 'pexpire', k, math.floor(ttl * 1000))
if err then
return err
end
end
end

function _M:delete(k)
Expand Down
Loading