From 2ec755b01bc3ab1238964741719a15c33732885c Mon Sep 17 00:00:00 2001 From: Michal Kozakiewicz Date: Wed, 12 Jul 2023 08:26:59 +0000 Subject: [PATCH 01/12] 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 8f754ee4e18cea5ac2ca010e9809479b52eb4d62 Mon Sep 17 00:00:00 2001 From: Michal Kozakiewicz Date: Wed, 12 Jul 2023 08:29:09 +0000 Subject: [PATCH 02/12] 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 08f460ed4bf45488601bd72e5164c6f82c22bf21 Mon Sep 17 00:00:00 2001 From: Michal Kozakiewicz Date: Wed, 12 Jul 2023 08:36:18 +0000 Subject: [PATCH 03/12] Trigger workflows From e88b801999f72d25248171a4c8817b594f9adfeb Mon Sep 17 00:00:00 2001 From: Michal Kozakiewicz Date: Wed, 12 Jul 2023 08:38:28 +0000 Subject: [PATCH 04/12] 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 7161f0a72c7606b62e5f6d657f3e03c7a5b475c5 Mon Sep 17 00:00:00 2001 From: Michal Kozakiewicz Date: Wed, 12 Jul 2023 08:40:09 +0000 Subject: [PATCH 05/12] 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 66cb48f8b283ef387f129c66081c0a8833ec6e0f Mon Sep 17 00:00:00 2001 From: Michal Kozakiewicz Date: Wed, 12 Jul 2023 08:41:27 +0000 Subject: [PATCH 06/12] Trigger workflows From 1d90a4c8c9b839fb6444f3e17c6ba45da9c61937 Mon Sep 17 00:00:00 2001 From: Michal Kozakiewicz Date: Wed, 12 Jul 2023 08:52:27 +0000 Subject: [PATCH 07/12] 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 8c0f62451abc48ebc32dacf4ef888f459a0319b9 Mon Sep 17 00:00:00 2001 From: Michal Kozakiewicz Date: Wed, 12 Jul 2023 08:53:16 +0000 Subject: [PATCH 08/12] 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 4e587c04ee6af3392f8a4891ce97bdfeeeab20a8 Mon Sep 17 00:00:00 2001 From: Michal Kozakiewicz Date: Wed, 12 Jul 2023 08:55:23 +0000 Subject: [PATCH 09/12] 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 e335ee2e7960d911b4f9f00ddee69adcbed0bce8 Mon Sep 17 00:00:00 2001 From: Michal Kozakiewicz Date: Wed, 12 Jul 2023 09:09:49 +0000 Subject: [PATCH 10/12] 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 f312e06f2417c5ee58a755cab96ab499825896b5 Mon Sep 17 00:00:00 2001 From: Michal Kozakiewicz Date: Wed, 12 Jul 2023 09:56:00 +0000 Subject: [PATCH 11/12] 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 From febf816aa4551cba75453fd8e58825f11bf70c49 Mon Sep 17 00:00:00 2001 From: Michal Kozakiewicz Date: Tue, 12 Nov 2024 12:05:12 +0000 Subject: [PATCH 12/12] Use atomic set and redis pool size --- lib/resty/acme/storage/redis.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/resty/acme/storage/redis.lua b/lib/resty/acme/storage/redis.lua index 9d5736f..9bbd546 100644 --- a/lib/resty/acme/storage/redis.lua +++ b/lib/resty/acme/storage/redis.lua @@ -24,6 +24,7 @@ function _M.new(conf) scan_count = conf.scan_count or 10, username = conf.username, password = conf.password, + pool_size = conf.pool_size or 1, }, mt )