-
Notifications
You must be signed in to change notification settings - Fork 24
feat: support get last peer connection cached #82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
c826a55
feat: support get last peer connection cached
tzssangglass b8cc5a5
add file to compile
tzssangglass ea7a29d
disable build cache
tzssangglass 411e329
apply comments
tzssangglass 9670f5e
apply comments
tzssangglass 9819990
apply comments
tzssangglass cd967cf
add desc in README.md
tzssangglass 2e36301
fix comments
tzssangglass 074da4f
update
tzssangglass 6a0492d
apply comments
tzssangglass 0cd95cc
restore cache in ga
tzssangglass 05d3985
fix
tzssangglass 82d0e20
apply conmments
tzssangglass File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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() | ||
tzssangglass marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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, | ||
chobits marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
This file contains hidden or 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; | ||
| } | ||
|
|
||
tzssangglass marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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 hidden or 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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.