From a35404d15079c73344bafdfb94e259fb7913ad84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B9=83=E6=96=8C?= Date: Wed, 28 Aug 2024 12:04:13 +0800 Subject: [PATCH] fix continuous update dyups upstream connections accumulation when upstream keepalive on --- .../ngx_http_dyups.h | 2 ++ .../ngx_http_dyups_module.c | 2 ++ .../ngx_http_upstream_keepalive_module.c | 27 +++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/modules/ngx_http_upstream_dyups_module/ngx_http_dyups.h b/modules/ngx_http_upstream_dyups_module/ngx_http_dyups.h index 54923e38ed..58350eb57f 100644 --- a/modules/ngx_http_upstream_dyups_module/ngx_http_dyups.h +++ b/modules/ngx_http_upstream_dyups_module/ngx_http_dyups.h @@ -27,4 +27,6 @@ extern ngx_flag_t ngx_http_dyups_api_enable; extern ngx_dyups_add_upstream_filter_pt ngx_dyups_add_upstream_top_filter; extern ngx_dyups_del_upstream_filter_pt ngx_dyups_del_upstream_top_filter; +extern void ngx_http_upstream_keepalive_clear_cache_connections(ngx_http_upstream_srv_conf_t *us); + #endif diff --git a/modules/ngx_http_upstream_dyups_module/ngx_http_dyups_module.c b/modules/ngx_http_upstream_dyups_module/ngx_http_dyups_module.c index 8135ad4336..fb24c0df16 100644 --- a/modules/ngx_http_upstream_dyups_module/ngx_http_dyups_module.c +++ b/modules/ngx_http_upstream_dyups_module/ngx_http_dyups_module.c @@ -1717,6 +1717,8 @@ ngx_dyups_mark_upstream_delete(ngx_http_dyups_srv_conf_t *duscf) ngx_log_error(NGX_LOG_INFO, ngx_cycle->log, 0, "[dyups] delete upstream \"%V\"", &duscf->upstream->host); + ngx_http_upstream_keepalive_clear_cache_connections(uscf); + ngx_dyups_del_upstream_top_filter(umcf, uscf); us = uscf->servers->elts; diff --git a/modules/ngx_http_upstream_keepalive_module/ngx_http_upstream_keepalive_module.c b/modules/ngx_http_upstream_keepalive_module/ngx_http_upstream_keepalive_module.c index bfe4a401ff..06c4fa0c56 100644 --- a/modules/ngx_http_upstream_keepalive_module/ngx_http_upstream_keepalive_module.c +++ b/modules/ngx_http_upstream_keepalive_module/ngx_http_upstream_keepalive_module.c @@ -1105,3 +1105,30 @@ ngx_http_upstream_keepalive_timeout(ngx_conf_t *cf, ngx_command_t *cmd, return NGX_CONF_OK; } +void +ngx_http_upstream_keepalive_clear_cache_connections(ngx_http_upstream_srv_conf_t *us) { + if (us == NULL) { + return; + } + ngx_http_upstream_keepalive_srv_conf_t *kcf; + ngx_http_upstream_keepalive_cache_t *item; + ngx_queue_t *q, *cache; + kcf = ngx_http_conf_upstream_srv_conf(us, ngx_http_upstream_keepalive_module); + if (kcf == NULL || kcf->max_cached == 0) { + return; + } + cache = &kcf->cache; + if (cache == NULL || cache->prev == NULL || cache->next == NULL) { + return; + } + kcf->timeout = 0; + for (q = ngx_queue_head(cache); q != ngx_queue_sentinel(cache); q = ngx_queue_next(q)) { + item = ngx_queue_data(q, ngx_http_upstream_keepalive_cache_t, queue); + if (item->connection && item->connection->read && item->connection->read->timer_set) { + ngx_del_timer(item->connection->read); + ngx_add_timer(item->connection->read, kcf->timeout); + } + } +} + +