-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(module): new method
peer_conn.get_last_peer_connection_cached
(#…
…82) FTI-5616 --------- Signed-off-by: tzssangglass <[email protected]>
- Loading branch information
1 parent
71c3232
commit d1f8aaf
Showing
5 changed files
with
233 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
local ffi = require "ffi" | ||
local base = require "resty.core.base" | ||
|
||
local get_request = base.get_request | ||
local errmsg = base.get_errmsg_ptr() | ||
local C = ffi.C | ||
local ffi_str = ffi.string | ||
local get_phase = ngx.get_phase | ||
local NGX_ERROR = ngx.ERROR | ||
|
||
local error = error | ||
|
||
|
||
ffi.cdef[[ | ||
int | ||
ngx_http_lua_kong_ffi_get_last_peer_connection_cached(ngx_http_request_t *r, | ||
char **err); | ||
]] | ||
|
||
|
||
local function get_last_peer_connection_cached() | ||
if get_phase() ~= "balancer" then | ||
error("get_last_peer_connection_cached() can only be called in balancer phase") | ||
end | ||
|
||
local r = get_request() | ||
if not r then | ||
error("no request found") | ||
end | ||
|
||
local rc = C.ngx_http_lua_kong_ffi_get_last_peer_connection_cached(r, errmsg) | ||
|
||
if rc == NGX_ERROR then | ||
error(ffi_str(errmsg[0]), 2) | ||
end | ||
|
||
return rc == 1 | ||
end | ||
|
||
return { | ||
get_last_peer_connection_cached = get_last_peer_connection_cached, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Copyright 2019-2024 Kong Inc. | ||
* Licensed 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. | ||
*/ | ||
|
||
|
||
#include "ngx_http_lua_kong_common.h" | ||
|
||
|
||
int | ||
ngx_http_lua_kong_ffi_get_last_peer_connection_cached(ngx_http_request_t *r, | ||
char **err) | ||
{ | ||
if (r->upstream == NULL) { | ||
*err = "no upstream found"; | ||
return NGX_ERROR; | ||
} | ||
|
||
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | ||
"last_peer_connection_cached %d", r->upstream->peer.cached); | ||
return r->upstream->peer.cached; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
# vim:set ft= ts=4 sw=4 et: | ||
|
||
use Test::Nginx::Socket::Lua; | ||
use Cwd qw(cwd); | ||
|
||
log_level('debug'); | ||
repeat_each(1); | ||
|
||
plan tests => repeat_each() * (blocks() * 2); | ||
|
||
my $pwd = cwd(); | ||
|
||
no_long_string(); | ||
run_tests(); | ||
|
||
|
||
__DATA__ | ||
|
||
=== TEST 1: sanity | ||
--- http_config | ||
lua_package_path "../lua-resty-core/lib/?.lua;lualib/?.lua;;"; | ||
--- http_config | ||
lua_shared_dict request_counter 1m; | ||
upstream my_upstream { | ||
server 127.0.0.1; | ||
balancer_by_lua_block { | ||
local peer_conn = require("resty.kong.peer_conn") | ||
local last_peer_connection_cached = peer_conn.get_last_peer_connection_cached() | ||
|
||
local balancer = require "ngx.balancer" | ||
local host = "127.0.0.1" | ||
local port = 8090; | ||
|
||
local pool = host .. "|" .. port | ||
local pool_opts = { | ||
pool = pool, | ||
pool_size = 512, | ||
} | ||
|
||
local ok, err = balancer.set_current_peer(host, port, pool_opts) | ||
if not ok then | ||
ngx.log(ngx.ERR, "failed to set the current peer: ", err) | ||
return ngx.exit(500) | ||
end | ||
|
||
balancer.set_timeouts(60000, 60000, 60000) | ||
|
||
local ok, err = balancer.enable_keepalive(60, 100) | ||
if not ok then | ||
ngx.log(ngx.ERR, "failed to enable keepalive: ", err) | ||
return ngx.exit(500) | ||
end | ||
} | ||
} | ||
|
||
server { | ||
listen 0.0.0.0:8090; | ||
location /hello { | ||
content_by_lua_block{ | ||
local request_counter = ngx.shared.request_counter | ||
local first_request = request_counter:get("first_request") | ||
if first_request == nil then | ||
request_counter:set("first_request", "yes") | ||
ngx.say("hello") | ||
else | ||
ngx.exit(ngx.HTTP_CLOSE) | ||
end | ||
} | ||
} | ||
} | ||
--- config | ||
location /hello { | ||
proxy_pass http://my_upstream; | ||
proxy_set_header Connection "keep-alive"; | ||
} | ||
|
||
location = /t { | ||
content_by_lua_block { | ||
local http = require "resty.http" | ||
local httpc = http.new() | ||
local uri = "http://127.0.0.1:" .. ngx.var.server_port | ||
.. "/hello" | ||
local res, err = httpc:request_uri(uri) | ||
if not res then | ||
ngx.say(err) | ||
return | ||
end | ||
res, err = httpc:request_uri(uri) | ||
if not res then | ||
ngx.say(err) | ||
return | ||
end | ||
} | ||
} | ||
--- request | ||
GET /t | ||
--- error_code eval | ||
[200, 502] | ||
--- grep_error_log eval | ||
qr/last_peer_connection_cached \d+/ | ||
--- grep_error_log_out | ||
last_peer_connection_cached 0 | ||
last_peer_connection_cached 0 | ||
last_peer_connection_cached 1 |