Skip to content

bugfix: respect max retry after using balancer pool #2331

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 2 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/ngx_http_lua_balancer.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ static ngx_int_t ngx_http_lua_balancer_by_chunk(lua_State *L,
ngx_http_request_t *r);
static void ngx_http_lua_balancer_free_peer(ngx_peer_connection_t *pc,
void *data, ngx_uint_t state);
static void ngx_http_lua_balancer_notify_peer(ngx_peer_connection_t *pc,
void *data, ngx_uint_t type);
static void ngx_http_lua_balancer_close(ngx_connection_t *c);
static void ngx_http_lua_balancer_dummy_handler(ngx_event_t *ev);
static void ngx_http_lua_balancer_close_handler(ngx_event_t *ev);
Expand Down Expand Up @@ -365,6 +367,7 @@ ngx_http_lua_balancer_init_peer(ngx_http_request_t *r,
r->upstream->peer.data = bp;
r->upstream->peer.get = ngx_http_lua_balancer_get_peer;
r->upstream->peer.free = ngx_http_lua_balancer_free_peer;
r->upstream->peer.notify = ngx_http_lua_balancer_notify_peer;

#if (NGX_HTTP_SSL)
bp->original_set_session = r->upstream->peer.set_session;
Expand Down Expand Up @@ -737,6 +740,18 @@ ngx_http_lua_balancer_free_peer(ngx_peer_connection_t *pc, void *data,
}


static void
ngx_http_lua_balancer_notify_peer(ngx_peer_connection_t *pc, void *data,
ngx_uint_t type)
{
#ifdef NGX_HTTP_UPSTREAM_NOTIFY_CACHED_CONNECTION_ERROR
if (type == NGX_HTTP_UPSTREAM_NOTIFY_CACHED_CONNECTION_ERROR) {
pc->tries--;
}
#endif
}


static void
ngx_http_lua_balancer_close(ngx_connection_t *c)
{
Expand Down
89 changes: 89 additions & 0 deletions t/188-balancer_keepalive_pool_max_retry.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# vim:set ft= ts=4 sw=4 et:

use Test::Nginx::Socket::Lua;
use Cwd qw(cwd);

log_level('info');
repeat_each(1);

plan tests => repeat_each() * (blocks() * 6);

my $pwd = cwd();

no_long_string();

check_accum_error_log();
run_tests();

__DATA__

=== TEST 1: sanity
--- http_config
lua_shared_dict request_counter 1m;
upstream my_upstream {
server 127.0.0.1;
balancer_by_lua_block {
local balancer = require "ngx.balancer"

if not ngx.ctx.tries then
ngx.ctx.tries = 0
end

ngx.ctx.tries = ngx.ctx.tries + 1
ngx.log(ngx.INFO, "tries ", ngx.ctx.tries)

if ngx.ctx.tries == 1 then
balancer.set_more_tries(5)
end

local host = "127.0.0.1"
local port = 8090;

local ok, err = balancer.set_current_peer(host, port)
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.print("hello")
else
ngx.exit(ngx.HTTP_CLOSE)
end
}
}
}
--- config
location = /t {
proxy_pass http://my_upstream;
proxy_set_header Connection "keep-alive";

rewrite_by_lua_block {
ngx.req.set_uri("/hello")
}
}
--- pipelined_requests eval
["GET /t HTTP/1.1" , "GET /t HTTP/1.1"]
--- response_body eval
["hello", qr/502/]
--- error_code eval
[200, 502]
--- no_error_log eval
qr/tries 7/
Loading