Skip to content

Commit cf9bd9a

Browse files
alexsong93mhamann
authored andcommitted
Add retry logic to redis queries (#339)
1 parent 993dda2 commit cf9bd9a

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

scripts/lua/lib/redis.lua

+26-13
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ if CACHING_ENABLED then
4848
end
4949

5050

51-
local REDIS_RETRY_COUNT = os.getenv('REDIS_RETRY_COUNT') or 4
51+
local REDIS_RETRY_COUNT = os.getenv('REDIS_RETRY_COUNT')
52+
REDIS_RETRY_COUNT = REDIS_RETRY_COUNT == nil and 3 or tonumber(REDIS_RETRY_COUNT)
5253
local REDIS_FIELD = "resources"
5354

5455
local _M = {}
@@ -656,7 +657,7 @@ function exists(red, key, snapshotId)
656657
if red == nil then
657658
red = _M.init()
658659
end
659-
return red:exists(key), red
660+
return call(red.exists, {red, key}), red
660661
end
661662
end
662663

@@ -677,7 +678,7 @@ function get(red, key)
677678
if red == nil then
678679
red = _M.init()
679680
end
680-
return red:get(key)
681+
return call(red.get, {red, key})
681682
end
682683
end
683684

@@ -711,12 +712,12 @@ function hget(red, key, id)
711712
if red == nil then
712713
red = _M.init()
713714
end
714-
return red:hget(key, id), red
715+
return call(red.hget, {red, key, id}), red
715716
end
716717
end
717718

718719
function hgetall(red, key)
719-
return red:hgetall(key)
720+
return call(red.hgetall, {red, key})
720721
end
721722

722723
function hset(red, key, id, value)
@@ -732,7 +733,7 @@ function hset(red, key, id, value)
732733
c:set(key, val, CACHE_TTL)
733734
end
734735
end
735-
return red:hset(key, id, value)
736+
return call(red.hset, {red, key, id, value})
736737
end
737738

738739
function expire(red, key, ttl)
@@ -744,14 +745,14 @@ function expire(red, key, ttl)
744745
end
745746
c:set(key, value, ttl)
746747
end
747-
return red:expire(key, ttl)
748+
return call(red.expire, {red, ttl})
748749
end
749750

750751
function del(red, key)
751752
if CACHING_ENABLED then
752753
c:delete(key)
753754
end
754-
return red:del(key)
755+
return call(red.del, {red, key})
755756
end
756757

757758
function hdel(red, key, id)
@@ -762,25 +763,37 @@ function hdel(red, key, id)
762763
c:set(key, cachecontents, CACHE_TTL)
763764
end
764765
end
765-
return red:hdel(key, id)
766+
return call(red.hdel, {red, key, id})
766767
end
767768

768769
function set(red, key, value)
769-
return red:set(key, value)
770+
return call(red.set, {red, key, value})
770771
end
771772

772773
function smembers(red, key)
773-
return red:smembers(key)
774+
return call(red.smembers, {red, key})
774775
end
775776

776777
function srem(red, key, id)
777-
return red:srem(key, id)
778+
return call(red.srem, {red, key, id})
778779
end
779780

780781
function sadd(red, key, id)
781-
return red:sadd(key, id)
782+
return call(red.sadd, {red, key, id})
782783
end
783784

785+
--- Call function with retry logic
786+
-- @param func function to call
787+
-- @param args arguments to pass in to function
788+
function call(func, args)
789+
local res, err = func(unpack(args))
790+
local retryCount = REDIS_RETRY_COUNT
791+
while not res and retryCount > 0 do
792+
res, err = func(unpack(args))
793+
retryCount = retryCount - 1
794+
end
795+
return res, err
796+
end
784797

785798
_M.get = get
786799
_M.set = set

0 commit comments

Comments
 (0)