Skip to content

Commit dbf19ed

Browse files
author
乃斌
committed
fix continuous update dyups upstream connections accumulation when upstream keepalive on
1 parent 04baff4 commit dbf19ed

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed

modules/ngx_http_upstream_dyups_module/ngx_http_dyups.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,6 @@ extern ngx_flag_t ngx_http_dyups_api_enable;
2727
extern ngx_dyups_add_upstream_filter_pt ngx_dyups_add_upstream_top_filter;
2828
extern ngx_dyups_del_upstream_filter_pt ngx_dyups_del_upstream_top_filter;
2929

30+
extern void ngx_http_upstream_keepalive_clear_cache_connections(ngx_http_upstream_srv_conf_t *us);
31+
3032
#endif

modules/ngx_http_upstream_dyups_module/ngx_http_dyups_module.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1717,6 +1717,8 @@ ngx_dyups_mark_upstream_delete(ngx_http_dyups_srv_conf_t *duscf)
17171717
ngx_log_error(NGX_LOG_INFO, ngx_cycle->log, 0,
17181718
"[dyups] delete upstream \"%V\"", &duscf->upstream->host);
17191719

1720+
ngx_http_upstream_keepalive_clear_cache_connections(uscf);
1721+
17201722
ngx_dyups_del_upstream_top_filter(umcf, uscf);
17211723

17221724
us = uscf->servers->elts;

modules/ngx_http_upstream_keepalive_module/ngx_http_upstream_keepalive_module.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,3 +1105,30 @@ ngx_http_upstream_keepalive_timeout(ngx_conf_t *cf, ngx_command_t *cmd,
11051105
return NGX_CONF_OK;
11061106
}
11071107

1108+
void
1109+
ngx_http_upstream_keepalive_clear_cache_connections(ngx_http_upstream_srv_conf_t *us) {
1110+
if (us == NULL) {
1111+
return;
1112+
}
1113+
ngx_http_upstream_keepalive_srv_conf_t *kcf;
1114+
ngx_http_upstream_keepalive_cache_t *item;
1115+
ngx_queue_t *q, *cache;
1116+
kcf = ngx_http_conf_upstream_srv_conf(us, ngx_http_upstream_keepalive_module);
1117+
if (kcf == NULL || kcf->max_cached == 0) {
1118+
return;
1119+
}
1120+
cache = &kcf->cache;
1121+
if (cache == NULL || cache->prev == NULL || cache->next == NULL) {
1122+
return;
1123+
}
1124+
kcf->timeout = 0;
1125+
for (q = ngx_queue_head(cache); q != ngx_queue_sentinel(cache); q = ngx_queue_next(q)) {
1126+
item = ngx_queue_data(q, ngx_http_upstream_keepalive_cache_t, queue);
1127+
if (item->connection && item->connection->read && item->connection->read->timer_set) {
1128+
ngx_del_timer(item->connection->read);
1129+
ngx_add_timer(item->connection->read, kcf->timeout);
1130+
}
1131+
}
1132+
}
1133+
1134+

tests/nginx-tests/tengine-tests/dyups.t

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,71 @@ unlike(mhttp_get('/', 'dyhost', 8080), qr/8088/m, '5/ 5 11:04:42 2014');
569569
$t->stop();
570570
unlink("/tmp/dyupssocket");
571571

572+
##############################################################################
573+
574+
$t->write_file_expand('nginx.conf', <<'EOF');
575+
576+
%%TEST_GLOBALS%%
577+
578+
daemon off;
579+
580+
worker_processes 1;
581+
582+
events {
583+
accept_mutex off;
584+
}
585+
586+
http {
587+
%%TEST_GLOBALS_HTTP%%
588+
589+
resolver_timeout 500ms;
590+
591+
server {
592+
listen 8080;
593+
594+
location / {
595+
proxy_http_version 1.1;
596+
proxy_set_header Connection "";
597+
proxy_pass http://$host;
598+
}
599+
}
600+
601+
server {
602+
listen 8088;
603+
location / {
604+
return 200 "8088";
605+
}
606+
}
607+
608+
server {
609+
listen 8089;
610+
location / {
611+
return 200 "8089";
612+
}
613+
}
614+
615+
server {
616+
listen 8081;
617+
location / {
618+
dyups_interface;
619+
}
620+
}
621+
}
622+
EOF
623+
624+
mrun($t);
625+
626+
like(mhttp_post('/upstream/dyhost', 'keepalive 32; keepalive_timeout 50s; server 127.0.0.1:8088; server 127.0.0.1:8089;', 8081), qr/success/m, '2024-08-28 14:37:20');
627+
like(mhttp_get('/', 'dyhost', 8080), qr/8088|8089/m, '2024-08-28 14:37:23');
628+
like(mhttp_get('/', 'dyhost', 8080), qr/8088|8089/m, '2024-08-28 14:37:26');
629+
like(mhttp_get('/', 'dyhost', 8080), qr/8088|8089/m, '2024-08-28 14:37:29');
630+
like(mhttp_post('/upstream/dyhost', 'keepalive 32; keepalive_timeout 60s; server 127.0.0.1:8088; server 127.0.0.1:8089;', 8081), qr/success/m, '2024-08-28 14:37:32');
631+
like(mhttp_get('/', 'dyhost', 8080), qr/8088|8089/m, '2024-08-28 14:37:35');
632+
like(mhttp_get('/', 'dyhost', 8080), qr/8088|8089/m, '2024-08-28 14:37:38');
633+
like(mhttp_get('/', 'dyhost', 8080), qr/8088|8089/m, '2024-08-28 14:37:41');
634+
635+
$t->stop();
636+
572637
###############################################################################
573638
# test ssl session reuse
574639

0 commit comments

Comments
 (0)