From 7feffccbc9a4e60217d281a5792a9fb266a7abfd Mon Sep 17 00:00:00 2001 From: Michal Kozakiewicz Date: Wed, 12 Jul 2023 08:26:59 +0000 Subject: [PATCH 01/11] Use atomic set --- lib/resty/acme/storage/redis.lua | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/lib/resty/acme/storage/redis.lua b/lib/resty/acme/storage/redis.lua index c9daf2b..db0617f 100644 --- a/lib/resty/acme/storage/redis.lua +++ b/lib/resty/acme/storage/redis.lua @@ -95,35 +95,34 @@ 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 ms + if ttl then + ms = math.floor(ttl * 1000) + else + ms = nil + end + local ok, err = op(self, 'set', k, v, "nx", ms) if err then return err elseif ok == 0 then 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 ms + if ttl then + ms = math.floor(ttl * 1000) + else + ms = nil + end + local _, err = op(self, 'set', k, v, "ex", ttl) 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) From 5f4dd874c69fb3b83102f689b2537498349080fb Mon Sep 17 00:00:00 2001 From: Michal Kozakiewicz Date: Wed, 12 Jul 2023 08:29:09 +0000 Subject: [PATCH 02/11] set nx returns nil instead of 0 --- lib/resty/acme/storage/redis.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/resty/acme/storage/redis.lua b/lib/resty/acme/storage/redis.lua index db0617f..5c2c086 100644 --- a/lib/resty/acme/storage/redis.lua +++ b/lib/resty/acme/storage/redis.lua @@ -103,10 +103,10 @@ function _M:add(k, v, ttl) else ms = nil end - local ok, err = op(self, 'set', k, v, "nx", ms) + local ok, err = op(self, 'set', k, v, 'nx', ms) if err then return err - elseif ok == 0 then + elseif ok == nil then return "exists" end end @@ -119,7 +119,7 @@ function _M:set(k, v, ttl) else ms = nil end - local _, err = op(self, 'set', k, v, "ex", ttl) + local _, err = op(self, 'set', k, v, 'ex', ttl) if err then return err end From 0bf946bfc0997314ed0f60dfff01c7580ff46263 Mon Sep 17 00:00:00 2001 From: Michal Kozakiewicz Date: Wed, 12 Jul 2023 08:36:18 +0000 Subject: [PATCH 03/11] Trigger workflows From 179c8ba355b8bcaf071e27140b83bc20d467f16f Mon Sep 17 00:00:00 2001 From: Michal Kozakiewicz Date: Wed, 12 Jul 2023 08:38:28 +0000 Subject: [PATCH 04/11] Fix error --- lib/resty/acme/storage/redis.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/resty/acme/storage/redis.lua b/lib/resty/acme/storage/redis.lua index 5c2c086..70d9f15 100644 --- a/lib/resty/acme/storage/redis.lua +++ b/lib/resty/acme/storage/redis.lua @@ -119,7 +119,7 @@ function _M:set(k, v, ttl) else ms = nil end - local _, err = op(self, 'set', k, v, 'ex', ttl) + local _, err = op(self, 'set', k, v, 'ex', ms) if err then return err end From 2f9c0852bd9c36014f64a5c443d91669f3140e09 Mon Sep 17 00:00:00 2001 From: Michal Kozakiewicz Date: Wed, 12 Jul 2023 08:40:09 +0000 Subject: [PATCH 05/11] Trigger tests for this branch --- .github/workflows/tests.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index f76ae66..8a08a49 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -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.) From 5e1f1a5746b698c8d1b81ed713c1deece788151c Mon Sep 17 00:00:00 2001 From: Michal Kozakiewicz Date: Wed, 12 Jul 2023 08:41:27 +0000 Subject: [PATCH 06/11] Trigger workflows From 5a654e5f2f79e265164a73d98efda85f72fc7d86 Mon Sep 17 00:00:00 2001 From: Michal Kozakiewicz Date: Wed, 12 Jul 2023 08:52:27 +0000 Subject: [PATCH 07/11] Fix tests --- README.md | 2 +- lib/resty/acme/storage/redis.lua | 16 +++++++--------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 8d193c5..9af9b67 100644 --- a/README.md +++ b/README.md @@ -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). ### vault diff --git a/lib/resty/acme/storage/redis.lua b/lib/resty/acme/storage/redis.lua index 70d9f15..f9ec53f 100644 --- a/lib/resty/acme/storage/redis.lua +++ b/lib/resty/acme/storage/redis.lua @@ -97,29 +97,27 @@ end function _M:add(k, v, ttl) k = self.namespace .. k - local ms + local ok, err if ttl then - ms = math.floor(ttl * 1000) + ok, err = op(self, 'set', k, v, "nx", "px", math.floor(ttl * 1000)) else - ms = nil + ok, err = op(self, 'set', k, v, "nx") end - local ok, err = op(self, 'set', k, v, 'nx', ms) if err then return err - elseif ok == nil then + elseif ok == 0 then return "exists" end end function _M:set(k, v, ttl) k = self.namespace .. k - local ms + local err if ttl then - ms = math.floor(ttl * 1000) + _, err = op(self, 'set', k, v, "ex", "px", math.floor(ttl * 1000)) else - ms = nil + _, err = op(self, 'set', k, v, "ex") end - local _, err = op(self, 'set', k, v, 'ex', ms) if err then return err end From d84a8ac83f5d7b704f429a3f1455dd1fa809af00 Mon Sep 17 00:00:00 2001 From: Michal Kozakiewicz Date: Wed, 12 Jul 2023 08:53:16 +0000 Subject: [PATCH 08/11] Fix tests --- lib/resty/acme/storage/redis.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/resty/acme/storage/redis.lua b/lib/resty/acme/storage/redis.lua index f9ec53f..5a37edc 100644 --- a/lib/resty/acme/storage/redis.lua +++ b/lib/resty/acme/storage/redis.lua @@ -105,7 +105,7 @@ function _M:add(k, v, ttl) end if err then return err - elseif ok == 0 then + elseif ok == nil then return "exists" end end From 62c5f19c6c4a88de53622ea55c20cddbc0e836bf Mon Sep 17 00:00:00 2001 From: Michal Kozakiewicz Date: Wed, 12 Jul 2023 08:55:23 +0000 Subject: [PATCH 09/11] Fix lint --- lib/resty/acme/storage/redis.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/resty/acme/storage/redis.lua b/lib/resty/acme/storage/redis.lua index 5a37edc..4b1adb9 100644 --- a/lib/resty/acme/storage/redis.lua +++ b/lib/resty/acme/storage/redis.lua @@ -112,7 +112,7 @@ end function _M:set(k, v, ttl) k = self.namespace .. k - local err + local err, _ if ttl then _, err = op(self, 'set', k, v, "ex", "px", math.floor(ttl * 1000)) else From 85a88463a22a94bd3bcf5a0586f48297a0dd4a38 Mon Sep 17 00:00:00 2001 From: Michal Kozakiewicz Date: Wed, 12 Jul 2023 09:09:49 +0000 Subject: [PATCH 10/11] Ex and px --- lib/resty/acme/storage/redis.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/resty/acme/storage/redis.lua b/lib/resty/acme/storage/redis.lua index 4b1adb9..353d173 100644 --- a/lib/resty/acme/storage/redis.lua +++ b/lib/resty/acme/storage/redis.lua @@ -114,9 +114,9 @@ function _M:set(k, v, ttl) k = self.namespace .. k local err, _ if ttl then - _, err = op(self, 'set', k, v, "ex", "px", math.floor(ttl * 1000)) + _, err = op(self, 'set', k, v, "px", math.floor(ttl * 1000)) else - _, err = op(self, 'set', k, v, "ex") + _, err = op(self, 'set', k, v) end if err then return err From 353e371219df2321f195ab54e62912f36ceba52c Mon Sep 17 00:00:00 2001 From: Michal Kozakiewicz Date: Wed, 12 Jul 2023 09:56:00 +0000 Subject: [PATCH 11/11] Try to use ngx.null --- lib/resty/acme/storage/redis.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/resty/acme/storage/redis.lua b/lib/resty/acme/storage/redis.lua index 353d173..9d5736f 100644 --- a/lib/resty/acme/storage/redis.lua +++ b/lib/resty/acme/storage/redis.lua @@ -105,7 +105,7 @@ function _M:add(k, v, ttl) end if err then return err - elseif ok == nil then + elseif ok == ngx.null then return "exists" end end