Skip to content
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

修复主进程可能存在的内存问题 #37

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions ngx_health_detect_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ ngx_rbtree_node_t *ngx_http_health_detect_peers_shm_rbtree_lookup(
ngx_health_detect_default_detect_policy_t *
ngx_http_health_detect_get_default_detect_policy(ngx_uint_t type);
ngx_int_t ngx_http_health_detect_add_or_update_node(
ngx_health_detect_detect_policy_t *policy);
ngx_pool_t * pool, ngx_health_detect_detect_policy_t *policy);
ngx_int_t ngx_http_health_detect_delete_node(ngx_str_t *key);
ngx_int_t ngx_http_health_detect_delete_all_node();
ngx_uint_t ngx_http_health_detect_get_down_count();
Expand All @@ -58,7 +58,7 @@ ngx_rbtree_node_t *ngx_stream_health_detect_peers_shm_rbtree_lookup(
ngx_health_detect_default_detect_policy_t *
ngx_stream_health_detect_get_default_detect_policy(ngx_uint_t type);
ngx_int_t ngx_stream_health_detect_add_or_update_node(
ngx_health_detect_detect_policy_t *policy);
ngx_pool_t * pool, ngx_health_detect_detect_policy_t *policy);
ngx_int_t ngx_stream_health_detect_delete_node(ngx_str_t *key);
ngx_int_t ngx_stream_health_detect_delete_all_node();
ngx_uint_t ngx_stream_health_detect_get_down_count();
Expand Down Expand Up @@ -774,9 +774,9 @@ ngx_health_detect_add_or_update_node(ngx_http_request_t *r, void *data)
apicf = ngx_http_get_module_loc_conf(r, ngx_health_detect_api_module);

if (apicf->used_module & NGX_HEALTH_DETECT_API_ON_HTTP) {
rc = ngx_http_health_detect_add_or_update_node(policy);
rc = ngx_http_health_detect_add_or_update_node(NULL,policy);
} else {
rc = ngx_stream_health_detect_add_or_update_node(policy);
rc = ngx_stream_health_detect_add_or_update_node(NULL,policy);
}

