3 Copyright 2016 ZTE Corporation.
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
9 http://www.apache.org/licenses/LICENSE-2.0
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
18 email: meng.zhaoxing1@zte.com.cn
21 -- put red into the connection pool of size 100,
22 -- with 10 seconds max idle time
23 local function close_redis(red)
27 --release connection to the pool
28 local pool_max_idle_time = 10000
30 local ok, err = red:set_keepalive(pool_max_idle_time, pool_size)
32 ngx.log(ngx.ERR, "set keepalive error:", err)
37 local function rewrite_apijson_url()
38 local apiserver=ngx.var.apiserver
39 if apiserver=="fallback" then
40 ngx.status = ngx.HTTP_NOT_FOUND
43 local apijsonurl=ngx.var.apijsonurl
44 local uri = ngx.re.sub(ngx.var.uri, "^/apijson/([^/]+)(/[Vv][^/]*)?(.*)", apijsonurl.."$3", "o")
48 local function query_apijson_info()
49 local apiserver = ngx.var.apiserver
50 local apiname = ngx.var.apiname
51 local apiversion = ngx.var.apiversion
52 apiversion=string.sub(apiversion,2,string.len(apiversion))
54 -- Check if key exists in local cache
55 local cache = ngx.shared.ceryx
56 local server, flags = cache:get("server:apijson:"..apiname..":"..apiversion)
57 local url, flags = cache:get("url:apijson:"..apiname..":"..apiversion)
58 if server and url then
59 ngx.var.apiserver = server
60 ngx.var.apijsonurl = url
61 ngx.log(ngx.WARN, "==using apijson cache:", server.."&&"..url)
65 local redis = require "resty.redis"
66 local red = redis:new()
67 red:set_timeout(1000) -- 1000 ms
68 local redis_host = "127.0.0.1"
69 local redis_port = 6379
70 local res, err = red:connect(redis_host, redis_port)
72 -- Return if could not connect to Redis
74 ngx.log(ngx.ERR, "connect to redis error:", err)
78 -- Construct Redis key
80 local keyprefix = prefix..":routing:api:"..apiname..":"..apiversion
83 -- Try to get ip:port for apiname
84 local serverkey=keyprefix..":lb:server1"
85 local server, err = red:hget(serverkey,"ip")..":"..red:hget(serverkey,"port")
86 ngx.log(ngx.WARN, "==apijsonserver:", server)
87 if not server or server == ngx.null then
88 return close_redis(red)
91 -- Try to get apijson url for apiname
92 local infokey=keyprefix..":info"
93 local url, err = red:hget(infokey,"apijson")
94 ngx.log(ngx.WARN, "==apijsonurl:", url)
95 if not url or url == ngx.null then
96 return close_redis(red)
99 -- Save found key to local cache for 5 seconds
100 cache:set("server:apijson:"..apiname..":"..apiversion, server, 5)
101 cache:set("url:apijson:"..apiname..":"..apiversion, url, 5)
103 ngx.log(ngx.WARN, "==apijson result:", server.."&&"..url)
104 ngx.var.apiserver = server
105 ngx.var.apijsonurl = url
107 return close_redis(red)
110 rewrite_apijson_url()