2727-- Time: 18:01
2828--
2929-- Dependencies:
30- -- 1. ngx.var.redis_backend needs to be set
31- -- 2. ngx.var.redis_backend_rw needs to be set
32- -- 2. ngx.var.disable_redis_replica_healthchecks OPTIONAL - it disables redis healthchecks
30+ -- 1. api-gateway-redis upstream needs to be set
31+ -- 2. api-gateway-redis-replica needs to be set
3332--
3433local base = require " api-gateway.validation.base"
3534local redis = require " resty.redis"
@@ -38,39 +37,15 @@ local cjson = require "cjson"
3837local debug_mode = ngx .config .debug
3938
4039-- redis endpoints are assumed to be global per GW node and therefore are read here
41- local redis_RO_backend = " none"
42- local redis_RW_backend = " none"
43- local disable_redis_replica_healthchecks = " none"
40+ local redis_RO_upstream = " api-gateway-redis-replica"
41+ local redis_RW_upstream = " api-gateway-redis"
4442
4543-- class to be used as a base class for all api-gateway validators --
4644local BaseValidator = {}
4745local redisHealthCheck = RedisHealthCheck :new ({
4846 shared_dict = " cachedkeys"
4947})
5048
51- local function get_redis_RO_backend ()
52- if ( redis_RO_backend == " none" ) then
53- redis_RO_backend = ngx .var .redis_backend
54-
55- end
56- return redis_RO_backend
57- end
58-
59- local function get_redis_RW_backend ()
60- if ( redis_RW_backend == " none" ) then
61- redis_RW_backend = ngx .var .redis_backend_rw
62- end
63- return redis_RW_backend
64- end
65-
66- local function get_disable_redis_replica_healthchecks ()
67- if ( disable_redis_replica_healthchecks == " none" ) then
68- disable_redis_replica_healthchecks = ngx .var .disable_redis_replica_healthchecks
69- end
70- return disable_redis_replica_healthchecks
71- end
72-
73-
7449function BaseValidator :new (o )
7550 local o = o or {}
7651 setmetatable (o , self )
@@ -103,26 +78,24 @@ function BaseValidator:setKeyInLocalCache(key, string_value, exptime, dict_name)
10378 end
10479end
10580
106- function BaseValidator :getRedisUpstream ()
107- if (get_disable_redis_replica_healthchecks () == " on" ) then
108- return get_redis_RO_backend (), 6379
109- end
110- local upstream , host , port = redisHealthCheck :getHealthyRedisNodeForRead ()
111- ngx .log (ngx .DEBUG , " REDIS HOST:" .. tostring (host ) .. " :" .. tostring (port ))
81+ function BaseValidator :getRedisUpstream (upstream_name )
82+ local n = upstream_name or redis_RO_upstream
83+ local upstream , host , port = redisHealthCheck :getHealthyRedisNode (n )
84+ ngx .log (ngx .DEBUG , " Obtained Redis Host:" .. tostring (host ) .. " :" .. tostring (port ), " from upstream:" , n )
11285 if (nil ~= host and nil ~= port ) then
11386 return host , port
11487 end
11588
116- ngx .log (ngx .ERR , " Could not find a Redis upstream. Using default upstream: [ " .. get_redis_RO_backend () .. " :6379] " )
117- return get_redis_RO_backend (), 6379
89+ ngx .log (ngx .ERR , " Could not find a Redis upstream." )
90+ return nil , nil
11891end
11992
12093-- retrieves a saved information from the Redis cache --
12194-- the method uses HGET redis command --
12295-- it returns the value of the key, when found in the cache, nil otherwise --
12396function BaseValidator :getKeyFromRedis (key , hash_name )
12497 local redisread = redis :new ()
125- local redis_host , redis_port = self :getRedisUpstream ()
98+ local redis_host , redis_port = self :getRedisUpstream (redis_RO_upstream )
12699 local ok , err = redisread :connect (redis_host , redis_port )
127100 if ok then
128101 local redis_key , selecterror = redisread :hget (key , hash_name )
@@ -132,7 +105,7 @@ function BaseValidator:getKeyFromRedis(key, hash_name)
132105 return redis_key
133106 end
134107 else
135- ngx .log (ngx .WARN , " Failed to read key " .. key .. " from Redis cache:" , redis_host , " :" , redis_port , " . Error:" , err )
108+ ngx .log (ngx .WARN , " Failed to read key " .. tostring ( key ) .. " from Redis cache:[ " , redis_host , " :" , redis_port , " ]. Error:" , err )
136109 end
137110 return nil ;
138111end
142115-- it retuns true if the information is saved in the cache, false otherwise --
143116function BaseValidator :setKeyInRedis (key , hash_name , keyexpires , value )
144117 local rediss = redis :new ()
145- local ok , err = rediss :connect (get_redis_RW_backend (), 6379 )
118+ local redis_host , redis_port = self :getRedisUpstream (redis_RW_upstream )
119+ local ok , err = rediss :connect (redis_host , redis_port )
146120 if ok then
147121 -- ngx.log(ngx.DEBUG, "WRITING IN REDIS JSON OBJ key=" .. key .. "=" .. value .. ",expiring in:" .. (keyexpires - (os.time() * 1000)) )
148122 rediss :init_pipeline ()
@@ -157,7 +131,7 @@ function BaseValidator:setKeyInRedis(key, hash_name, keyexpires, value)
157131 ngx .log (ngx .WARN , " Failed to write the key [" , key , " ] in Redis. Error:" , commit_err )
158132 end
159133 else
160- ngx .log (ngx .WARN , " Failed to save key:" .. key .. " into cache: " , get_redis_RW_backend () , " . Error:" , err )
134+ ngx .log (ngx .WARN , " Failed to save key:" .. tostring ( key ) .. " into cache: [ " , tostring ( redis_host ) .. " : " .. tostring ( redis_port ) , " ]. Error:" , err )
161135 end
162136 return false ;
163137end
0 commit comments