Skip to content

Commit

Permalink
Merge pull request #3 from lojaintegrada/fix/retry-redis-connection
Browse files Browse the repository at this point in the history
fix: retry redis connection
  • Loading branch information
udleinati authored Aug 10, 2021
2 parents ed546c1 + cc1f828 commit 56534ba
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ $ curl -X POST http://kong:8001/apis/{api}/plugins \
| `config.redis_port` | [required] | Port of redis server. |
| `config.redis_cache_time` | [required] | Caches expiration time in seconds. |
| `config.is_required` | false | Define if idempotency is required, when is `false` it is possible to send request without `x-idempotency-id`. |

## Author

David Washington - [GitHub](https://github.com/davidwshg "GitHub")
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package = "kong-plugin-idempotency"
version = "1.0.1-1"
version = "1.0.3-1"

source = {
url = "git://github.com/lojaintegrada/kong-plugin-idempotency",
tag = "1.0.1",
tag = "1.0.3",
}

supported_platforms = {"linux", "macosx"}
Expand Down
23 changes: 19 additions & 4 deletions kong/plugins/idempotency/cache.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
local redis = require 'redis'
local kong = kong
local redis = require 'redis'
local _M = {
client = nil
client = nil,
last_ping_time = 0
}

function ping()
return _M.client:ping()
end

function _M.connection(conf)
if _M.client then
if _M.client and _M.last_ping_time + 5 > os.time() then
return _M.client
end

_M.client = redis.connect(conf.redis_host, conf.redis_port)
if _M.client then
local result = pcall(ping)

if result then
_M.last_ping_time = os.time()
return _M.client
end
end

client = redis.connect(conf.redis_host, conf.redis_port)
_M.client = client

return _M.client
end
Expand Down

0 comments on commit 56534ba

Please sign in to comment.