return rc;
Expand Down
1 change: 1 addition & 0 deletions ngx_health_detect_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ typedef struct {
ngx_peer_connection_t pc;
ngx_health_detect_detect_policy_t *policy;
ngx_pool_t *temp_pool;
ngx_uint_t using_cf_pool;

ngx_health_detect_default_detect_policy_t *default_policy;
void *check_data;
Expand Down
36 changes: 23 additions & 13 deletions ngx_http_health_detect_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ static ngx_rbtree_node_t *ngx_http_health_detect_peers_rbtree_lookup(
ngx_rbtree_node_t *ngx_http_health_detect_peers_shm_rbtree_lookup(
uint32_t hash, ngx_str_t *key);
ngx_int_t ngx_http_health_detect_add_or_update_node(
ngx_health_detect_detect_policy_t *policy);
ngx_pool_t * pool,ngx_health_detect_detect_policy_t *policy);
static ngx_int_t ngx_http_health_detect_add_or_update_node_on_shm(
ngx_health_detect_detect_policy_t *policy);
ngx_int_t ngx_http_health_detect_delete_node(ngx_str_t *key);
Expand Down Expand Up @@ -1310,7 +1310,7 @@ ngx_http_health_detect_add_or_update_node_on_shm(

static ngx_int_t
ngx_http_health_detect_add_or_update_node_on_local(
ngx_health_detect_detect_policy_t *policy, ngx_uint_t start_detect_timer)
ngx_pool_t *pool, ngx_health_detect_detect_policy_t *policy, ngx_uint_t start_detect_timer)
{
uint32_t hash;
ngx_health_detect_peer_t *opeer;
Expand Down Expand Up @@ -1352,13 +1352,17 @@ ngx_http_health_detect_add_or_update_node_on_local(
1 /*type*/ + PEER_NAME_LEN_MAX_VALUE /*peer_name*/ +
MAX_SEND_CONTENT_LEN_MAX_VALUE /*send_content*/ +
sizeof("syslog") - 1 /*alert method*/;

temp_pool = ngx_create_pool(

if(pool == NULL){
temp_pool = ngx_create_pool(
ngx_align(peer_size + peer_policy_max_size, ngx_cacheline_size),
ngx_cycle->log);
if (temp_pool == NULL) {
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0,
if (temp_pool == NULL) {
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0,
"on local: op(add/update) create pool error");
}
} else {
temp_pool = pool;
}

node = ngx_pcalloc(temp_pool, peer_size);
Expand All @@ -1373,6 +1377,12 @@ ngx_http_health_detect_add_or_update_node_on_local(
peer = (ngx_health_detect_peer_t *) &node->color;
peer->temp_pool = temp_pool;

if(pool == NULL){
peer->using_cf_pool = 0;
} else {
peer->using_cf_pool = 1;
}

peer->default_policy =
ngx_http_health_detect_get_default_detect_policy(policy->data.type);

Expand Down Expand Up @@ -1450,7 +1460,7 @@ ngx_http_health_detect_add_or_update_node_on_local(

ngx_int_t
ngx_http_health_detect_add_or_update_node(
ngx_health_detect_detect_policy_t *policy)
ngx_pool_t *pool,ngx_health_detect_detect_policy_t *policy)
{
ngx_int_t rc;

Expand All @@ -1459,7 +1469,7 @@ ngx_http_health_detect_add_or_update_node(
return rc;
}

return ngx_http_health_detect_add_or_update_node_on_local(policy, 1);
return ngx_http_health_detect_add_or_update_node_on_local(pool,policy, 1);
}

static void
Expand All @@ -1477,7 +1487,7 @@ ngx_http_health_detect_free_node(ngx_rbtree_node_t *node)

ngx_rbtree_delete(&peers_manager_ctx->peers->rbtree, node);

if (peer->temp_pool != NULL) {
if (peer->temp_pool != NULL && !peer->using_cf_pool) {
ngx_destroy_pool(peer->temp_pool);
}
}
Expand Down Expand Up @@ -2086,7 +2096,7 @@ ngx_http_health_detect_construct_policy(ngx_pool_t *temp_pool,

ngx_uint_t
ngx_http_health_detect_upstream_add_peer(
ngx_http_upstream_srv_conf_t *us, ngx_str_t *server, ngx_addr_t *peer_addr)
ngx_pool_t *pool, ngx_http_upstream_srv_conf_t *us, ngx_str_t *server, ngx_addr_t *peer_addr)
{
ngx_int_t rc;
ngx_pool_t *temp_pool;
Expand Down Expand Up @@ -2117,9 +2127,9 @@ ngx_http_health_detect_upstream_add_peer(
}

if (ngx_process == NGX_PROCESS_WORKER) {
rc = ngx_http_health_detect_add_or_update_node(policy);
rc = ngx_http_health_detect_add_or_update_node(NULL, policy);
} else {
rc = ngx_http_health_detect_add_or_update_node_on_local(policy, 0);
rc = ngx_http_health_detect_add_or_update_node_on_local(pool,policy, 0);
}

if (rc == NGX_OK) {
Expand Down Expand Up @@ -2424,7 +2434,7 @@ ngx_http_health_detect_sync_peers_shm_to_peers()
peer_name, ngx_pid);

ngx_http_health_detect_add_or_update_node_on_local(
&peer_shm->policy, 1);
NULL, &peer_shm->policy, 1);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion ngx_http_health_detect_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <ngx_http.h>

ngx_uint_t ngx_http_health_detect_upstream_add_peer(
ngx_http_upstream_srv_conf_t *us, ngx_str_t *server, ngx_addr_t *peer_addr);
ngx_pool_t *pool, ngx_http_upstream_srv_conf_t *us, ngx_str_t *server, ngx_addr_t *peer_addr);

void ngx_http_health_detect_upstream_delete_peer(
ngx_str_t *upstream_name, ngx_str_t *server_name, ngx_addr_t *peer_addr);
Expand Down
34 changes: 22 additions & 12 deletions ngx_stream_health_detect_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ static ngx_rbtree_node_t *ngx_stream_health_detect_peers_rbtree_lookup(
ngx_rbtree_node_t *ngx_stream_health_detect_peers_shm_rbtree_lookup(
uint32_t hash, ngx_str_t *key);
ngx_int_t ngx_stream_health_detect_add_or_update_node(
ngx_health_detect_detect_policy_t *policy);
ngx_pool_t * pool, ngx_health_detect_detect_policy_t *policy);
static ngx_int_t ngx_stream_health_detect_add_or_update_node_on_shm(
ngx_health_detect_detect_policy_t *policy);
ngx_int_t ngx_stream_health_detect_delete_node(ngx_str_t *key);
Expand Down Expand Up @@ -1308,7 +1308,7 @@ ngx_stream_health_detect_add_or_update_node_on_shm(

static ngx_int_t
ngx_stream_health_detect_add_or_update_node_on_local(
ngx_health_detect_detect_policy_t *policy, ngx_uint_t start_detect_timer)
ngx_pool_t *pool, ngx_health_detect_detect_policy_t *policy, ngx_uint_t start_detect_timer)
{
uint32_t hash;
ngx_health_detect_peer_t *opeer;
Expand Down Expand Up @@ -1352,12 +1352,16 @@ ngx_stream_health_detect_add_or_update_node_on_local(
MAX_SEND_CONTENT_LEN_MAX_VALUE /*send_content*/ +
sizeof("syslog") - 1 /*alert method*/;

temp_pool = ngx_create_pool(
if(pool == NULL){
temp_pool = ngx_create_pool(
ngx_align(peer_size + peer_policy_max_size, ngx_cacheline_size),
ngx_cycle->log);
if (temp_pool == NULL) {
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0,
if (temp_pool == NULL) {
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0,
"on local: op(add/update) create pool error");
}
} else {
temp_pool = pool;
}

node = ngx_pcalloc(temp_pool, peer_size);
Expand All @@ -1372,6 +1376,12 @@ ngx_stream_health_detect_add_or_update_node_on_local(
peer = (ngx_health_detect_peer_t *) &node->color;
peer->temp_pool = temp_pool;

if(pool == NULL){
peer->using_cf_pool = 0;
} else {
peer->using_cf_pool = 1;
}

peer->default_policy =
ngx_stream_health_detect_get_default_detect_policy(policy->data.type);

Expand Down Expand Up @@ -1449,7 +1459,7 @@ ngx_stream_health_detect_add_or_update_node_on_local(

ngx_int_t
ngx_stream_health_detect_add_or_update_node(
ngx_health_detect_detect_policy_t *policy)
ngx_pool_t *pool, ngx_health_detect_detect_policy_t *policy)
{
ngx_int_t rc;

Expand All @@ -1458,7 +1468,7 @@ ngx_stream_health_detect_add_or_update_node(
return rc;
}

return ngx_stream_health_detect_add_or_update_node_on_local(policy, 1);
return ngx_stream_health_detect_add_or_update_node_on_local(pool, policy, 1);
}

static void
Expand All @@ -1476,7 +1486,7 @@ ngx_stream_health_detect_free_node(ngx_rbtree_node_t *node)

ngx_rbtree_delete(&peers_manager_ctx->peers->rbtree, node);

if (peer->temp_pool != NULL) {
if (peer->temp_pool != NULL && !peer->using_cf_pool) {
ngx_destroy_pool(peer->temp_pool);
}
}
Expand Down Expand Up @@ -2096,7 +2106,7 @@ ngx_stream_health_detect_construct_policy(ngx_pool_t *temp_pool,

ngx_uint_t
ngx_stream_health_detect_upstream_add_peer(ngx_stream_upstream_srv_conf_t *us,
ngx_str_t *server, ngx_addr_t *peer_addr)
ngx_pool_t *pool, ngx_str_t *server, ngx_addr_t *peer_addr)
{
ngx_int_t rc;
ngx_pool_t *temp_pool;
Expand Down Expand Up @@ -2128,9 +2138,9 @@ ngx_stream_health_detect_upstream_add_peer(ngx_stream_upstream_srv_conf_t *us,
}

if (ngx_process == NGX_PROCESS_WORKER) {
rc = ngx_stream_health_detect_add_or_update_node(policy);
rc = ngx_stream_health_detect_add_or_update_node(NULL, policy);
} else {
rc = ngx_stream_health_detect_add_or_update_node_on_local(policy, 0);
rc = ngx_stream_health_detect_add_or_update_node_on_local(NULL, policy, 0);
}

if (rc == NGX_OK) {
Expand Down Expand Up @@ -2441,7 +2451,7 @@ ngx_stream_health_detect_sync_peers_shm_to_peers()
"process(%P)",
peer_name, ngx_pid);
ngx_stream_health_detect_add_or_update_node_on_local(
&peer_shm->policy, 1);
NULL, &peer_shm->policy, 1);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion ngx_stream_health_detect_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <ngx_stream.h>

ngx_uint_t ngx_stream_health_detect_upstream_add_peer(
ngx_stream_upstream_srv_conf_t *us, ngx_str_t *server,
ngx_pool_t *pool, ngx_stream_upstream_srv_conf_t *us, ngx_str_t *server,
ngx_addr_t *peer_addr);

void ngx_stream_health_detect_upstream_delete_peer(
Expand Down
8 changes: 4 additions & 4 deletions patch/nginx_healthdetect_for_nginx_1.12+.patch
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ index f6051ae5..e1bbae35 100644

+#if (NGX_HTTP_HEALTH_DETECT)
+ if (!server[i].down) {
+ ngx_http_health_detect_upstream_add_peer(us, &server[i].name, &server[i].addrs[j]);
+ ngx_http_health_detect_upstream_add_peer(cf->pool, us, &server[i].name, &server[i].addrs[j]);
+ }
+#endif
+
Expand All @@ -138,7 +138,7 @@ index f6051ae5..e1bbae35 100644

+#if (NGX_HTTP_HEALTH_DETECT)
+ if (!server[i].down) {
+ ngx_http_health_detect_upstream_add_peer(us, &server[i].name, &server[i].addrs[j]);
+ ngx_http_health_detect_upstream_add_peer(cf->pool,us, &server[i].name, &server[i].addrs[j]);
+ }
+#endif
+
Expand Down Expand Up @@ -271,7 +271,7 @@ index 526de3a8..d9e289f3 100644

+#if (NGX_STREAM_HEALTH_DETECT)
+ if (!server[i].down) {
+ ngx_stream_health_detect_upstream_add_peer(us, &server[i].name, &server[i].addrs[j]);
+ ngx_stream_health_detect_upstream_add_peer(cf->pool, us, &server[i].name, &server[i].addrs[j]);
+ }
+#endif
+
Expand All @@ -284,7 +284,7 @@ index 526de3a8..d9e289f3 100644

+#if (NGX_STREAM_HEALTH_DETECT)
+ if (!server[i].down) {
+ ngx_stream_health_detect_upstream_add_peer(us, &server[i].name, &server[i].addrs[j]);
+ ngx_stream_health_detect_upstream_add_peer(cf->pool, us, &server[i].name, &server[i].addrs[j]);
+ }
+#endif
+
Expand Down
8 changes: 4 additions & 4 deletions patch/nginx_healthdetect_for_nginx_1.16+.patch
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ index f72de3ee..bce1740c 100644

+#if (NGX_HTTP_HEALTH_DETECT)
+ if (!server[i].down) {
+ ngx_http_health_detect_upstream_add_peer(us, &server[i].name, &server[i].addrs[j]);
+ ngx_http_health_detect_upstream_add_peer(cf->pool,us, &server[i].name, &server[i].addrs[j]);
+ }
+#endif
+
Expand All @@ -138,7 +138,7 @@ index f72de3ee..bce1740c 100644

+#if (NGX_HTTP_HEALTH_DETECT)
+ if (!server[i].down) {
+ ngx_http_health_detect_upstream_add_peer(us, &server[i].name, &server[i].addrs[j]);
+ ngx_http_health_detect_upstream_add_peer(cf->pool,us, &server[i].name, &server[i].addrs[j]);
+ }
+#endif
+
Expand Down Expand Up @@ -271,7 +271,7 @@ index 36e2ec5c..5dadd1cb 100644

+#if (NGX_STREAM_HEALTH_DETECT)
+ if (!server[i].down) {
+ ngx_stream_health_detect_upstream_add_peer(us, &server[i].name, &server[i].addrs[j]);
+ ngx_stream_health_detect_upstream_add_peer(cf->pool, us, &server[i].name, &server[i].addrs[j]);
+ }
+#endif
+
Expand All @@ -284,7 +284,7 @@ index 36e2ec5c..5dadd1cb 100644

+#if (NGX_STREAM_HEALTH_DETECT)
+ if (!server[i].down) {
+ ngx_stream_health_detect_upstream_add_peer(us, &server[i].name, &server[i].addrs[j]);
+ ngx_stream_health_detect_upstream_add_peer(cf->pool, us, &server[i].name, &server[i].addrs[j]);
+ }
+#endif
+
Expand Down
8 changes: 4 additions & 4 deletions patch/nginx_healthdetect_for_nginx_1.18+.patch
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ index 8e7b4ea8..79c6f879 100644

+#if (NGX_HTTP_HEALTH_DETECT)
+ if (!server[i].down) {
+ ngx_http_health_detect_upstream_add_peer(us, &server[i].name, &server[i].addrs[j]);
+ ngx_http_health_detect_upstream_add_peer(cf->pool,us, &server[i].name, &server[i].addrs[j]);
+ }
+#endif
+
Expand All @@ -138,7 +138,7 @@ index 8e7b4ea8..79c6f879 100644

+#if (NGX_HTTP_HEALTH_DETECT)
+ if (!server[i].down) {
+ ngx_http_health_detect_upstream_add_peer(us, &server[i].name, &server[i].addrs[j]);
+ ngx_http_health_detect_upstream_add_peer(cf->pool,us, &server[i].name, &server[i].addrs[j]);
+ }
+#endif
+
Expand Down Expand Up @@ -271,7 +271,7 @@ index c2076673..bd458852 100644

+#if (NGX_STREAM_HEALTH_DETECT)
+ if (!server[i].down) {
+ ngx_stream_health_detect_upstream_add_peer(us, &server[i].name, &server[i].addrs[j]);
+ ngx_stream_health_detect_upstream_add_peer(cf->pool, us, &server[i].name, &server[i].addrs[j]);
+ }
+#endif
+
Expand All @@ -284,7 +284,7 @@ index c2076673..bd458852 100644

+#if (NGX_STREAM_HEALTH_DETECT)
+ if (!server[i].down) {
+ ngx_stream_health_detect_upstream_add_peer(us, &server[i].name, &server[i].addrs[j]);
+ ngx_stream_health_detect_upstream_add_peer(cf->pool, us, &server[i].name, &server[i].addrs[j]);
+ }
+#endif
+
Expand Down
Loading