|
| 1 | +-- |
| 2 | +-- Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +-- contributor license agreements. See the NOTICE file distributed with |
| 4 | +-- this work for additional information regarding copyright ownership. |
| 5 | +-- The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +-- (the "License"); you may not use this file except in compliance with |
| 7 | +-- the License. You may obtain a copy of the License at |
| 8 | +-- |
| 9 | +-- http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +-- |
| 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. |
| 16 | +-- |
| 17 | +local redis = require("apisix.utils.redis") |
| 18 | +local core = require("apisix.core") |
| 19 | +local util = require("apisix.plugins.limit-conn.util") |
| 20 | +local ngx_timer_at = ngx.timer.at |
| 21 | + |
| 22 | +local setmetatable = setmetatable |
| 23 | + |
| 24 | + |
| 25 | +local _M = {version = 0.1} |
| 26 | + |
| 27 | + |
| 28 | +local mt = { |
| 29 | + __index = _M |
| 30 | +} |
| 31 | + |
| 32 | +function _M.new(plugin_name, conf, max, burst, default_conn_delay) |
| 33 | + |
| 34 | + local self = { |
| 35 | + conf = conf, |
| 36 | + plugin_name = plugin_name, |
| 37 | + burst = burst, |
| 38 | + max = max + 0, -- just to ensure the param is good |
| 39 | + unit_delay = default_conn_delay, |
| 40 | + } |
| 41 | + return setmetatable(self, mt) |
| 42 | +end |
| 43 | + |
| 44 | + |
| 45 | +function _M.incoming(self, key, commit) |
| 46 | + local conf = self.conf |
| 47 | + local red, err = redis.new(conf) |
| 48 | + if not red then |
| 49 | + return red, err |
| 50 | + end |
| 51 | + return util.incoming(self, red, key, commit) |
| 52 | +end |
| 53 | + |
| 54 | + |
| 55 | +function _M.is_committed(self) |
| 56 | + return self.committed |
| 57 | +end |
| 58 | + |
| 59 | + |
| 60 | +local function leaving_thread(premature, self, key, req_latency) |
| 61 | + |
| 62 | + local conf = self.conf |
| 63 | + local red, err = redis.new(conf) |
| 64 | + if not red then |
| 65 | + return red, err |
| 66 | + end |
| 67 | + return util.leaving(self, red, key, req_latency) |
| 68 | +end |
| 69 | + |
| 70 | + |
| 71 | +function _M.leaving(self, key, req_latency) |
| 72 | + -- log_by_lua can't use cosocket |
| 73 | + local ok, err = ngx_timer_at(0, leaving_thread, self, key, req_latency) |
| 74 | + if not ok then |
| 75 | + core.log.error("failed to create timer: ", err) |
| 76 | + return nil, err |
| 77 | + end |
| 78 | + |
| 79 | + return ok |
| 80 | + |
| 81 | +end |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | +return _M |
0 commit comments