Skip to content

Commit 7ba16a8

Browse files
committed
abandon redundancy code
1 parent 67f318a commit 7ba16a8

File tree

1 file changed

+28
-75
lines changed

1 file changed

+28
-75
lines changed

src/ngx_http_dynamic_update_upstream_module.c

Lines changed: 28 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,13 @@
2323

2424
#define NGX_MAX_VALUE 65535
2525

26-
#define NGX_DELAY_DELETE 150 * 1000
26+
#define NGX_DELAY_DELETE 75 * 1000
2727

2828
#define NGX_ADD 0
2929
#define NGX_DEL 1
3030

31-
#define NGX_BLOCK 1024
32-
#define NGX_PAGESIZE 4 * 1024
33-
#define NGX_PAGE_COUNT 1024
34-
35-
#define NGX_BACKEND_NUMBER 35 /* everypage(4K) can store backend number is 35 approximately*/
31+
#define NGX_PAGE_SIZE 4 * 1024
32+
#define NGX_PAGE_NUMBER 1024
3633

3734
#define NGX_HTTP_RETRY_TIMES 3
3835
#define NGX_HTTP_SOCKET_TIMEOUT 1
@@ -84,8 +81,6 @@ typedef struct {
8481
ngx_msec_t update_timeout;
8582
ngx_msec_t update_interval;
8683

87-
ngx_msec_t delay_delete;
88-
8984
ngx_uint_t strong_dependency;
9085

9186
ngx_str_t update_send;
@@ -152,7 +147,7 @@ typedef struct {
152147

153148
enum { NONE=0, FIELD, VALUE } last_header;
154149

155-
u_char http_body[NGX_PAGESIZE * NGX_BLOCK];
150+
u_char http_body[NGX_PAGE_SIZE * NGX_PAGE_NUMBER];
156151
} ngx_http_state;
157152

158153

@@ -279,7 +274,7 @@ static void ngx_http_dynamic_update_upstream_del_delay_delete(ngx_event_t *event
279274
static ngx_int_t ngx_http_dynamic_update_upstream_need_exit();
280275
static void ngx_http_dynamic_update_upstream_clear_all_events();
281276

282-
static ngx_int_t ngx_http_dynamic_update_upstream_get_all(ngx_cycle_t *cycle,
277+
static ngx_int_t ngx_http_dynamic_update_upstream_get_upstream(ngx_cycle_t *cycle,
283278
ngx_http_dynamic_update_upstream_server_t *conf_server, char **conf_value);
284279
static ngx_http_conf_client *ngx_http_create_client(ngx_cycle_t *cycle,
285280
ngx_http_dynamic_update_upstream_server_t *conf_server);
@@ -377,7 +372,6 @@ ngx_http_dynamic_update_upstream_consul_server(ngx_conf_t *cf, ngx_command_t *cm
377372
{
378373
u_char *p = NULL;
379374
time_t update_timeout = 0, update_interval = 0;
380-
time_t delay_delete = 0;
381375
ngx_str_t *value, s;
382376
ngx_url_t u;
383377
ngx_uint_t i, strong_dependency = 0;
@@ -420,20 +414,6 @@ ngx_http_dynamic_update_upstream_consul_server(ngx_conf_t *cf, ngx_command_t *cm
420414
continue;
421415
}
422416

423-
if (ngx_strncmp(value[i].data, "delay_delete=", 13) == 0) {
424-
425-
s.len = value[i].len - 13;
426-
s.data = &value[i].data[13];
427-
428-
delay_delete = ngx_parse_time(&s, 0);
429-
430-
if (delay_delete == (time_t) NGX_ERROR) {
431-
goto invalid;
432-
}
433-
434-
continue;
435-
}
436-
437417
if (ngx_strncmp(value[i].data, "strong_dependency=", 18) == 0) {
438418
s.len = value[i].len - 18;
439419
s.data = value[i].data + 18;
@@ -464,10 +444,6 @@ ngx_http_dynamic_update_upstream_consul_server(ngx_conf_t *cf, ngx_command_t *cm
464444
duscf->update_timeout = update_timeout;
465445
}
466446

467-
if (delay_delete != 0) {
468-
duscf->delay_delete = delay_delete;
469-
}
470-
471447
if (strong_dependency != 0) {
472448
duscf->strong_dependency = strong_dependency;
473449
}
@@ -930,19 +906,13 @@ ngx_http_dynamic_update_upstream_del_server(ngx_cycle_t *cycle,
930906
struct sockaddr_in *sin;
931907

932908
ctx = &conf_server->ctx;
909+
pool = ctx->pool;
933910

934911
ngx_memzero(&us, sizeof(ngx_http_upstream_server_t));
935912

936-
pool = ngx_create_pool(ngx_pagesize, cycle->log);
937-
if (pool == NULL) {
938-
ngx_log_error(NGX_LOG_ERR, cycle->log, 0,
939-
"dynamic_update_upstream_del_server: no enough memory");
940-
return NGX_ERROR;
941-
}
942-
943913
addrs = ngx_pcalloc(pool, ctx->del_upstream.nelts * sizeof(ngx_addr_t));
944914
if (addrs == NULL) {
945-
goto invalid;
915+
return NGX_ERROR;
946916
}
947917

948918
for (i = 0, j = 0; i < ctx->del_upstream.nelts; i++, j++) {
@@ -969,7 +939,7 @@ ngx_http_dynamic_update_upstream_del_server(ngx_cycle_t *cycle,
969939

970940
sin = ngx_pcalloc(pool, sizeof(struct sockaddr_in));
971941
if (sin == NULL) {
972-
goto invalid;
942+
return NGX_ERROR;
973943
}
974944

975945
sin->sin_family = AF_INET;
@@ -988,7 +958,7 @@ ngx_http_dynamic_update_upstream_del_server(ngx_cycle_t *cycle,
988958

989959
pp = ngx_pcalloc(pool, last - p);
990960
if (pp == NULL) {
991-
goto invalid;
961+
return NGX_ERROR;
992962
}
993963

994964
addrs[j].name.len = ngx_sprintf(pp, "%s", p) - pp;
@@ -1004,18 +974,11 @@ ngx_http_dynamic_update_upstream_del_server(ngx_cycle_t *cycle,
1004974
if (us.naddrs > 0) {
1005975
if (ngx_http_dynamic_update_upstream_del_peer(cycle, &us,
1006976
conf_server) != NGX_OK) {
1007-
goto invalid;
977+
return NGX_ERROR;
1008978
}
1009979
}
1010980

1011-
ngx_destroy_pool(pool);
1012-
1013981
return NGX_OK;
1014-
1015-
invalid:
1016-
ngx_destroy_pool(pool);
1017-
1018-
return NGX_ERROR;
1019982
}
1020983

1021984

@@ -1542,8 +1505,6 @@ ngx_http_dynamic_update_upstream_create_srv_conf(ngx_conf_t *cf)
15421505
duscf->update_timeout = NGX_CONF_UNSET_MSEC;
15431506
duscf->update_interval = NGX_CONF_UNSET_MSEC;
15441507

1545-
duscf->delay_delete = NGX_CONF_UNSET_MSEC;
1546-
15471508
duscf->strong_dependency = NGX_CONF_UNSET_UINT;
15481509

15491510
duscf->conf_file = NGX_CONF_UNSET_PTR;
@@ -1589,10 +1550,6 @@ ngx_http_dynamic_update_upstream_init_srv_conf(ngx_conf_t *cf, void *conf, ngx_u
15891550
duscf->update_interval = 1000 * 5;
15901551
}
15911552

1592-
if (duscf->delay_delete == NGX_CONF_UNSET_MSEC) {
1593-
duscf->delay_delete = 1000 * 75;
1594-
}
1595-
15961553
if (duscf->strong_dependency == NGX_CONF_UNSET_UINT) {
15971554
duscf->strong_dependency = 0;
15981555
}
@@ -1742,7 +1699,7 @@ ngx_http_dynamic_update_upstream_init_process(ngx_cycle_t *cycle)
17421699

17431700
ngx_http_dynamic_update_upstream_init_peers(cycle, &conf_server[i]);
17441701

1745-
pool = ngx_create_pool(NGX_PAGE_COUNT * ngx_pagesize, ngx_cycle->log);
1702+
pool = ngx_create_pool(NGX_DEFAULT_POOL_SIZE, ngx_cycle->log);
17461703
if (pool == NULL) {
17471704
ngx_log_error(NGX_LOG_ERR, cycle->log, 0,
17481705
"dynamic_update_upstream_init_process: recv no enough memory");
@@ -1751,7 +1708,7 @@ ngx_http_dynamic_update_upstream_init_process(ngx_cycle_t *cycle)
17511708
ctx->pool = pool;
17521709

17531710
for (j = 0; j < NGX_HTTP_RETRY_TIMES; j++) {
1754-
status = ngx_http_dynamic_update_upstream_get_all(cycle,
1711+
status = ngx_http_dynamic_update_upstream_get_upstream(cycle,
17551712
&conf_server[i], &conf_value);
17561713
if (status == NGX_OK) {
17571714
break;
@@ -1774,7 +1731,7 @@ ngx_http_dynamic_update_upstream_init_process(ngx_cycle_t *cycle)
17741731

17751732
ctx->recv.pos = (u_char *)conf_value;
17761733
ctx->recv.last = (u_char *)(conf_value + ngx_strlen(conf_value));
1777-
ctx->recv.end = (u_char *)(conf_value + ngx_pagesize);
1734+
ctx->recv.end = ctx->recv.last;
17781735

17791736
if (ngx_http_parser_init() == NGX_ERROR) {
17801737
ngx_destroy_pool(pool);
@@ -2055,8 +2012,8 @@ ngx_http_dynamic_update_upstream_send_handler(ngx_event_t *event)
20552012

20562013
ctx = &peer->ctx;
20572014

2058-
u_char request[NGX_BLOCK];
2059-
ngx_memzero(request, NGX_BLOCK);
2015+
u_char request[ngx_pagesize];
2016+
ngx_memzero(request, ngx_pagesize);
20602017
ngx_sprintf(request, "GET %V?recurse&index=%d HTTP/1.0\r\nHost: %V\r\nAccept: */*\r\n\r\n",
20612018
&conf->update_send, peer->index, &conf->us.name);
20622019

@@ -2125,7 +2082,7 @@ ngx_http_dynamic_update_upstream_recv_handler(ngx_event_t *event)
21252082
ctx = &peer->ctx;
21262083

21272084
if (ctx->pool == NULL) {
2128-
pool = ngx_create_pool(NGX_PAGE_COUNT * ngx_pagesize, ngx_cycle->log);
2085+
pool = ngx_create_pool(NGX_DEFAULT_POOL_SIZE, ngx_cycle->log);
21292086

21302087
if (pool == NULL) {
21312088
ngx_log_error(NGX_LOG_ERR, event->log, 0,
@@ -2226,7 +2183,7 @@ static ngx_int_t
22262183
ngx_http_dynamic_update_upstream_dump_conf(ngx_http_dynamic_update_upstream_server_t *conf_server)
22272184
{
22282185
ngx_buf_t *b=NULL;
2229-
ngx_uint_t i, page_numbers;
2186+
ngx_uint_t i;
22302187
ngx_http_upstream_rr_peers_t *peers=NULL;
22312188
ngx_http_upstream_srv_conf_t *uscf=NULL;
22322189
ngx_http_dynamic_update_upstream_srv_conf_t *duscf=NULL;
@@ -2254,9 +2211,8 @@ ngx_http_dynamic_update_upstream_dump_conf(ngx_http_dynamic_update_upstream_serv
22542211

22552212
return NGX_ERROR;
22562213
}
2257-
page_numbers = peers->number / NGX_BACKEND_NUMBER + 1;
22582214

2259-
b = ngx_create_temp_buf(conf_server->ctx.pool, page_numbers * ngx_pagesize);
2215+
b = ngx_create_temp_buf(conf_server->ctx.pool, NGX_PAGE_SIZE * NGX_PAGE_NUMBER);
22602216
if (b == NULL) {
22612217
ngx_log_error(NGX_LOG_ERR, conf_server->ctx.pool->log, 0,
22622218
"dynamic_update_upstream_dump_conf: dump failed %V", &uscf->host);
@@ -2363,7 +2319,7 @@ ngx_http_dynamic_update_upstream_init_consul(ngx_event_t *event)
23632319
ctx = &peer->ctx;
23642320
if (ctx->pool == NULL) {
23652321

2366-
pool = ngx_create_pool(NGX_PAGE_COUNT * ngx_pagesize, ngx_cycle->log);
2322+
pool = ngx_create_pool(NGX_DEFAULT_POOL_SIZE, ngx_cycle->log);
23672323
if (pool == NULL) {
23682324
ngx_log_error(NGX_LOG_ERR, event->log, 0,
23692325
"dynamic_update_upstream_init_consul: creat pool, no enough memory");
@@ -2464,9 +2420,6 @@ ngx_http_dynamic_update_upstream_event_init(ngx_http_upstream_rr_peers_t *tmp_pe
24642420
{
24652421
ngx_time_t *tp;
24662422
ngx_delay_event_t *delay_event;
2467-
ngx_http_dynamic_update_upstream_srv_conf_t *conf;
2468-
2469-
conf = conf_server->conf;
24702423

24712424
delay_event = ngx_calloc(sizeof(*delay_event), ngx_cycle->log);
24722425
if (delay_event == NULL) {
@@ -2497,7 +2450,7 @@ ngx_http_dynamic_update_upstream_event_init(ngx_http_upstream_rr_peers_t *tmp_pe
24972450
}
24982451

24992452
delay_event->data = tmp_peers;
2500-
ngx_add_timer(&delay_event->delay_delete_ev, conf->delay_delete);
2453+
ngx_add_timer(&delay_event->delay_delete_ev, NGX_DELAY_DELETE);
25012454

25022455
return;
25032456
}
@@ -2721,7 +2674,7 @@ static ngx_int_t
27212674
ngx_http_parser_init()
27222675
{
27232676
ngx_memzero(state.status, 3);
2724-
ngx_memzero(state.http_body, NGX_PAGESIZE * NGX_BLOCK);
2677+
ngx_memzero(state.http_body, NGX_PAGE_SIZE * NGX_PAGE_NUMBER);
27252678
ngx_memzero(state.headers, NGX_MAX_HEADERS * 2 * NGX_MAX_ELEMENT_SIZE);
27262679

27272680
state.num_headers = 0;
@@ -2988,21 +2941,21 @@ ngx_http_dynamic_update_upstream_clear_all_events()
29882941

29892942

29902943
static ngx_int_t
2991-
ngx_http_dynamic_update_upstream_get_all(ngx_cycle_t *cycle,
2944+
ngx_http_dynamic_update_upstream_get_upstream(ngx_cycle_t *cycle,
29922945
ngx_http_dynamic_update_upstream_server_t *conf_server, char **conf_value)
29932946
{
29942947
ngx_http_conf_client *client = ngx_http_create_client(cycle, conf_server);
29952948

29962949
if (client == NULL) {
29972950
ngx_log_error(NGX_LOG_ERR, cycle->log, 0,
2998-
"dynamic_update_upstream_get_all: http client create error");
2951+
"dynamic_update_upstream_get_upstream: http client create error");
29992952
return NGX_ERROR;
30002953
}
30012954

30022955
ngx_int_t status = ngx_http_client_conn(client);
30032956
if (status != NGX_OK) {
30042957
ngx_log_error(NGX_LOG_ERR, cycle->log, 0,
3005-
"dynamic_update_upstream_get_all: http client conn error");
2958+
"dynamic_update_upstream_get_upstream: http client conn error");
30062959

30072960
ngx_http_client_destroy(client);
30082961
return NGX_ERROR;
@@ -3014,7 +2967,7 @@ ngx_http_dynamic_update_upstream_get_all(ngx_cycle_t *cycle,
30142967

30152968
if (ngx_http_client_recv(client, &response, 0) <= 0) {
30162969
ngx_log_error(NGX_LOG_ERR, cycle->log, 0,
3017-
"dynamic_update_upstream_get_all: http client recv fail");
2970+
"dynamic_update_upstream_get_upstream: http client recv fail");
30182971

30192972
if (response != NULL) {
30202973
ngx_free(response);
@@ -3115,8 +3068,8 @@ ngx_http_client_send(ngx_http_conf_client *client,
31153068

31163069
conf = conf_server->conf;
31173070

3118-
u_char request[NGX_BLOCK];
3119-
ngx_memzero(request, NGX_BLOCK);
3071+
u_char request[ngx_pagesize];
3072+
ngx_memzero(request, ngx_pagesize);
31203073
ngx_sprintf(request, "GET %V?recurse HTTP/1.0\r\nHost: %V\r\nAccept: */*\r\n\r\n",
31213074
&conf->update_send, &conf->us.name);
31223075

@@ -3235,7 +3188,7 @@ ngx_http_dynamic_update_upstream_show(ngx_http_request_t *r)
32353188
}
32363189
}
32373190

3238-
b = ngx_create_temp_buf(r->pool, NGX_PAGE_COUNT * ngx_pagesize);
3191+
b = ngx_create_temp_buf(r->pool, NGX_PAGE_SIZE * NGX_PAGE_NUMBER);
32393192
if (b == NULL) {
32403193
return NGX_HTTP_INTERNAL_SERVER_ERROR;
32413194
}

0 commit comments

Comments
 (0)