Skip to content

Commit

Permalink
fix: httpc connection close
Browse files Browse the repository at this point in the history
  • Loading branch information
Madhawa Gunasekara authored and Madhawa Gunasekara committed Jul 31, 2024
1 parent 9980cb3 commit 72ca229
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 1 deletion.
4 changes: 4 additions & 0 deletions apisix/discovery/nacos/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ local require = require
local local_conf = require('apisix.core.config_local').local_conf()
local http = require('resty.http')
local core = require('apisix.core')
local connection_util = require("apisix.utils.connection-util")
local ipairs = ipairs
local type = type
local math = math
Expand Down Expand Up @@ -89,6 +90,9 @@ local function request(request_uri, path, body, method, basic_auth)
body = body,
ssl_verify = true,
})

connection_util.close_http_connection(httpc)

if not res then
return nil, err
end
Expand Down
3 changes: 2 additions & 1 deletion apisix/plugins/authz-casdoor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
-- limitations under the License.
--
local core = require("apisix.core")
local connection_util = require("apisix.utils.connection-util")
local http = require("resty.http")
local session = require("resty.session")
local ngx = ngx
Expand Down Expand Up @@ -60,7 +61,7 @@ local function fetch_access_token(code, conf)
["Content-Type"] = "application/x-www-form-urlencoded"
}
})

connection_util.close_http_connection(client)
if not res then
return nil, nil, err
end
Expand Down
3 changes: 3 additions & 0 deletions apisix/plugins/batch-requests.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
local core = require("apisix.core")
local http = require("resty.http")
local plugin = require("apisix.plugin")
local connection_util = require("apisix.utils.connection-util")
local ngx = ngx
local ipairs = ipairs
local pairs = pairs
Expand Down Expand Up @@ -258,6 +259,7 @@ local function batch_requests(ctx)

local responses, err = httpc:request_pipeline(data.pipeline)
if not responses then
connection_util.close_http_connection(httpc)
return 400, {error_msg = "request failed: " .. err}
end

Expand Down Expand Up @@ -286,6 +288,7 @@ local function batch_requests(ctx)
end
core.table.insert(aggregated_resp, sub_resp)
end
connection_util.close_http_connection(httpc)
return 200, aggregated_resp
end

Expand Down
3 changes: 3 additions & 0 deletions apisix/plugins/cas-auth.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
----
local core = require("apisix.core")
local http = require("resty.http")
local connection_util = require("apisix.utils.connection-util")
local ngx = ngx
local ngx_re_match = ngx.re.match

Expand Down Expand Up @@ -130,6 +131,8 @@ local function validate(conf, ctx, ticket)
core.log.error("validate ticket failed: status=", (res and res.status),
", has_body=", (res and res.body ~= nil or false), ", err=", err)
end

connection_util.close_http_connection(httpc)
return nil
end

Expand Down
2 changes: 2 additions & 0 deletions apisix/plugins/clickhouse-logger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

local bp_manager_mod = require("apisix.utils.batch-processor-manager")
local log_util = require("apisix.utils.log-util")
local connection_util = require("apisix.utils.connection-util")
local core = require("apisix.core")
local http = require("resty.http")
local url = require("net.url")
Expand Down Expand Up @@ -151,6 +152,7 @@ local function send_http_data(conf, log_message)
}
})

connection_util.close_http_connection(httpc)
if not httpc_res then
return false, "error while sending data to [" .. host .. "] port["
.. tostring(port) .. "] " .. httpc_err
Expand Down
3 changes: 3 additions & 0 deletions apisix/plugins/elasticsearch-logger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
local core = require("apisix.core")
local http = require("resty.http")
local log_util = require("apisix.utils.log-util")
local connection_util = require("apisix.utils.connection-util")
local bp_manager_mod = require("apisix.utils.batch-processor-manager")

local ngx = ngx
Expand Down Expand Up @@ -178,6 +179,8 @@ local function send_to_elasticsearch(conf, entries)
headers = headers,
body = body
})

