Divide the MSB source codes into two repos
[msb/apigateway.git] / openresty-ext / src / assembly / resources / openresty / nginx / luaext / loadbalance / peerwatcher.lua
1 --[[\r
2 \r
3     Copyright (C) 2016 ZTE, Inc. and others. All rights reserved. (ZTE)\r
4 \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
8 \r
9             http://www.apache.org/licenses/LICENSE-2.0\r
10 \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
16 \r
17 --]]\r
18 \r
19 local _M = {\r
20         _VERSION = '1.0.0',\r
21         STATUS_OK = 0, STATUS_UNSTABLE = 1, STATUS_ERR = 2\r
22 }\r
23 local msbConf   =  require('conf.msbinit')\r
24 local str_format    = string.format\r
25 local now           = ngx.now\r
26 local fail_timeout = msbConf.server.fail_timeout or 10\r
27 local max_fails = msbConf.server.max_fails  or 1\r
28 \r
29 local cluster_status = {}\r
30 _M.cluster_status = cluster_status\r
31 \r
32 function _M.is_server_ok(skey, srv)\r
33         return _M.get_srv_status(skey, srv)==_M.STATUS_OK\r
34 end\r
35 \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
39                 return _M.STATUS_OK\r
40         end\r
41 \r
42         local srv_key = str_format("%s:%d", srv.ip, srv.port)\r
43         local srv_status = server_status[srv_key]\r
44 \r
45         if srv_status and srv_status.lastmodify + fail_timeout > now() then\r
46                 return srv_status.status\r
47         end\r
48 \r
49         return _M.STATUS_OK\r
50 end\r
51 \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
55                 server_status = {}\r
56                 cluster_status[skey] = server_status\r
57         end\r
58 \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
63                 srv_status = {\r
64                         status = _M.STATUS_OK,\r
65                         failed_count = 0,\r
66                         lastmodify = time_now\r
67                 }\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
73         end\r
74 \r
75         if failed then\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
79                 end\r
80         end\r
81 end\r
82 \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
88                 return \r
89         end\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
97                     need_reset = false\r
98                         break\r
99             end\r
100     end\r
101         if need_reset then\r
102                 ngx.log(ngx.DEBUG, "service:",skey," need reset")\r
103                 cluster_status[skey] = {}\r
104         end\r
105 end\r
106 \r
107 return _M