3 Copyright (C) 2016 ZTE, Inc. and others. All rights reserved. (ZTE)
\r
5 Licensed under the Apache License, Version 2.0 (the "License");
\r
6 you may not use this file except in compliance with the License.
\r
7 You may obtain a copy of the License at
\r
9 http://www.apache.org/licenses/LICENSE-2.0
\r
11 Unless required by applicable law or agreed to in writing, software
\r
12 distributed under the License is distributed on an "AS IS" BASIS,
\r
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
14 See the License for the specific language governing permissions and
\r
15 limitations under the License.
\r
21 STATUS_OK = 0, STATUS_UNSTABLE = 1, STATUS_ERR = 2
\r
23 local msbConf = require('conf.msbinit')
\r
24 local str_format = string.format
\r
26 local fail_timeout = msbConf.server.fail_timeout or 10
\r
27 local max_fails = msbConf.server.max_fails or 1
\r
29 local cluster_status = {}
\r
30 _M.cluster_status = cluster_status
\r
32 function _M.is_server_ok(skey, srv)
\r
33 return _M.get_srv_status(skey, srv)==_M.STATUS_OK
\r
36 function _M.get_srv_status(skey, srv)
\r
37 local server_status = cluster_status[skey]
\r
38 if not server_status then
\r
42 local srv_key = str_format("%s:%d", srv.ip, srv.port)
\r
43 local srv_status = server_status[srv_key]
\r
45 if srv_status and srv_status.lastmodify + fail_timeout > now() then
\r
46 return srv_status.status
\r
52 function _M.set_srv_status(skey, srv, failed)
\r
53 local server_status = cluster_status[skey]
\r
54 if not server_status then
\r
56 cluster_status[skey] = server_status
\r
59 local time_now = now()
\r
60 local srv_key = str_format("%s:%d", srv.ip, srv.port)
\r
61 local srv_status = server_status[srv_key]
\r
62 if not srv_status then -- first set
\r
64 status = _M.STATUS_OK,
\r
66 lastmodify = time_now
\r
68 server_status[srv_key] = srv_status
\r
69 elseif srv_status.lastmodify + fail_timeout < time_now then -- srv_status expired
\r
70 srv_status.status = _M.STATUS_OK
\r
71 srv_status.failed_count = 0
\r
72 srv_status.lastmodify = time_now
\r
76 srv_status.failed_count = srv_status.failed_count + 1
\r
77 if srv_status.failed_count >= max_fails then
\r
78 srv_status.status = _M.STATUS_ERR
\r
83 function _M.check_and_reset_srv_status_ifneed(skey,servers)
\r
84 local server_status = cluster_status[skey]
\r
85 --if disabled servers of the service is empty,do nothing
\r
86 if not server_status then
\r
87 ngx.log(ngx.DEBUG, "service:",skey," server_status is nil")
\r
90 local need_reset = true
\r
91 for _, srv in ipairs(servers) do
\r
92 local srv_key = str_format("%s:%d", srv.ip, srv.port)
\r
93 local srv_status = server_status[srv_key]
\r
94 if not (srv_status and srv_status.status == _M.STATUS_ERR and srv_status.lastmodify + fail_timeout > now()) then
\r
95 --once find the server is not disabled now, no need to reset the status table. break the loop
\r
96 ngx.log(ngx.DEBUG, "service:",skey," donot need reset,break the loop")
\r
102 ngx.log(ngx.DEBUG, "service:",skey," need reset")
\r
103 cluster_status[skey] = {}
\r