connection_util.close_http_connection(httpc)
if not resp then
return false, err
end
Expand Down
4 changes: 4 additions & 0 deletions apisix/plugins/error-log-logger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
local core = require("apisix.core")
local errlog = require("ngx.errlog")
local batch_processor = require("apisix.utils.batch-processor")
local connection_util = require("apisix.utils.connection-util")
local plugin = require("apisix.plugin")
local timers = require("apisix.timers")
local http = require("resty.http")
Expand Down Expand Up @@ -273,6 +274,7 @@ local function send_to_skywalking(log_message)
}
)

connection_util.close_http_connection(httpc)
if not httpc_res then
return false, "error while sending data to skywalking["
.. config.skywalking.endpoint_addr .. "] " .. httpc_err
Expand Down Expand Up @@ -324,6 +326,8 @@ local function send_to_clickhouse(log_message)
}
)

connection_util.close_http_connection(httpc)

if not httpc_res then
return false, "error while sending data to clickhouse["
.. config.clickhouse.endpoint_addr .. "] " .. httpc_err
Expand Down
2 changes: 2 additions & 0 deletions apisix/plugins/google-cloud-logging.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ local core = require("apisix.core")
local tostring = tostring
local http = require("resty.http")
local log_util = require("apisix.utils.log-util")
local connection_util = require("apisix.utils.connection-util")
local bp_manager_mod = require("apisix.utils.batch-processor-manager")
local google_oauth = require("apisix.plugins.google-cloud-logging.oauth")

Expand Down Expand Up @@ -126,6 +127,7 @@ local function send_to_google(oauth, entries)
},
})

connection_util.close_http_connection(http_new)
if not res then
return nil, "failed to write log to google, " .. err
end
Expand Down
3 changes: 3 additions & 0 deletions apisix/plugins/google-cloud-logging/oauth.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
--

local core = require("apisix.core")
local connection_util = require("apisix.utils.connection-util")
local type = type
local setmetatable = setmetatable

Expand Down Expand Up @@ -58,6 +59,8 @@ function _M:refresh_access_token()
},
})

connection_util.close_http_connection(http_new)

if not res then
core.log.error("failed to refresh google oauth access token, ", err)
return
Expand Down
3 changes: 3 additions & 0 deletions apisix/plugins/http-logger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

local bp_manager_mod = require("apisix.utils.batch-processor-manager")
local log_util = require("apisix.utils.log-util")
local connection_util = require("apisix.utils.connection-util")
local core = require("apisix.core")
local http = require("resty.http")
local url = require("net.url")
Expand Down Expand Up @@ -146,6 +147,7 @@ local function send_http_data(conf, log_message)
})

if not httpc_res then
connection_util.close_http_connection(httpc)
return false, "error while sending data to [" .. host .. "] port["
.. tostring(port) .. "] " .. httpc_err
end
Expand All @@ -158,6 +160,7 @@ local function send_http_data(conf, log_message)
.. "body[" .. httpc_res:read_body() .. "]"
end

connection_util.close_http_connection(httpc)
return res, err_msg
end

Expand Down
2 changes: 2 additions & 0 deletions apisix/plugins/skywalking-logger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

local bp_manager_mod = require("apisix.utils.batch-processor-manager")
local log_util = require("apisix.utils.log-util")
local connection_util = require("apisix.utils.connection-util")
local core = require("apisix.core")
local http = require("resty.http")
local url = require("net.url")
Expand Down Expand Up @@ -116,6 +117,7 @@ local function send_http_data(conf, log_message)
}
})

connection_util.close_http_connection(httpc)
if not httpc_res then
return false, "error while sending data to [" .. host .. "] port["
.. tostring(port) .. "] " .. httpc_err
Expand Down
28 changes: 28 additions & 0 deletions apisix/utils/connection-util.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--
-- Licensed to the Apache Software Foundation (ASF) under one or more
-- contributor license agreements. See the NOTICE file distributed with
-- this work for additional information regarding copyright ownership.
-- The ASF licenses this file to You under the Apache License, Version 2.0
-- (the "License"); you may not use this file except in compliance with
-- the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
local core = require("apisix.core")

local _M = {}

function _M.close_http_connection(httpc)
local ok, con_err = httpc:close()
if not ok then
core.log.warn("Failed to close connection: ", con_err)
end
end

return _M

0 comments on commit 72ca229

Please sign in to comment.