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); + } + } +} + + diff --git a/tests/nginx-tests/tengine-tests/dyups.t b/tests/nginx-tests/tengine-tests/dyups.t index c044425ac6..0b435df6c6 100644 --- a/tests/nginx-tests/tengine-tests/dyups.t +++ b/tests/nginx-tests/tengine-tests/dyups.t @@ -569,6 +569,71 @@ unlike(mhttp_get('/', 'dyhost', 8080), qr/8088/m, '5/ 5 11:04:42 2014'); $t->stop(); unlink("/tmp/dyupssocket"); +############################################################################## + +$t->write_file_expand('nginx.conf', <<'EOF'); + +%%TEST_GLOBALS%% + +daemon off; + +worker_processes 1; + +events { + accept_mutex off; +} + +http { + %%TEST_GLOBALS_HTTP%% + + resolver_timeout 500ms; + + server { + listen 8080; + + location / { + proxy_http_version 1.1; + proxy_set_header Connection ""; + proxy_pass http://$host; + } + } + + server { + listen 8088; + location / { + return 200 "8088"; + } + } + + server { + listen 8089; + location / { + return 200 "8089"; + } + } + + server { + listen 8081; + location / { + dyups_interface; + } + } +} +EOF + +mrun($t); + +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'); +like(mhttp_get('/', 'dyhost', 8080), qr/8088|8089/m, '2024-08-28 14:37:23'); +like(mhttp_get('/', 'dyhost', 8080), qr/8088|8089/m, '2024-08-28 14:37:26'); +like(mhttp_get('/', 'dyhost', 8080), qr/8088|8089/m, '2024-08-28 14:37:29'); +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'); +like(mhttp_get('/', 'dyhost', 8080), qr/8088|8089/m, '2024-08-28 14:37:35'); +like(mhttp_get('/', 'dyhost', 8080), qr/8088|8089/m, '2024-08-28 14:37:38'); +like(mhttp_get('/', 'dyhost', 8080), qr/8088|8089/m, '2024-08-28 14:37:41'); + +$t->stop(); + ############################################################################### # test ssl session reuse